about summary refs log tree commit diff stats
path: root/internal/importer/utils.go
blob: 1c0d3af120ec6745da59bd85e1c8fab8590f3081 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package importer

import (
	"bytes"
	"fmt"
	"io"
	"net/url"
	"os"
	"path"
	"searchix/internal/config"
	"searchix/internal/nix"

	"github.com/andybalholm/brotli"
	"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 makeGitHubFileURL(userRepo string, ref string, subPath string, line string) string {
	if ref == "" {
		ref = "master"
	}
	url, _ := url.JoinPath("https://github.com/", userRepo, "blob", ref, subPath)
	if line != "" {
		url = url + "#L" + line
	}

	return url
}

// make configurable?
var channelRepoMap = map[string]string{
	"nixpkgs":      "NixOS/nixpkgs",
	"nix-darwin":   "LnL7/nix-darwin",
	"home-manager": "nix-community/home-manager",
}

func MakeChannelLink(channel string, ref string, subPath string) (*nix.Link, error) {
	if channelRepoMap[channel] == "" {
		return nil, fmt.Errorf("don't know what repository relates to channel <%s>", channel)
	}

	return &nix.Link{
		Name: fmt.Sprintf("<%s/%s>", channel, subPath),
		URL:  makeGitHubFileURL(channelRepoMap[channel], ref, subPath, ""),
	}, nil
}

func setRepoRevision(filename string, source *config.Source) error {
	if filename != "" {
		bits, err := os.ReadFile(filename)
		if err != nil {
			return errors.WithMessagef(
				err,
				"unable to read revision file at %s",
				filename,
			)
		}

		source.Repo.Revision = string(bytes.TrimSpace(bits))

	}

	return nil
}

type brotliReadCloser struct {
	src io.ReadCloser
	*brotli.Reader
}

func newBrotliReader(src io.ReadCloser) *brotliReadCloser {
	return &brotliReadCloser{
		src:    src,
		Reader: brotli.NewReader(src),
	}
}

func (r *brotliReadCloser) Close() error {
	return errors.Wrap(r.src.Close(), "failed to call close on underlying reader")
}

func openFileDecoded(filename string) (io.ReadCloser, error) {
	var reader io.ReadCloser
	var err error
	ext := path.Ext(filename)
	reader, err = os.Open(filename)
	if err != nil {
		return nil, errors.WithMessagef(err, "failed to open file %s", filename)
	}
	switch ext {
	case ".json":
	// nothing to do
	case ".br":
		reader = newBrotliReader(reader)
	default:
		reader.Close()

		return nil, errors.Errorf("invalid file extension %s", ext)
	}

	return reader, nil
}