-
Notifications
You must be signed in to change notification settings - Fork 12
/
update.go
121 lines (95 loc) · 3.02 KB
/
update.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
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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/spf13/cobra"
trdlClient "github.com/werf/trdl/pkg/client"
)
func updateCmd() *cobra.Command {
var noSelfUpdate bool
var inBackground bool
var backgroundStdoutFile string
var backgroundStderrFile string
cmd := &cobra.Command{
Use: "update REPO GROUP [CHANNEL]",
Short: "Update channel",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cobra.RangeArgs(2, 3)(cmd, args); err != nil {
PrintHelp(cmd)
return err
}
repoName := args[0]
group := args[1]
var optionalChannel string
if len(args) == 3 {
optionalChannel = args[2]
if err := ValidateChannel(optionalChannel); err != nil {
PrintHelp(cmd)
return err
}
}
c, err := trdlClient.NewClient(homeDir)
if err != nil {
return fmt.Errorf("unable to initialize trdl client: %s", err)
}
if inBackground {
trdlBinPath := os.Args[0]
var backgroundUpdateArgs []string
for _, arg := range os.Args[1:] {
if arg == "--in-background" || strings.HasPrefix(arg, "--in-background=") {
continue
}
backgroundUpdateArgs = append(backgroundUpdateArgs, arg)
}
if err := StartUpdateInBackground(trdlBinPath, backgroundUpdateArgs, backgroundStdoutFile, backgroundStderrFile); err != nil {
return fmt.Errorf("unable to start update in background: %s", err)
}
return nil
}
if !noSelfUpdate {
if err := c.DoSelfUpdate(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "WARNING: Self-update failed: %s\n", err)
}
}
if err := c.UpdateRepoChannel(repoName, group, optionalChannel); err != nil {
return err
}
return nil
},
}
cmd.Flags().BoolVar(&noSelfUpdate, "no-self-update", GetBoolEnvironmentDefaultFalse("TRDL_NO_SELF_UPDATE"), "Do not perform self-update")
cmd.Flags().BoolVar(&inBackground, "in-background", false, "Perform update in background")
cmd.Flags().StringVarP(&backgroundStdoutFile, "background-stdout-file", "", "", "Redirect the stdout of the background update to a file")
cmd.Flags().StringVarP(&backgroundStderrFile, "background-stderr-file", "", "", "Redirect the stderr of the background update to a file")
return cmd
}
func StartUpdateInBackground(name string, args []string, backgroundStdoutFile, backgroundStderrFile string) error {
cmd := exec.Command(name, args...)
if backgroundStdoutFile != "" {
f, err := os.Create(backgroundStdoutFile)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
cmd.Stdout = f
}
if backgroundStderrFile != "" {
f, err := os.Create(backgroundStderrFile)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
cmd.Stderr = f
}
if err := cmd.Start(); err != nil {
command := strings.Join(append([]string{name}, args...), " ")
return fmt.Errorf("unable to start command %q: %s\n", command, err)
}
if err := cmd.Process.Release(); err != nil {
return fmt.Errorf("unable to release process: %s\n", err)
}
return nil
}