aboutsummaryrefslogtreecommitdiff
path: root/internal/commands/update.go
blob: 5889d3ac3e8ba07d8f189b0ea22e44eb3c3cc9d3 (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
package commands

import (
	"fmt"

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

func UpdateFlags() []cli.Flag {
	return []cli.Flag{
		&cli.BoolFlag{
			Name:  "output-updated-list",
			Usage: "Output a newline-seperated list of updated sources, regardless of silent mode",
		},
		&cli.BoolFlag{
			Name:  "output-formatted-updated-list",
			Usage: "Output a comma and/or ampersand list of updated sources, regardless of silent mode",
		},
		&cli.BoolFlag{
			Name:  "force-hashed",
			Usage: "Force updates for non-pinned sources that have an unchanged version (recalculate hash)",
		},
		&cli.BoolFlag{
			Name:  "force-pinned",
			Usage: "Force updates for all sources, including pinned sources (can be used with --force-hashed)",
		},
	}
}

func Update(sources *yae.Environment) func(c *cli.Context) error {
	return func(c *cli.Context) error {
		updates := []string{}
		force := c.Bool("force-hashed")
		forcePinned := c.Bool("force-pinned")

		if c.Args().Len() == 0 {
			for name, source := range sources.Sources {
				if updated, err := source.Update(sources, name, force, forcePinned); err != nil {
					return err
				} else if updated {
					updates = append(updates, name)
				}
			}
		} else {
			name := c.Args().Get(0)
			source := (*sources).Sources[name]

			if updated, err := source.Update(sources, name, force, forcePinned); err != nil {
				return err
			} else if updated {
				updates = append(updates, name)
			}
		}

		if len(updates) > 0 && !c.Bool("dry-run") {
			if err := sources.Save(c.String("sources")); err != nil {
				return err
			}
		}

		if c.Bool("output-updated-list") {
			for _, update := range updates {
				fmt.Println(update)
			}
		} else if c.Bool("output-formatted-updated-list") {
			fmt.Println(yae.Lister(updates))
		}

		return nil
	}
}