gunk-dev/the-valley

selfupdate.go

source937924e2f63emain's published tip history
4.3 KiB
1package main
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "os/exec"
10 "os/signal"
11 "strings"
12 "syscall"
13
14 "the-valley/selfupdate"
15)
16
17type selfUpdateArgs struct {
18 verbose, retry, rollback, pin, unpin bool
19 repo, branch string
20 help bool
21}
22
23func parseSelfUpdateArgs(args []string) (selfUpdateArgs, error) {
24 var a selfUpdateArgs
25 usage := "usage: valley self-update [-v] [--retry | --rollback | --pin | --unpin] [--repo <url>] [--branch <name>]"
26 for i := 0; i < len(args); i++ {
27 arg := args[i]
28 value := func(flag string) (string, error) {
29 if v, ok := strings.CutPrefix(arg, flag+"="); ok {
30 return v, nil
31 }
32 if i+1 >= len(args) {
33 return "", failure(flag + " needs a value; " + usage)
34 }
35 i++
36 return args[i], nil
37 }
38 var err error
39 switch {
40 case arg == "-v" || arg == "--verbose":
41 a.verbose = true
42 case arg == "--retry":
43 a.retry = true
44 case arg == "--rollback":
45 a.rollback = true
46 case arg == "--pin":
47 a.pin = true
48 case arg == "--unpin":
49 a.unpin = true
50 case arg == "-h" || arg == "--help":
51 a.help = true
52 case arg == "--repo" || strings.HasPrefix(arg, "--repo="):
53 a.repo, err = value("--repo")
54 case arg == "--branch" || strings.HasPrefix(arg, "--branch="):
55 a.branch, err = value("--branch")
56 default:
57 err = failure("unknown argument " + arg + "; " + usage)
58 }
59 if err != nil {
60 return a, err
61 }
62 }
63 n := 0
64 for _, set := range []bool{a.retry, a.rollback, a.pin, a.unpin} {
65 if set {
66 n++
67 }
68 }
69 if n > 1 {
70 return a, failure("--retry, --rollback, --pin and --unpin each ask for one thing; give one of them")
71 }
72 return a, nil
73}
74
75// runSelfUpdate is valley self-update (../selfupdate): it installs the tip
76// of a branch of the-valley into the user's nix profile, or rolls back,
77// pins or unpins what it installed. The running valley's version is its
78// stamp (main.go).
79func runSelfUpdate(args []string, stdout, stderr io.Writer) int {
80 a, err := parseSelfUpdateArgs(args)
81 if err != nil {
82 fmt.Fprintf(stderr, "valley: %s\n", err)
83 return 2
84 }
85 if a.help {
86 io.WriteString(droppable(stdout), helpText("self-update"))
87 return 0
88 }
89 if err := selfUpdate(a, stdout); err != nil {
90 fmt.Fprintf(stderr, "valley: self-update: %s\n", err)
91 return 1
92 }
93 return 0
94}
95
96func selfUpdate(a selfUpdateArgs, stdout io.Writer) error {
97 u, err := newUpdater(a, stdout)
98 if err != nil {
99 return err
100 }
101 // A systemd stop or a ^C ends the nix build and git with it.
102 ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
103 defer stop()
104 switch {
105 case a.rollback:
106 return u.Rollback(ctx)
107 case a.pin || a.unpin:
108 return u.Pin(a.pin)
109 }
110 if u.Repo == "" {
111 return failure("it is off until a repository is named, by --repo or by\n git config --global valley.selfUpdate.repo git@<valley host>:the-valley")
112 }
113 return u.Update(ctx, a.retry)
114}
115
116// newUpdater is the updater a asks for, of the running valley's stamp,
117// with the repository and branch a names or the user's git config does.
118func newUpdater(a selfUpdateArgs, stdout io.Writer) (*selfupdate.Updater, error) {
119 dir, err := selfupdate.DefaultDir()
120 if err != nil {
121 return nil, err
122 }
123 u := &selfupdate.Updater{Dir: dir, Version: versionStamp, Out: stdout, Verbose: a.verbose}
124 if u.Repo = a.repo; u.Repo == "" {
125 if u.Repo, err = userConfig("valley.selfUpdate.repo"); err != nil {
126 return nil, err
127 }
128 }
129 if u.Branch = a.branch; u.Branch == "" {
130 if u.Branch, err = userConfig("valley.selfUpdate.branch"); err != nil {
131 return nil, err
132 }
133 }
134 if u.Branch == "" {
135 u.Branch = "main"
136 }
137 return u, nil
138}
139
140// userConfig reads a key of the user's own git config, "" when it is
141// unset. git runs at /, with no variable naming a repository, so no
142// repository's config is read, and the answer is the same from any
143// directory.
144func userConfig(key string) (string, error) {
145 cmd := exec.Command("git", "config", "--get", key)
146 cmd.Dir = "/"
147 for _, kv := range gitEnvironment(os.Environ()) {
148 switch name, _, _ := strings.Cut(kv, "="); name {
149 case "GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR":
150 default:
151 cmd.Env = append(cmd.Env, kv)
152 }
153 }
154 out, err := cmd.Output()
155 var exit *exec.ExitError
156 if errors.As(err, &exit) && exit.ExitCode() == 1 {
157 return "", nil
158 }
159 if err != nil {
160 return "", fmt.Errorf("reading git config %s: %w", key, err)
161 }
162 return strings.TrimSpace(string(out)), nil
163}