Skip to content

Commit

Permalink
update config (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 6, 2023
1 parent 35de1c1 commit 75e3994
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ const (
ExpirationKey ConfKey = "expiration"
)

func Load() (*Config, error) {
conf := new(Config)
func newConfig() *Config {
return &Config{
tokenViper: viper.New(),
clientViper: viper.New(),
}
}

func New() (*Config, error) {
conf := newConfig()

configPath, err := getConfigPath()
if err != nil {
Expand All @@ -50,13 +57,6 @@ func Load() (*Config, error) {
return nil, err
}

if err := conf.tokenViper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("fail to read token viper: %v", err)
}
if err := conf.clientViper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("fail to read client viper: %v", err)
}

return conf, nil
}

Expand Down Expand Up @@ -97,6 +97,17 @@ func setupViper(v *viper.Viper, configPath, configName, configType string) error
return nil
}

func (c *Config) Load() error {
if err := c.tokenViper.ReadInConfig(); err != nil {
return fmt.Errorf("fail to read token viper: %v", err)
}
if err := c.clientViper.ReadInConfig(); err != nil {
return fmt.Errorf("fail to read client viper: %v", err)
}

return nil
}

func (c *Config) SetToken(token *oauth2.Token) error {
c.tokenViper.Set(string(AccessTokenKey), token.AccessToken)
c.tokenViper.Set(string(RefreshTokenKey), token.RefreshToken)
Expand Down Expand Up @@ -143,3 +154,7 @@ func (c *Config) IsClientValid() bool {

return spotifyID != "" && spotifySecret != "" && openAIApiKey != ""
}

func (c *Config) GetClientValue(key ConfKey) string {
return c.clientViper.GetString(string(key))
}

0 comments on commit 75e3994

Please sign in to comment.