Skip to content

Commit c3bd8b1

Browse files
committed
chore: taco art
1 parent 10ae843 commit c3bd8b1

File tree

1 file changed

+130
-135
lines changed

1 file changed

+130
-135
lines changed

taco/cmd/taco/commands/setup.go

Lines changed: 130 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,203 +1,198 @@
11
package commands
22

3-
import(
4-
3+
import (
4+
"bufio"
5+
"encoding/json"
56
"fmt"
67
"github.com/spf13/cobra"
78
"os"
89
"path/filepath"
910
"strings"
10-
"encoding/json"
11-
"bufio"
1211
)
1312

14-
1513
var setupCmd = &cobra.Command{
16-
Use: "setup",
14+
Use: "setup",
1715
Short: "Run the configuration setup wizard",
18-
Long: "Run the configuration setup wizard to configure your OpenTaco CLI settings.",
19-
RunE: runSetup,
16+
Long: "Run the configuration setup wizard to configure your OpenTaco CLI settings.",
17+
RunE: runSetup,
2018
}
2119

22-
func init(){
23-
rootCmd.AddCommand(setupCmd)
20+
func init() {
21+
rootCmd.AddCommand(setupCmd)
2422
}
2523

26-
2724
func runSetup(cmd *cobra.Command, args []string) error {
2825
fmt.Println("OpenTaco CLI Configuration Setup")
2926
fmt.Println("This will update your configuration settings.\n")
3027

3128
config, err := runSetupWizard()
3229

3330
if err != nil {
34-
return err
31+
return err
3532
}
3633

3734
if err := saveConfig(config); err != nil {
3835
return fmt.Errorf("Failed to save configuration %w", err)
39-
}
36+
}
4037

4138
fmt.Println("Configuration Updated Successfully")
42-
return nil
39+
return nil
4340
}
4441

45-
46-
47-
4842
// return the configuration location
4943
func configPath() (string, error) {
50-
home, err := os.UserHomeDir()
51-
52-
if err != nil {
53-
return "", err
54-
}
55-
dir := filepath.Join(home, ".config", "opentaco")
56-
57-
if err := os.MkdirAll(dir, 0o755); err != nil {
58-
return "", err
59-
}
60-
return filepath.Join(dir, "config.json"), nil
44+
home, err := os.UserHomeDir()
6145

62-
}
46+
if err != nil {
47+
return "", err
48+
}
49+
dir := filepath.Join(home, ".config", "opentaco")
6350

51+
if err := os.MkdirAll(dir, 0o755); err != nil {
52+
return "", err
53+
}
54+
return filepath.Join(dir, "config.json"), nil
6455

65-
// loads and returns the config
66-
func loadConfig() (*Config, error) {
67-
path, err := configPath()
68-
69-
if err != nil {
70-
return nil, err
71-
}
72-
73-
data, err := os.ReadFile(path)
74-
75-
if os.IsNotExist(err) {
76-
return nil, nil // config file doesn't exist
77-
}
78-
79-
if err != nil {
80-
return nil, err
81-
}
82-
83-
var config Config
84-
85-
if err := json.Unmarshal(data, &config); err != nil {
86-
return nil, err
87-
}
88-
89-
return &config, nil
9056
}
9157

58+
// loads and returns the config
59+
func loadConfig() (*Config, error) {
60+
path, err := configPath()
9261

62+
if err != nil {
63+
return nil, err
64+
}
9365

66+
data, err := os.ReadFile(path)
9467

95-
// saves the configuration to the path
96-
func saveConfig(config *Config) error {
97-
path, err := configPath()
98-
99-
if err != nil {
100-
return err
101-
}
68+
if os.IsNotExist(err) {
69+
return nil, nil // config file doesn't exist
70+
}
10271

103-
data, err := json.MarshalIndent(config, "", " ")
72+
if err != nil {
73+
return nil, err
74+
}
10475

105-
if err != nil {
106-
return err
107-
}
76+
var config Config
10877

109-
return os.WriteFile(path, data, 0o600)
110-
}
78+
if err := json.Unmarshal(data, &config); err != nil {
79+
return nil, err
80+
}
11181

82+
return &config, nil
83+
}
11284

85+
// saves the configuration to the path
86+
func saveConfig(config *Config) error {
87+
path, err := configPath()
11388

89+
if err != nil {
90+
return err
91+
}
11492

115-
func loadOrCreateConfig() (*Config, error) {
116-
config, err := loadConfig()
117-
118-
if err != nil {
119-
return nil, err
120-
}
93+
data, err := json.MarshalIndent(config, "", " ")
12194

122-
// You dont have a config, start the wizard experience
123-
if config == nil {
124-
fmt.Println("Welcome to OpenTaco CLI!")
125-
fmt.Println("It looks like its your first time running the CLI.")
126-
fmt.Println("Let's setup your configuration.\n")
127-
128-
config, err = runSetupWizard()
95+
if err != nil {
96+
return err
97+
}
12998

130-
if err != nil {
131-
return nil, err
132-
}
99+
return os.WriteFile(path, data, 0o600)
100+
}
133101

134-
if err := saveConfig(config); err != nil {
135-
return nil, fmt.Errorf("Failed to save configuration: %w", err)
136-
}
102+
func loadOrCreateConfig() (*Config, error) {
103+
config, err := loadConfig()
137104

138-
fmt.Println("Configuration saved successfully!")
139-
fmt.Println("You can reconfigure anytime by running: taco setup\n")
105+
if err != nil {
106+
return nil, err
107+
}
140108

141-
}
109+
// You dont have a config, start the wizard experience
110+
if config == nil {
111+
fmt.Println()
112+
fmt.Println()
113+
fmt.Println(" /$$$$$$ /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ ")
114+
fmt.Println(" /$$__ $$ |__ $$__//$$__ $$ /$$__ $$ /$$__ $$")
115+
fmt.Println("| $$ \\ $$ /$$$$$$ /$$$$$$ /$$$$$$$ | $$ | $$ \\ $$| $$ \\__/| $$ \\ $$")
116+
fmt.Println("| $$ | $$ /$$__ $$ /$$__ $$| $$__ $$| $$ | $$$$$$$$| $$ | $$ | $$")
117+
fmt.Println("| $$ | $$| $$ \\ $$| $$$$$$$$| $$ \\ $$| $$ | $$__ $$| $$ | $$ | $$")
118+
fmt.Println("| $$ | $$| $$ | $$| $$_____/| $$ | $$| $$ | $$ | $$| $$ $$| $$ | $$")
119+
fmt.Println("| $$$$$$/| $$$$$$$/| $$$$$$$| $$ | $$| $$ | $$ | $$| $$$$$$/| $$$$$$/")
120+
fmt.Println(" \\______/ | $$____/ \\_______/|__/ |__/|__/ |__/ |__/ \\______/ \\______/ ")
121+
fmt.Println(" | $$ ")
122+
fmt.Println(" | $$ ")
123+
fmt.Println(" |__/ ")
124+
fmt.Println("")
125+
fmt.Println("🌮 Welcome to OpenTaco CLI! 🌮")
126+
fmt.Println("It looks like it's your first time running the CLI.")
127+
fmt.Println("Let's set up your configuration.\n")
128+
129+
config, err = runSetupWizard()
130+
131+
if err != nil {
132+
return nil, err
133+
}
134+
135+
if err := saveConfig(config); err != nil {
136+
return nil, fmt.Errorf("Failed to save configuration: %w", err)
137+
}
138+
139+
fmt.Println("Configuration saved successfully!")
140+
fmt.Println("You can reconfigure anytime by running: taco setup\n")
142141

142+
}
143143

144-
return config, nil
144+
return config, nil
145145

146146
}
147147

148+
func runSetupWizard() (*Config, error) {
149+
reader := bufio.NewReader(os.Stdin)
150+
config := &Config{}
148151

152+
// Get server url
149153

150-
func runSetupWizard() (*Config, error) {
151-
reader := bufio.NewReader(os.Stdin)
152-
config := &Config{}
153-
154-
// Get server url
155-
156-
for {
157-
158-
fmt.Print("Enter OpenTaco server url [http://localhost:8080]: ")
159-
serverURL, err := reader.ReadString('\n')
160-
if err != nil {
161-
return nil, err
162-
}
163-
164-
serverURL = strings.TrimSpace(serverURL)
165-
if serverURL == "" {
166-
serverURL = "http://localhost:8080"
167-
}
154+
for {
168155

169-
config.ServerUrl = serverURL
156+
fmt.Print("Enter OpenTaco server url [http://localhost:8080]: ")
157+
serverURL, err := reader.ReadString('\n')
158+
if err != nil {
159+
return nil, err
160+
}
170161

171-
break
172-
}
162+
serverURL = strings.TrimSpace(serverURL)
163+
if serverURL == "" {
164+
serverURL = "http://localhost:8080"
165+
}
173166

167+
config.ServerUrl = serverURL
174168

175-
fmt.Println("Configuration Summary:")
176-
fmt.Printf(" Server URL: %s\n", config.ServerUrl)
177-
178-
for {
179-
fmt.Print("\nSave this configuration? [Y/n]: ")
180-
confirm, err := reader.ReadString('\n')
181-
if err != nil {
182-
return nil, err
183-
}
184-
185-
confirm = strings.ToLower(strings.TrimSpace(confirm))
169+
break
170+
}
186171

187-
if confirm == "" || confirm == "y" || confirm == "yes"{
188-
return config, nil
189-
} else if confirm == "n" || confirm == "no" {
190-
fmt.Println("Configuration cancelled")
191-
os.Exit(0)
192-
} else {
193-
fmt.Println("Please enter 'y' for yes or 'n' for no.")
194-
}
195-
}
172+
fmt.Println("Configuration Summary:")
173+
fmt.Printf(" Server URL: %s\n", config.ServerUrl)
174+
175+
for {
176+
fmt.Print("\nSave this configuration? [Y/n]: ")
177+
confirm, err := reader.ReadString('\n')
178+
if err != nil {
179+
return nil, err
180+
}
181+
182+
confirm = strings.ToLower(strings.TrimSpace(confirm))
183+
184+
if confirm == "" || confirm == "y" || confirm == "yes" {
185+
return config, nil
186+
} else if confirm == "n" || confirm == "no" {
187+
fmt.Println("Configuration cancelled")
188+
os.Exit(0)
189+
} else {
190+
fmt.Println("Please enter 'y' for yes or 'n' for no.")
191+
}
192+
}
196193

197194
}
198195

199-
200-
func GetGlobalConfig() *Config {
201-
return globalConfig
196+
func GetGlobalConfig() *Config {
197+
return globalConfig
202198
}
203-

0 commit comments

Comments
 (0)