-
Notifications
You must be signed in to change notification settings - Fork 5
/
update.go
104 lines (96 loc) · 2.6 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 cli
import (
"os"
"github.com/blang/semver"
"github.com/containerum/chkit/pkg/cli/postrun"
"github.com/containerum/chkit/pkg/cli/prerun"
"github.com/containerum/chkit/pkg/context"
"github.com/containerum/chkit/pkg/update"
"github.com/containerum/chkit/pkg/util/activekit"
"github.com/containerum/chkit/pkg/util/coblog"
"github.com/spf13/cobra"
)
func Update(ctx *context.Context) *cobra.Command {
var debug bool
command := &cobra.Command{
Use: "update",
Short: "update chkit client",
Example: "chkit update [from github|dir <path>] [--debug]",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
prerun.PreRun(ctx)
},
Run: func(cmd *cobra.Command, args []string) {
if err := updateFromGithub(ctx, debug); err != nil {
activekit.Attention(err.Error())
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
postrun.PostRun(coblog.Logger(cmd), ctx)
},
}
command.PersistentFlags().
BoolVarP(&debug, "debug", "", false, "print debug information")
command.AddCommand(fromCommand(ctx, &debug))
return command
}
func fromCommand(ctx *context.Context, debug *bool) *cobra.Command {
command := &cobra.Command{
Use: "from",
}
command.AddCommand(updateFromGithubCommand(ctx, debug))
command.AddCommand(updateFromDirCommand(ctx, debug))
return command
}
func updateFromGithubCommand(ctx *context.Context, debug *bool) *cobra.Command {
command := &cobra.Command{
Use: "github",
Short: "update from github releases",
Run: func(cmd *cobra.Command, args []string) {
if err := updateFromGithub(ctx, *debug); err != nil {
activekit.Attention(err.Error())
os.Exit(1)
}
},
}
return command
}
func updateFromDirCommand(ctx *context.Context, debug *bool) *cobra.Command {
command := &cobra.Command{
Use: "dir",
Short: "update from local directory",
Example: "chkit update from dir <path> [--debug]",
Run: func(cmd *cobra.Command, args []string) {
if len(args) <= 0 {
cmd.Help()
os.Exit(1)
}
if err := updateFromDir(ctx, args[0]); err != nil {
activekit.Attention(err.Error())
os.Exit(1)
}
},
}
return command
}
func updateFromGithub(ctx *context.Context, debug bool) error {
ver, err := semver.ParseTolerant(ctx.Version)
if err != nil {
return err
}
return update.Update(
ver,
update.NewGithubLatestCheckerDownloader("containerum", "chkit", debug),
false,
)
}
func updateFromDir(ctx *context.Context, path string) error {
ver, err := semver.ParseTolerant(ctx.Version)
if err != nil {
return err
}
return update.Update(
ver,
update.NewFileSystemLatestCheckerDownloader(path),
false,
)
}