aboutsummaryrefslogtreecommitdiffstats
path: root/skate/cmd/skate-dot/main.go
blob: 573209ee551b952a6e1548eb53462f24340f9339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// [wip] skate-dot generates dot files from inbound and outbound citation
// links. Just a demo, replacement for a couple python scripts. We want things
// like: https://git.io/JObzq.
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/elastic/go-elasticsearch/esapi"
	elasticsearch "github.com/elastic/go-elasticsearch/v7"
)

var (
	es     = flag.String("es", "http://localhost:9200", "elasticsearch holding fatcat_ref index")
	index  = flag.String("x", "fatcat_ref_v01", "index name")
	fatcat = flag.String("f", "https://api.fatcat.wiki/v0", "fatcat api")
	ident  = flag.String("i", "2kw3xjf2cbcmdlm3ihkoz2t4lu", "release ident")
)

func main() {
	flag.Parse()
	cfg := elasticsearch.Config{Addresses: []string{*es}}
	es, err := elasticsearch.NewClient(cfg)
	if err != nil {
		log.Fatal(err)
	}
	client := &Client{
		Api:   *fatcat,
		Es:    es,
		Index: *index,
	}
	client.Outbound(*ident)
}

// A client for fatcat and elasticsearch.
type Client struct {
	Api   string
	Es    *elasticsearch.Client
	Index string
}

func (c *Client) String() string {
	info, _ := c.Es.Info()
	return fmt.Sprintf("%s %s (%s) %s", c.Api, info, elasticsearch.Version, c.Index)
}

func (c *Client) Inbound(ident string) []string {
	resp, err := c.Es.Search(
		c.Es.Search.WithContext(context.Background()),
		c.Es.Search.WithIndex(c.Index),
	)
	if err != nil {
		log.Fatal(err)
	}
	io.Copy(os.Stdout, resp.Body)
	return nil
}

func (c *Client) Outbound(ident string) []string {
	req := &esapi.SearchRequest{
		Query: fmt.Sprintf("source_release_ident:%s", ident),
	}
	resp, err := req.Do(context.Background(), c.Es)
	if err != nil {
		log.Fatal(err)
	}
	io.Copy(os.Stdout, resp.Body)
	return nil
}