Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement login feature #50

Merged
merged 9 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 53 additions & 51 deletions cmd/other/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,46 @@ var configInitURLCmd = &cobra.Command{
return
}

// Create config directory if it doesn't exist
configDir := GetConfigDir()
if err := os.MkdirAll(configDir, 0755); err != nil {
pterm.Error.Printf("Failed to create config directory: %v\n", err)
return
}

// Initialize config.yaml if it doesn't exist
mainConfigPath := filepath.Join(configDir, "config.yaml")
if _, err := os.Stat(mainConfigPath); os.IsNotExist(err) {
initialConfig := []byte("environments:\n")
if err := os.WriteFile(mainConfigPath, initialConfig, 0644); err != nil {
pterm.Error.Printf("Failed to create config file: %v\n", err)
return
}
}

// Initialize the environment
if appFlag {
envName = fmt.Sprintf("%s-app", envName)
updateConfig(envName, urlStr, "app")
} else {
envName = fmt.Sprintf("%s-user", envName)
updateConfig(envName, urlStr, "user")
}

// Update configuration
updateConfig(envName, urlStr, map[bool]string{true: "app", false: "user"}[appFlag])

// Update the current environment in the main config
configDir := GetConfigDir()
mainConfigPath := filepath.Join(configDir, "config.yaml")
mainV := viper.New()
mainV.SetConfigFile(mainConfigPath)

// Read existing config or create new one
// Read the config file
if err := mainV.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
pterm.Error.Printf("Error reading config file: %v\n", err)
return
}
pterm.Error.Printf("Failed to read config file: %v\n", err)
return
}

// Set the new environment as current
mainV.Set("environment", envName)

if err := mainV.WriteConfig(); err != nil {
pterm.Error.Printf("Failed to update current environment: %v\n", err)
return
Expand Down Expand Up @@ -408,6 +423,7 @@ Available Services are fetched dynamically from the backend.`,
}

pterm.Error.Println("Please specify a service using -s or --service.")
fmt.Println()

// Fetch and display available services
baseURL, err := getBaseURL(appV)
Expand All @@ -427,9 +443,21 @@ Available Services are fetched dynamically from the backend.`,
return
}

var formattedServices []string
for _, service := range services {
if service == "identity" {
formattedServices = append(formattedServices, pterm.FgCyan.Sprintf("%s (proxy)", service))
} else {
formattedServices = append(formattedServices, pterm.FgDefault.Sprint(service))
}
}

pterm.DefaultBox.WithTitle("Available Services").
WithRightPadding(1).WithLeftPadding(1).WithTopPadding(0).WithBottomPadding(0).
Println(strings.Join(services, "\n"))
WithRightPadding(1).
WithLeftPadding(1).
WithTopPadding(0).
WithBottomPadding(0).
Println(strings.Join(formattedServices, "\n"))
return
}

Expand Down Expand Up @@ -860,41 +888,17 @@ func updateConfig(envName, urlStr, configType string) {
configDir := GetConfigDir()
mainConfigPath := filepath.Join(configDir, "config.yaml")

// Create config directory if it doesn't exist
if err := os.MkdirAll(filepath.Dir(mainConfigPath), 0755); err != nil {
pterm.Error.Printf("Failed to create config directory: %v\n", err)
return
}

// Initialize main viper instance
mainV := viper.New()
mainV.SetConfigFile(mainConfigPath)

// Read existing config or create new one
if err := mainV.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
if !os.IsNotExist(err) {
pterm.Error.Printf("Error reading config file: %v\n", err)
return
}
}

// Check if the environment already exists
if mainV.IsSet(fmt.Sprintf("environments.%s", envName)) {
pterm.Warning.Printf("Environment '%s' already exists. Do you want to overwrite it? (y/n): ", envName)

var response string
fmt.Scanln(&response)
response = strings.ToLower(strings.TrimSpace(response))

if response != "y" {
pterm.Info.Printf("Skipping initialization for '%s'. No changes were made.\n", envName)
return
}
}

// Update environment in main config
mainV.Set("environment", envName)

// Handle app type configuration
if configType == "app" && urlStr != "" {
endpoint, err := constructEndpoint(urlStr)
Expand All @@ -915,31 +919,29 @@ func updateConfig(envName, urlStr, configType string) {

// Handle user type configuration
if configType == "user" {
cacheConfigPath := filepath.Join(configDir, "cache", "config.yaml")
if err := os.MkdirAll(filepath.Dir(cacheConfigPath), 0755); err != nil {
cacheDir := filepath.Join(configDir, "cache")
if err := os.MkdirAll(cacheDir, 0755); err != nil {
pterm.Error.Printf("Failed to create cache directory: %v\n", err)
return
}

cacheV := viper.New()
cacheV.SetConfigFile(cacheConfigPath)
cacheConfigPath := filepath.Join(cacheDir, "config.yaml")

if err := cacheV.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
pterm.Error.Printf("Error reading cache config: %v\n", err)
// Create cache config file if it doesn't exist
if _, err := os.Stat(cacheConfigPath); os.IsNotExist(err) {
initialConfig := []byte("environments:\n")
if err := os.WriteFile(cacheConfigPath, initialConfig, 0644); err != nil {
pterm.Error.Printf("Failed to create cache config file: %v\n", err)
return
}
}

if cacheV.IsSet(fmt.Sprintf("environments.%s", envName)) {
pterm.Warning.Printf("Environment '%s' already exists in cache config. Do you want to overwrite it? (y/n): ", envName)

var response string
fmt.Scanln(&response)
response = strings.ToLower(strings.TrimSpace(response))
cacheV := viper.New()
cacheV.SetConfigFile(cacheConfigPath)

if response != "y" {
pterm.Info.Printf("Skipping initialization for '%s' in cache config. No changes were made.\n", envName)
if err := cacheV.ReadInConfig(); err != nil {
if !os.IsNotExist(err) {
pterm.Error.Printf("Error reading cache config: %v\n", err)
return
}
}
Expand Down
Loading
Loading