aboutsummaryrefslogtreecommitdiff
path: root/internal/commands/add.go
blob: 951f119e01c359b26576bd45d5638e74296e03a6 (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
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
package commands

import (
	"fmt"
	"strings"

	"github.com/Fuwn/yae/internal/yae"
	"github.com/urfave/cli/v2"
)

func AddFlags() []cli.Flag {
	return []cli.Flag{
		&cli.BoolFlag{
			Name:  "unpack",
			Usage: "Unpack the source into the Nix Store",
			Value: true,
		},
		&cli.StringFlag{
			Name:     "type",
			Usage:    "Source type",
			Required: true,
			Action: func(c *cli.Context, value string) error {
				if value != "binary" && value != "git" {
					return fmt.Errorf("invalid source type: must be 'binary' or 'git'")
				}

				return nil
			},
		},
		&cli.StringFlag{
			Name:  "version",
			Usage: "Source version used in identifying latest git source",
		},
		&cli.StringFlag{
			Name:  "tag-predicate",
			Usage: "Git tag predicate used in identifying latest git source",
		},
		&cli.StringFlag{
			Name:  "trim-tag-prefix",
			Usage: "A prefix to trim from remote git tags",
		},
		&cli.BoolFlag{
			Name:  "pin",
			Usage: "Prevent the source from being updated",
		},
		&cli.BoolFlag{
			Name:  "force",
			Usage: "Always force update the source, regardless of unchanged remote tag",
		},
	}
}

func Add(sources *yae.Environment) func(c *cli.Context) error {
	return func(c *cli.Context) error {
		if c.Args().Len() != 2 {
			return fmt.Errorf("invalid number of arguments")
		}

		if sources.Exists(c.Args().Get(0)) {
			return fmt.Errorf("source already exists")
		}

		source := yae.Source{
			Unpack: c.Bool("unpack"),
			Type:   c.String("type"),
		}
		version := c.String("version")

		if version != "" {
			source.URLTemplate = c.Args().Get(1)
			source.Version = c.String("version")

			if strings.Contains(source.URLTemplate, "{version}") {
				source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version)
			} else {
				return fmt.Errorf("version template must contain {version}")
			}
		} else {
			source.URL = c.Args().Get(1)
		}

		if source.Type == "git" && c.String("tag-predicate") != "" {
			source.TagPredicate = c.String("tag-predicate")
		}

		if c.String("trim-tag-prefix") != "" {
			source.TrimTagPrefix = c.String("trim-tag-prefix")
		}

		if c.Bool("pin") {
			source.Pinned = true
		}

		if c.Bool("force") {
			if source.Pinned {
				return fmt.Errorf("cannot set a source to be statically forced and pinned at the same time")
			}

			source.Force = true
		}

		if sha256, err := yae.FetchSHA256(source.URL, c.Bool("unpack")); err != nil {
			return err
		} else {
			source.SHA256 = sha256
		}

		if err := sources.Add(c.Args().Get(0), source); err != nil {
			return err
		}

		if c.Bool("dry-run") {
			return nil
		}

		return sources.Save(c.String("sources"))
	}
}