-
Notifications
You must be signed in to change notification settings - Fork 221
/
version.go
129 lines (108 loc) · 2.94 KB
/
version.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
122
123
124
125
126
127
128
129
package cmd
import (
"fmt"
"os"
"os/exec"
"runtime"
"github.com/spf13/viper"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/flyname"
"github.com/superfly/flyctl/docstrings"
)
func newVersionCommand() *Command {
versionStrings := docstrings.Get("version")
version := BuildCommandKS(nil, runVersion, versionStrings, os.Stdout)
version.AddBoolFlag(BoolFlagOpts{
Name: "full",
Shorthand: "f",
Description: "Show full version details",
})
version.AddStringFlag(StringFlagOpts{
Name: "completions",
Shorthand: "c",
Description: "Generate completions for supported shells bash/zsh)",
})
version.AddStringFlag(StringFlagOpts{
Name: "saveinstall",
Shorthand: "s",
Description: "Save parameter in config",
})
version.Flag("saveinstall").Hidden = true
updateStrings := docstrings.Get("version.update")
BuildCommandKS(version, runUpdate, updateStrings, os.Stdout)
return version
}
func runVersion(ctx *cmdctx.CmdContext) error {
shellType, _ := ctx.Config.GetString("completions")
if shellType != "" {
switch shellType {
case "bash":
return GetRootCommand().GenBashCompletion(os.Stdout)
case "zsh":
return GetRootCommand().GenZshCompletion(os.Stdout)
default:
return fmt.Errorf("unable to generate %s completions", shellType)
}
}
saveInstall, _ := ctx.Config.GetString("saveinstall")
if saveInstall != "" {
viper.Set(flyctl.ConfigInstaller, saveInstall)
if err := flyctl.SaveConfig(); err != nil {
return err
}
}
if ctx.Config.GetBool("full") {
if ctx.OutputJSON() {
type flyctlBuild struct {
Name string
Version string
Commit string
Build string
}
ctx.WriteJSON(flyctlBuild{Name: flyname.Name(), Version: flyctl.Version, Commit: flyctl.Commit, Build: flyctl.BuildDate})
} else {
fmt.Printf("%s %s %s %s\n", flyname.Name(), flyctl.Version, flyctl.Commit, flyctl.BuildDate)
}
} else {
if ctx.OutputJSON() {
type flyctlBuild struct {
Name string
Version string
}
ctx.WriteJSON(flyctlBuild{Name: flyname.Name(), Version: flyctl.Version})
} else {
fmt.Printf("%s %s\n", flyname.Name(), flyctl.Version)
}
}
return nil
}
func runUpdate(ctx *cmdctx.CmdContext) error {
installerstring := flyctl.CheckForUpdate(true, true) // No skipping, be silent
if installerstring == "" {
return fmt.Errorf("no update currently available")
}
shellToUse, ok := os.LookupEnv("SHELL")
switchToUse := "-c"
if !ok {
if runtime.GOOS == "windows" {
shellToUse = "powershell.exe"
switchToUse = "-Command"
} else {
shellToUse = "/bin/bash"
}
}
fmt.Println(shellToUse, switchToUse)
fmt.Println("Running automatic update [" + installerstring + "]")
cmd := exec.Command(shellToUse, switchToUse, installerstring)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
return nil
}