-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7103ad
commit cb33cad
Showing
3 changed files
with
31 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,44 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
// Token represents token-related configuration. | ||
type Token struct { | ||
FileName string `mapstructure:"FileName"` | ||
AppName string `mapstructure:"AppName"` | ||
FolderName string `mapstructure:"FolderName"` | ||
FileName string | ||
AppName string | ||
FolderName string | ||
} | ||
|
||
// Service represents a service configuration. | ||
type Service struct { | ||
Host string `mapstructure:"host"` | ||
Host string | ||
} | ||
|
||
// Config holds the entire configuration structure. | ||
type Config struct { | ||
Token Token `mapstructure:"token"` | ||
Services map[string]Service `mapstructure:"services"` | ||
Token Token | ||
Services map[string]Service | ||
} | ||
|
||
// C is the global configuration variable. | ||
var C Config | ||
|
||
// LoadConfig loads the configuration from config.yaml. | ||
// LoadConfig initializes the configuration with hardcoded values. | ||
func LoadConfig() { | ||
exePath, err := os.Executable() | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
rootDir := filepath.Dir(exePath) | ||
configPath := filepath.Join(rootDir, "config.yaml") | ||
|
||
// Check if the config file exists | ||
if _, err := os.Stat(configPath); os.IsNotExist(err) { | ||
// If the file does not exist, create it with default values | ||
defaultConfig := []byte(`token: | ||
FileName: "token" | ||
AppName: "gitversity" | ||
FolderName: ".config" | ||
services: | ||
user_grpc: | ||
host: "grpc.dev.user.api.gitversity.com:443" | ||
assignment_grpc: | ||
host: "grpc.dev.assignment.api.gitversity.com:443" | ||
git_grpc: | ||
host: "grpc.dev.git.api.gitversity.com:443" | ||
`) | ||
|
||
err = os.WriteFile(configPath, defaultConfig, 0644) | ||
if err != nil { | ||
fmt.Println("Error creating default config file:", err) | ||
return | ||
} | ||
} | ||
|
||
viper.AddConfigPath(rootDir) | ||
viper.SetConfigName("config") | ||
viper.SetConfigType("yaml") | ||
|
||
if err := viper.ReadInConfig(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
err = viper.Unmarshal(&C) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
C = Config{ | ||
Token: Token{ | ||
FileName: "token", | ||
AppName: "gitversity", | ||
FolderName: ".config", | ||
}, | ||
Services: map[string]Service{ | ||
"user_grpc": { | ||
Host: "grpc.dev.user.api.gitversity.com:443", | ||
}, | ||
"assignment_grpc": { | ||
Host: "grpc.dev.assignment.api.gitversity.com:443", | ||
}, | ||
"git_grpc": { | ||
Host: "grpc.dev.git.api.gitversity.com:443", | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters