about summary refs log tree commit diff
path: root/cmd/flakeinfo/main.go
blob: 23a5169a616afcfd91fd9a771dbc5ff683f0cf8c (plain) (blame)
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
package main

import (
	"errors"
	"flag"
	"fmt"
	"os"
	"text/template"

	"github.com/fcuny/world/internal/version"
	"github.com/fcuny/world/pkg/flake/lock"
)

const usage = `Usage:
    flake-info [flake.lock]

Options:
    -v, --version     Print version information
    -h, --help        Print this message
`

const tmplInput = ` • repository: {{ .Locked.Repository }}
 • updated on: {{ .Locked.LastModifiedRFC3339 }}

`

func main() {
	flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) }

	var (
		flakeLockPath string
		versionFlag   bool
	)

	flag.StringVar(&flakeLockPath, "flake-lock", "flake.lock", "path to the flake lock file")
	flag.BoolVar(&versionFlag, "version", false, "Print version information")
	flag.BoolVar(&versionFlag, "v", false, "Print version information")

	flag.Parse()

	if versionFlag {
		information := version.VersionAndBuildInfo()
		fmt.Println(information)
		return
	}

	if _, err := os.Stat(flakeLockPath); err != nil {
		if errors.Is(err, os.ErrNotExist) {
			fmt.Fprintf(os.Stderr, "%s does not exists\n", flakeLockPath)
		} else {
			fmt.Fprintf(os.Stderr, "failed to check if %s exists: %v\n", flakeLockPath, err)
		}
		os.Exit(1)
	}

	lock, err := lock.New(flakeLockPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse the lockfile for %s: %+v\n", flakeLockPath, err)
		os.Exit(1)
	}

	for nodeName, node := range lock.Nodes {
		tmpl, err := template.New("tmpl").Parse(tmplInput)
		if err != nil {
			panic(err)
		}
		fmt.Printf("%s\n", nodeName)
		err = tmpl.Execute(os.Stdout, node)
		if err != nil {
			panic(err)
		}
	}
}