-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
149 lines (118 loc) · 3.7 KB
/
root.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/go-dawn/cli/cmd/internal"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)
const version = "0.0.8"
const configName = ".dawnconfig"
var (
rc = rootConfig{
CliVersionCheckInterval: int64((time.Hour * 12) / time.Second),
}
verbose bool
)
type rootConfig struct {
CliVersionCheckInterval int64 `json:"cli_version_check_interval"`
CliVersionCheckedAt int64 `json:"cli_version_checked_at"`
}
func init() {
rootCmd.AddCommand(
versionCmd, newCmd, generateCmd, devCmd, upgradeCmd,
)
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
}
var rootCmd = &cobra.Command{
Use: "dawn",
Short: "Dawn is an opinionated lightweight framework",
Long: colorizeLongDescription(),
RunE: rootRunE,
PersistentPreRun: rootPersistentPreRun,
PersistentPostRun: rootPersistentPostRun,
SilenceErrors: true,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
_ = rootCmd.Help()
_, _ = fmt.Fprintf(os.Stderr, "\n%s\n", err)
}
}
func rootRunE(cmd *cobra.Command, _ []string) error {
return cmd.Help()
}
func rootPersistentPreRun(cmd *cobra.Command, _ []string) {
if err := loadConfig(); err != nil {
warning := fmt.Sprintf("WARNING: failed to load config: %s\n\n", err)
cmd.Println(termenv.String(warning).Foreground(termenv.ANSIBrightYellow))
}
}
func rootPersistentPostRun(cmd *cobra.Command, _ []string) {
checkCliVersion(cmd)
}
func checkCliVersion(cmd *cobra.Command) {
if !needCheckCliVersion() {
return
}
cliLatestVersion, err := latestVersion(true)
if err != nil {
return
}
if version != cliLatestVersion {
title := termenv.String(fmt.Sprintf(versionUpgradeTitleFormat, version, cliLatestVersion)).
Foreground(termenv.ANSIBrightYellow)
prompt := internal.NewPrompt(title.String())
ok, err := prompt.YesOrNo()
if err == nil && ok {
upgrade(cmd, cliLatestVersion)
}
if err != nil {
warning := fmt.Sprintf("WARNING: Failed to upgrade dawn cli: %s", err)
cmd.Println(termenv.String(warning).Foreground(termenv.ANSIBrightYellow))
}
}
updateVersionCheckedAt()
}
func updateVersionCheckedAt() {
rc.CliVersionCheckedAt = time.Now().Unix()
storeConfig()
}
func needCheckCliVersion() bool {
return !upgraded && rc.CliVersionCheckedAt+rc.CliVersionCheckInterval < time.Now().Unix()
}
func colorizeLongDescription() string {
var oldnew []string
yellow := `_'/\|,`
for _, c := range yellow {
logo := termenv.String(string(c)).
Foreground(termenv.ANSIYellow).String()
oldnew = append(oldnew, string(c), logo)
}
wave := termenv.String("~").
Foreground(termenv.ANSICyan).String()
oldnew = append(oldnew, "~", wave)
v := termenv.String("v" + version).
Foreground(termenv.ANSIYellow).String()
oldnew = append(oldnew, "v"+version, v)
github := termenv.String("https://github.com/go-dawn/dawn").
Foreground(termenv.ANSICyan).
Underline().Italic().String()
oldnew = append(oldnew, "https://github.com/go-dawn/dawn", github)
replacer := strings.NewReplacer(oldnew...)
return replacer.Replace(longDescription)
}
const (
longDescription = ` __
___/ /__ __ _____ dawn-cli v` + version + `
~/ _ / _ '/ |/|/ / _ \~ For the opinionated lightweight framework dawn
~~\_,_/\_,_/|__,__/_//_/~~ Visit https://github.com/go-dawn/dawn for detail
~~~ ~~ ~~~~~~~~~ ~~~~~~ (c) since 2020 by kiyonlin@gmail.com`
versionUpgradeTitleFormat = `
You are using dawn cli version %s; however, version %s is available.
Would you like to upgrade now? (y/N)`
)