about summary refs log tree commit diff stats
path: root/process/main.go
blob: 1089144d7fe9a79ef6eb8d80bada74782344b3a9 (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
package main

import (
	"log"
	"log/slog"
	"os"
	"strings"

	"searchix/internal/options"

	"github.com/ardanlabs/conf/v3"
	"github.com/pkg/errors"
)

type Config struct {
	Input        string `conf:"short:i,required,help:NixOS options file (json)"`
	Output       string `conf:"short:o,default:/dev/stdout"`
	Revision     string `conf:"short:r,flag:revision,default:master"`
	RevisionFile string `conf:"short:f,flag:revision-file"`
	Channel      string `conf:"short:c,flag:channel,default:nixpkgs"`
}

func main() {
	if os.Getenv("DEBUG") != "" {
		slog.SetLogLoggerLevel(slog.LevelDebug)
	}
	log.SetFlags(0)

	config := Config{}
	help, err := conf.Parse("", &config)
	if err != nil {
		if errors.Is(err, conf.ErrHelpWanted) {
			log.Fatalln(help)
		}
		log.Fatalf("parsing command line: %v", err)
	}
	if config.RevisionFile != "" {
		f, err := os.ReadFile(config.RevisionFile)
		if err != nil {
			log.Fatalf("Error reading revision file %s: %v", config.RevisionFile, err)
		}
		config.Revision = strings.TrimSpace(string(f))
	}

	err = options.Process(config.Input, config.Output, config.Channel, config.Revision)
	if err != nil {
		log.Fatalf("Error processing file: %v", err)
	}
}