/
config.go
119 lines (103 loc) · 3.26 KB
/
config.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
"github.com/tursodatabase/turso-cli/internal/settings"
)
func init() {
rootCmd.AddCommand(configCmd)
configCmd.AddCommand(configPathCmd)
configCmd.AddCommand(configSetCmd)
configSetCmd.AddCommand(configSetAutoUpdateCmd)
configSetCmd.AddCommand(configSetTokenCmd)
configCmd.AddCommand(configCacheCmd)
configCacheCmd.AddCommand(configCacheClearCmd)
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage your CLI configuration",
}
var configSetCmd = &cobra.Command{
Use: "set",
Short: "Set a configuration value",
}
var configSetAutoUpdateCmd = &cobra.Command{
Use: "autoupdate <on|off>",
Short: "Configure autoupdate behavior",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return []string{"on", "off"}, cobra.ShellCompDirectiveNoFileComp
}
return []string{}, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
value := args[0]
if value != "on" && value != "off" {
return fmt.Errorf("autoupdate must be either 'on' or 'off'")
}
cmd.SilenceUsage = true
settings, err := settings.ReadSettings()
if err != nil {
return fmt.Errorf("failed to read settings: %w", err)
}
settings.SetAutoupdate(value)
settings.SetLastUpdateCheck(0) // trigger an update
fmt.Println("Autoupdate set to", internal.Emph(value))
return nil
},
}
var configSetTokenCmd = &cobra.Command{
Use: "token <jwt>",
Short: "Configure the token used by turso",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{}, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
config, err := settings.ReadSettings()
if err != nil {
return fmt.Errorf("failed to read settings: %w", err)
}
token := args[0]
if !isJwtTokenValid(token) {
return fmt.Errorf("invalid token")
}
config.SetToken(token)
if err := settings.TryToPersistChanges(); err != nil {
return fmt.Errorf("%w\nIf the issue persists, set your token to the %s environment variable instead", err, internal.Emph(ENV_ACCESS_TOKEN))
}
fmt.Println("Token set succesfully.")
return nil
},
}
var configCacheCmd = &cobra.Command{
Use: "cache",
Short: "Manage your CLI cache",
}
var configCacheClearCmd = &cobra.Command{
Use: "clear",
Short: "Clear your CLI local cache",
Args: cobra.ExactArgs(0),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
if err := settings.ClearCache(); err != nil {
return fmt.Errorf("failed to clear cache: %w", err)
}
fmt.Println("Local cache cleared successfully")
return nil
},
}
var configPathCmd = &cobra.Command{
Use: "path",
Short: "Get the path to the CLI configuration file",
Args: cobra.ExactArgs(0),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
_, _ = settings.ReadSettings()
fmt.Println(settings.Path())
return nil
},
}