Skip to content

Commit

Permalink
refactor(config): ♻️ config adjustments
Browse files Browse the repository at this point in the history
rename interface{} -> any; fix insecure perms on config file; don't use filepath.Join unnecessarily; gofumpt files
  • Loading branch information
joshuar committed Jan 24, 2024
1 parent 8514f5f commit 0cab594
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 8 additions & 8 deletions pkg/config/tomlconfig/tomlconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type tomlConfig struct {
file string
}

func (c *tomlConfig) read() (map[string]interface{}, error) {
items := make(map[string]interface{})
func (c *tomlConfig) read() (map[string]any, error) {
items := make(map[string]any)
b, err := os.ReadFile(c.file)
if err != nil {
return nil, err
Expand All @@ -38,19 +38,19 @@ func (c *tomlConfig) read() (map[string]interface{}, error) {
return items, nil
}

func (c *tomlConfig) write(items map[string]interface{}) error {
func (c *tomlConfig) write(items map[string]any) error {
b, err := toml.Marshal(items)
if err != nil {
return err
}
err = os.WriteFile(c.file, b, 0o644)
err = os.WriteFile(c.file, b, 0o600)
if err != nil {
return err
}
return nil
}

func (c *tomlConfig) Get(key string, value interface{}) error {
func (c *tomlConfig) Get(key string, value any) error {
items, err := c.read()
if err != nil {
return err
Expand All @@ -70,7 +70,7 @@ func (c *tomlConfig) Get(key string, value interface{}) error {
return nil
}

func (c *tomlConfig) Set(key string, value interface{}) error {
func (c *tomlConfig) Set(key string, value any) error {
items, err := c.read()
if err != nil {
return err
Expand Down Expand Up @@ -116,7 +116,7 @@ func newTomlConfig(path string) *tomlConfig {
log.Debug().Err(err).Msgf("Failed to create new config directory %s.", path)
} else {
log.Debug().Msgf("Created new config directory %s.", path)
i := make(map[string]interface{})
i := make(map[string]any)
i[PrefAppRegistered] = false
err := c.write(i)
if err != nil {
Expand All @@ -132,7 +132,7 @@ func LoadTOMLConfig(name string) *tomlConfig {
if name != "" {
path = filepath.Join(configBasePath, name)
} else {
path = filepath.Join(configBasePath)
path = configBasePath
}
return newTomlConfig(path)
}
4 changes: 1 addition & 3 deletions pkg/config/viper/viperConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const (
PrefAppRegistered = "app.registered"
)

var (
configBasePath = filepath.Join(os.Getenv("HOME"), ".config", "go-hass-anything")
)
var configBasePath = filepath.Join(os.Getenv("HOME"), ".config", "go-hass-anything")

type ViperConfig struct {
store *viper.Viper
Expand Down

0 comments on commit 0cab594

Please sign in to comment.