enbas/internal/config/directory.go
Dan Anglin 421c21bf52
feat: allow users to specify the config directory
Allow users to specify their preferred configuration directory by using
the top level flag (a.k.a 'global flag') --config-dir. This also
introduces the design of baking top level flags into the executor.
2024-05-22 23:30:09 +01:00

37 lines
731 B
Go

package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"codeflow.dananglin.me.uk/apollo/enbas/internal"
)
func calculateConfigDir(configDir string) string {
if configDir != "" {
return configDir
}
rootDir, err := os.UserConfigDir()
if err != nil {
rootDir = "."
}
return filepath.Join(rootDir, internal.ApplicationName)
}
func ensureConfigDir(configDir string) error {
if _, err := os.Stat(configDir); err != nil {
if errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(configDir, 0o750); err != nil {
return fmt.Errorf("unable to create %s; %w", configDir, err)
}
} else {
return fmt.Errorf("unknown error received when running stat on %s; %w", configDir, err)
}
}
return nil
}