-
Notifications
You must be signed in to change notification settings - Fork 221
/
autoupdate.go
63 lines (46 loc) · 1.59 KB
/
autoupdate.go
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
package settings
import (
"context"
"fmt"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/state"
"github.com/superfly/flyctl/iostreams"
)
func newAutoUpdate() *cobra.Command {
autoupdateRoot := command.New("autoupdate", "Control automatic updates", "", runAutoupdateStatus)
optIn := command.New("enable", "Enable automatic updates", "", func(ctx context.Context) error {
return setAutoupdateEnabled(ctx, true)
})
optOut := command.New("disable", "Disable automatic updates", "", func(ctx context.Context) error {
return setAutoupdateEnabled(ctx, false)
})
autoupdateRoot.AddCommand(optIn)
autoupdateRoot.AddCommand(optOut)
return autoupdateRoot
}
func printAutoupdateEnabled(ctx context.Context, enabled bool) {
enabledStr := lo.Ternary(enabled, "enabled", "disabled")
io := iostreams.FromContext(ctx)
fmt.Fprintf(io.Out, "Automatic updating: %s\n", enabledStr)
}
func runAutoupdateStatus(ctx context.Context) error {
var (
cfg = config.FromContext(ctx)
io = iostreams.FromContext(ctx)
)
printAutoupdateEnabled(ctx, cfg.AutoUpdate)
fmt.Fprintf(io.Out, "\nThis can be controlled with 'fly settings autoupdate <enable/disable>'\n")
return nil
}
func setAutoupdateEnabled(ctx context.Context, enabled bool) error {
path := state.ConfigFile(ctx)
if err := config.SetAutoUpdate(path, enabled); err != nil {
return fmt.Errorf("failed persisting %s in %s: %w\n",
config.AutoUpdateFileKey, path, err)
}
printAutoupdateEnabled(ctx, enabled)
return nil
}