about summary refs log tree commit diff stats
path: root/internal/importer/utils.go
blob: 7c531734dbd2760b4b47db2e11a6ade957fccdc0 (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
75
76
77
78
79
80
81
82
83
84
85
package importer

import (
	"fmt"
	"io"
	"net/url"
	"searchix/internal/config"
	"searchix/internal/nix"
	"strings"

	"github.com/bcicen/jstream"
	"github.com/pkg/errors"
)

func ValueTypeToString(valueType jstream.ValueType) string {
	switch valueType {
	case jstream.Unknown:
		return "unknown"
	case jstream.Null:
		return "null"
	case jstream.String:
		return "string"
	case jstream.Number:
		return "number"
	case jstream.Boolean:
		return "boolean"
	case jstream.Array:
		return "array"
	case jstream.Object:
		return "object"
	}

	return "very strange"
}

func makeRepoURL(repo config.Repository, subPath string, line string) (string, error) {
	switch repo.Type {
	case config.GitHub:
		ref := repo.Revision
		if ref == "" {
			ref = "master"
		}
		url, _ := url.JoinPath("https://github.com/", repo.Owner, repo.Repo, "blob", ref, subPath)
		if line != "" {
			url = url + "#L" + line
		}

		return url, nil
	default:
		return "", errors.Errorf(
			"don't know how to generate a repository URL for %s",
			repo.Type.String(),
		)
	}
}

func MakeChannelLink(repo config.Repository, subPath string) (*nix.Link, error) {
	url, err := makeRepoURL(repo, subPath, "")
	if err != nil {
		return nil, err
	}

	return &nix.Link{
		Name: fmt.Sprintf("<%s/%s>", repo.Repo, subPath),
		URL:  url,
	}, nil
}

func setRepoRevision(file io.ReadCloser, source *config.Source) error {
	if file != nil {
		defer file.Close()
		var str strings.Builder
		_, err := io.Copy(&str, file)
		if err != nil {
			return errors.WithMessagef(
				err,
				"unable to read revision file",
			)
		}

		source.Repo.Revision = strings.TrimSpace(str.String())
	}

	return nil
}