-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathselfupdate_command.go
More file actions
214 lines (199 loc) · 5.12 KB
/
Copy pathselfupdate_command.go
File metadata and controls
214 lines (199 loc) · 5.12 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"github.com/we11adam/uddns/internal/selfupdate"
)
type selfUpdater interface {
Check(context.Context, string) (selfupdate.Plan, error)
Apply(
context.Context,
selfupdate.Plan,
selfupdate.ApplyOptions,
) (selfupdate.Result, error)
Rollback(context.Context) (selfupdate.Result, error)
}
type commandDependencies struct {
newSelfUpdater func() (selfUpdater, error)
}
func defaultCommandDependencies() commandDependencies {
return commandDependencies{
newSelfUpdater: func() (selfUpdater, error) {
return selfupdate.NewForCurrentExecutable(version)
},
}
}
func runSelfUpdateCommand(
ctx context.Context,
args []string,
stdout io.Writer,
stderr io.Writer,
dependencies commandDependencies,
) int {
flags := flag.NewFlagSet("uddns self-update", flag.ContinueOnError)
flags.SetOutput(stderr)
checkOnly := flags.Bool("check", false, "Check for an update without installing it")
requestedVersion := flags.String("version", "", "Install a specific stable version")
jsonOutput := flags.Bool("json", false, "Print the result as JSON")
rollback := flags.Bool("rollback", false, "Restore the previous installed binary")
allowDevelopment := flags.Bool(
"allow-dev",
false,
"Allow a development build to be replaced",
)
allowDowngrade := flags.Bool(
"allow-downgrade",
false,
"Allow installation of an older version",
)
if err := flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 2
}
if flags.NArg() != 0 {
fmt.Fprintf(stderr, "uddns self-update: unexpected argument %q\n", flags.Arg(0))
return 2
}
if *rollback && (*checkOnly || *requestedVersion != "" ||
*allowDevelopment || *allowDowngrade) {
fmt.Fprintln(
stderr,
"uddns self-update: --rollback cannot be combined with update options",
)
return 2
}
if *checkOnly && (*allowDevelopment || *allowDowngrade) {
fmt.Fprintln(
stderr,
"uddns self-update: apply-only options cannot be used with --check",
)
return 2
}
if dependencies.newSelfUpdater == nil {
fmt.Fprintln(stderr, "uddns self-update: updater is unavailable")
return 1
}
updater, err := dependencies.newSelfUpdater()
if err != nil {
return printSelfUpdateError(stderr, err)
}
if *rollback {
result, err := updater.Rollback(ctx)
if err != nil {
return printSelfUpdateOperationError(stderr, err)
}
return printSelfUpdateResult(stdout, stderr, result, *jsonOutput, true)
}
plan, err := updater.Check(ctx, *requestedVersion)
if err != nil {
return printSelfUpdateError(stderr, err)
}
if *checkOnly {
return printSelfUpdatePlan(stdout, stderr, plan, *jsonOutput)
}
result, err := updater.Apply(ctx, plan, selfupdate.ApplyOptions{
AllowDevelopment: *allowDevelopment,
AllowDowngrade: *allowDowngrade,
})
if err != nil {
return printSelfUpdateOperationError(stderr, err)
}
return printSelfUpdateResult(stdout, stderr, result, *jsonOutput, false)
}
func printSelfUpdatePlan(
stdout io.Writer,
stderr io.Writer,
plan selfupdate.Plan,
jsonOutput bool,
) int {
if jsonOutput {
if err := json.NewEncoder(stdout).Encode(plan); err != nil {
return printSelfUpdateError(stderr, err)
}
return 0
}
var message string
switch plan.Status {
case selfupdate.StatusDevelopment:
message = fmt.Sprintf(
"development build; latest stable version is %s",
plan.TargetVersion,
)
case selfupdate.StatusNewerThanTarget:
message = fmt.Sprintf(
"installed version %s is newer than target %s",
plan.CurrentVersion,
plan.TargetVersion,
)
case selfupdate.StatusUpdateAvailable:
message = fmt.Sprintf(
"update available: %s -> %s",
plan.CurrentVersion,
plan.TargetVersion,
)
case selfupdate.StatusUpToDate:
message = fmt.Sprintf("up to date: %s", plan.TargetVersion)
default:
return printSelfUpdateError(
stderr,
fmt.Errorf("unknown update status %q", plan.Status),
)
}
if _, err := fmt.Fprintln(stdout, message); err != nil {
return printSelfUpdateError(stderr, err)
}
return 0
}
func printSelfUpdateResult(
stdout io.Writer,
stderr io.Writer,
result selfupdate.Result,
jsonOutput bool,
rollback bool,
) int {
if jsonOutput {
if err := json.NewEncoder(stdout).Encode(result); err != nil {
return printSelfUpdateError(stderr, err)
}
return 0
}
if !result.Changed {
if _, err := fmt.Fprintf(stdout, "already up to date: %s\n", result.ToVersion); err != nil {
return printSelfUpdateError(stderr, err)
}
return 0
}
action := "updated"
if rollback {
action = "rolled back"
}
if _, err := fmt.Fprintf(
stdout,
"%s uddns from %s to %s\nbackup: %s\n",
action,
result.FromVersion,
result.ToVersion,
result.BackupPath,
); err != nil {
return printSelfUpdateError(stderr, err)
}
return 0
}
func printSelfUpdateError(stderr io.Writer, err error) int {
fmt.Fprintf(stderr, "uddns self-update: %v\n", err)
return 1
}
func printSelfUpdateOperationError(stderr io.Writer, err error) int {
printSelfUpdateError(stderr, err)
var permissionError *selfupdate.LocalPermissionError
if errors.As(err, &permissionError) {
fmt.Fprintln(stderr, "try running the same command with sudo")
}
return 1
}