-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
104 lines (80 loc) · 2.19 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
package cmd
import (
"context"
"fmt"
"log"
"net/http"
"os"
"runtime"
"github.com/minio/selfupdate"
"github.com/spf13/cobra"
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Self update adb",
Long: "Self update adb",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
doUpdate()
},
}
func doUpdate() {
dl := "https://github.com/airdb/adb/releases/latest/download/adb"
if runtime.GOOS == "darwin" {
dl = dl + "-" + runtime.GOOS
}
fmt.Printf("It will take about 1 minute for downloading.\nDownload url: %s\n", dl)
client := &http.Client{}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, dl, nil)
if err != nil {
log.Println(err)
return
}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
if err != nil {
log.Println("update failed!")
} else {
log.Println("update successfully!")
}
}
func updateCmdInit() {
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(completionBashCmd)
completionBashCmd.PersistentFlags().BoolVarP(&writeCompletionFile, "write_file", "w", false, "write completion file")
}
var writeCompletionFile bool
var completionBashCmdLongDesc = `To load completion run
. <(bitbucket completion)
To configure your bash shell to load completions for each session add to your bashrc
# MacOS:
# adb completion >/usr/local/etc/bash_completion.d/adb
# ~/.bashrc or ~/.profile
. <(bitbucket completion)
`
// CompletionCmd represents the completion command.
var completionBashCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash completion scripts",
Long: completionBashCmdLongDesc,
Run: func(cmd *cobra.Command, args []string) {
if writeCompletionFile {
completionFile := "/usr/local/etc/bash_completion.d/adb"
err := rootCmd.GenFishCompletionFile(completionFile, true)
if err != nil {
panic(err)
}
fmt.Println("Generates bash completion scripts successfully, file:", completionFile)
return
}
err := rootCmd.GenBashCompletion(os.Stdout)
if err != nil {
fmt.Println("Generates bash completion scripts failed!")
}
},
}