-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathroot.go
146 lines (128 loc) · 4.17 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
package cmd
import (
"context"
"fmt"
"os"
"path"
"time"
api "github.com/bootdotdev/bootdev/client"
"github.com/bootdotdev/bootdev/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var rootCmd = &cobra.Command{
Use: "bootdev",
Short: "The official boot.dev CLI",
Long: `The official CLI for boot.dev. This program is meant
to be a companion app (not a replacement) for the website.`,
}
// 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(currentVersion string) error {
rootCmd.Version = currentVersion
info := version.FetchUpdateInfo(rootCmd.Version)
defer info.PromptUpdateIfAvailable()
ctx := version.WithContext(context.Background(), &info)
return rootCmd.ExecuteContext(ctx)
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bootdev.yaml)")
}
func readViperConfig(paths []string) error {
for _, path := range paths {
_, err := os.Stat(path)
if err == nil {
viper.SetConfigFile(path)
break
}
}
return viper.ReadInConfig()
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetDefault("frontend_url", "https://boot.dev")
viper.SetDefault("api_url", "https://api.boot.dev")
viper.SetDefault("access_token", "")
viper.SetDefault("refresh_token", "")
viper.SetDefault("last_refresh", 0)
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
err := viper.ReadInConfig()
cobra.CheckErr(err)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// viper's built in config path thing sucks, let's do it ourselves
defaultPath := path.Join(home, ".bootdev.yaml")
configPaths := []string{}
configPaths = append(configPaths, path.Join(home, ".config", "bootdev", "config.yaml"))
configPaths = append(configPaths, defaultPath)
if err := readViperConfig(configPaths); err != nil {
viper.SafeWriteConfigAs(defaultPath)
viper.SetConfigFile(defaultPath)
err = viper.ReadInConfig()
cobra.CheckErr(err)
}
}
viper.SetEnvPrefix("bd")
viper.AutomaticEnv() // read in environment variables that match
}
// Chain multiple commands together.
func compose(commands ...func(cmd *cobra.Command, args []string)) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
for _, command := range commands {
command(cmd, args)
}
}
}
// Call this function at the beginning of a command handler
// if you want to require the user to update their CLI first.
func requireUpdated(cmd *cobra.Command, args []string) {
info := version.FromContext(cmd.Context())
if info == nil {
fmt.Fprintln(os.Stderr, "Failed to fetch update info. Are you online?")
os.Exit(1)
}
if info.FailedToFetch != nil {
fmt.Fprintf(os.Stderr, "Failed to fetch update info: %s\n", info.FailedToFetch.Error())
os.Exit(1)
}
if info.IsUpdateRequired {
info.PromptUpdateIfAvailable()
os.Exit(1)
}
}
// Call this function at the beginning of a command handler
// if you need to make authenticated requests. This will
// automatically refresh the tokens, if necessary, and prompt
// the user to re-login if anything goes wrong.
func requireAuth(cmd *cobra.Command, args []string) {
promptLoginAndExitIf := func(condition bool) {
if condition {
fmt.Fprintln(os.Stderr, "You must be logged in to use that command.")
fmt.Fprintln(os.Stderr, "Please run 'bootdev login' first.")
os.Exit(1)
}
}
access_token := viper.GetString("access_token")
promptLoginAndExitIf(access_token == "")
// We only refresh if our token is getting stale.
last_refresh := viper.GetInt64("last_refresh")
if time.Now().Add(-time.Minute*55).Unix() <= last_refresh {
return
}
creds, err := api.FetchAccessToken()
promptLoginAndExitIf(err != nil)
if creds.AccessToken == "" || creds.RefreshToken == "" {
promptLoginAndExitIf(err != nil)
}
viper.Set("access_token", creds.AccessToken)
viper.Set("refresh_token", creds.RefreshToken)
viper.Set("last_refresh", time.Now().Unix())
err = viper.WriteConfig()
promptLoginAndExitIf(err != nil)
}