Skip to content

Commit fb8102b

Browse files
committed
adjusted imports, gitignore, refactor into setup.go, add setup.go
1 parent 7b398c8 commit fb8102b

File tree

3 files changed

+206
-160
lines changed

3 files changed

+206
-160
lines changed

taco/.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
taco
2-
statesman
3-
terraform-provider-opentaco
1+
/taco
2+
/statesman
3+
/terraform-provider-opentaco

taco/cmd/taco/commands/root.go

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ package commands
33
import (
44
"fmt"
55
"os"
6-
"encoding/json"
7-
"path/filepath"
8-
"strings"
9-
"bufio"
106
"github.com/spf13/cobra"
117
)
128

@@ -65,156 +61,7 @@ func init() {
6561
}
6662

6763

68-
// return the configuration location
69-
func configPath() (string, error) {
70-
home, err := os.UserHomeDir()
71-
72-
if err != nil {
73-
return "", err
74-
}
75-
dir := filepath.Join(home, ".config", "opentaco")
76-
77-
if err := os.MkdirAll(dir, 0o755); err != nil {
78-
return "", err
79-
}
80-
return filepath.Join(dir, "config.json"), nil
81-
82-
}
83-
84-
85-
// loads and returns the config
86-
func loadConfig() (*Config, error) {
87-
path, err := configPath()
88-
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
data, err := os.ReadFile(path)
94-
95-
if os.IsNotExist(err) {
96-
return nil, nil // config file doesn't exist
97-
}
98-
99-
if err != nil {
100-
return nil, err
101-
}
102-
103-
var config Config
104-
105-
if err := json.Unmarshal(data, &config); err != nil {
106-
return nil, err
107-
}
108-
109-
return &config, nil
110-
}
111-
112-
113-
114-
115-
// saves the configuration to the path
116-
func saveConfig(config *Config) error {
117-
path, err := configPath()
118-
119-
if err != nil {
120-
return err
121-
}
122-
123-
data, err := json.MarshalIndent(config, "", " ")
124-
125-
if err != nil {
126-
return err
127-
}
128-
129-
return os.WriteFile(path, data, 0o600)
130-
}
131-
132-
133-
13464

135-
func loadOrCreateConfig() (*Config, error) {
136-
config, err := loadConfig()
137-
138-
if err != nil {
139-
return nil, err
140-
}
141-
142-
// You dont have a config, start the wizard experience
143-
if config == nil {
144-
fmt.Println("Welcome to OpenTaco CLI!")
145-
fmt.Println("It looks like its your first time running the CLI.")
146-
fmt.Println("Let's setup your configuration.\n")
147-
148-
config, err = runSetupWizard()
149-
150-
if err != nil {
151-
return nil, err
152-
}
153-
154-
if err := saveConfig(config); err != nil {
155-
return nil, fmt.Errorf("Failed to save configuration: %w", err)
156-
}
157-
158-
fmt.Println("Configuration saved successfully!")
159-
fmt.Println("You can reconfigure anytime by running: taco setup\n")
160-
161-
}
162-
163-
164-
return config, nil
165-
166-
}
167-
168-
169-
170-
func runSetupWizard() (*Config, error) {
171-
reader := bufio.NewReader(os.Stdin)
172-
config := &Config{}
173-
174-
// Get server url
175-
176-
for {
177-
178-
fmt.Print("Enter OpenTaco server url [http://localhost:8080]: ")
179-
serverURL, err := reader.ReadString('\n')
180-
if err != nil {
181-
return nil, err
182-
}
183-
184-
serverURL = strings.TrimSpace(serverURL)
185-
if serverURL == "" {
186-
serverURL = "http://localhost:8080"
187-
}
188-
189-
config.ServerUrl = serverURL
190-
191-
break
192-
}
193-
194-
195-
fmt.Println("Configuration Summary:")
196-
fmt.Printf(" Server URL: %s\n", config.ServerUrl)
197-
198-
for {
199-
fmt.Print("\nSave this configuration? [Y/n]: ")
200-
confirm, err := reader.ReadString('\n')
201-
if err != nil {
202-
return nil, err
203-
}
204-
205-
confirm = strings.ToLower(strings.TrimSpace(confirm))
206-
207-
if confirm == "" || confirm == "y" || confirm == "yes"{
208-
return config, nil
209-
} else if confirm == "n" || confirm == "no" {
210-
fmt.Println("Configuration cancelled")
211-
os.Exit(0)
212-
} else {
213-
fmt.Println("Please enter 'y' for yes or 'n' for no.")
214-
}
215-
}
216-
217-
}
21865

21966
// getEnvOrDefault gets an environment variable or returns a default value
22067
func getEnvOrDefault(key, defaultValue string) string {
@@ -233,8 +80,4 @@ func printVerbose(format string, args ...interface{}) {
23380

23481

23582

236-
func GetGlobalConfig() *Config {
237-
return globalConfig
238-
}
239-
24083

taco/cmd/taco/commands/setup.go

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package commands
2+
3+
import(
4+
5+
"fmt"
6+
"github.com/spf13/cobra"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
"encoding/json"
11+
"bufio"
12+
)
13+
14+
15+
var setupCmd = &cobra.Command{
16+
Use: "setup",
17+
Short: "Run the configuration setup wizard",
18+
Long: "Run the configuration setup wizard to configure your OpenTaco CLI settings.",
19+
RunE: runSetup,
20+
}
21+
22+
func init(){
23+
rootCmd.AddCommand(setupCmd)
24+
}
25+
26+
27+
func runSetup(cmd *cobra.Command, args []string) error {
28+
fmt.Println("OpenTaco CLI Configuration Setup")
29+
fmt.Println("This will update your configuration settings.\n")
30+
31+
config, err := runSetupWizard()
32+
33+
if err != nil {
34+
return err
35+
}
36+
37+
if err := saveConfig(config); err != nil {
38+
return fmt.Errorf("Failed to save configuration %w", err)
39+
}
40+
41+
fmt.Println("Configuration Updated Successfully")
42+
return nil
43+
}
44+
45+
46+
47+
48+
// return the configuration location
49+
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
61+
62+
}
63+
64+
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
90+
}
91+
92+
93+
94+
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+
}
102+
103+
data, err := json.MarshalIndent(config, "", " ")
104+
105+
if err != nil {
106+
return err
107+
}
108+
109+
return os.WriteFile(path, data, 0o600)
110+
}
111+
112+
113+
114+
115+
func loadOrCreateConfig() (*Config, error) {
116+
config, err := loadConfig()
117+
118+
if err != nil {
119+
return nil, err
120+
}
121+
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()
129+
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
if err := saveConfig(config); err != nil {
135+
return nil, fmt.Errorf("Failed to save configuration: %w", err)
136+
}
137+
138+
fmt.Println("Configuration saved successfully!")
139+
fmt.Println("You can reconfigure anytime by running: taco setup\n")
140+
141+
}
142+
143+
144+
return config, nil
145+
146+
}
147+
148+
149+
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+
}
168+
169+
config.ServerUrl = serverURL
170+
171+
break
172+
}
173+
174+
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))
186+
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+
}
196+
197+
}
198+
199+
200+
func GetGlobalConfig() *Config {
201+
return globalConfig
202+
}
203+

0 commit comments

Comments
 (0)