75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package settings
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
DefaultConfigDir = "/etc/ClusterCanvas"
|
|
ConfigDirEnvVar = "CLUSTERCANVAS_CONFIG_DIR"
|
|
ConfigKeyEnvVar = "CLUSTERCANVAS_CONFIG_KEY"
|
|
|
|
SettingsFileName = "settings.json"
|
|
SecretsFileName = "secrets.enc"
|
|
PasswordsFileName = "passwords.enc"
|
|
SessionsFileName = "sessions.enc"
|
|
NodesFileName = "nodes.json"
|
|
NodeKeysFileName = "node-keys.enc"
|
|
NodeAuditFileName = "node-audit.json"
|
|
AuthAuditFileName = "auth-audit.json"
|
|
ActionsFileName = "actions.json"
|
|
NodeActionGroupsFileName = "node-action-groups.json"
|
|
NodeActionWidgetsFileName = "node-action-widgets.json"
|
|
ActionLogsDirName = "action-logs"
|
|
)
|
|
|
|
// ResolveDir returns the config directory using precedence:
|
|
// 1. flagValue (-configdir), if non-empty
|
|
// 2. CLUSTERCANVAS_CONFIG_DIR, if set
|
|
// 3. /etc/ClusterCanvas
|
|
//
|
|
// Leading ~/ is expanded to the user home directory.
|
|
func ResolveDir(flagValue string) (string, error) {
|
|
candidate := strings.TrimSpace(flagValue)
|
|
if candidate == "" {
|
|
candidate = strings.TrimSpace(os.Getenv(ConfigDirEnvVar))
|
|
}
|
|
if candidate == "" {
|
|
candidate = DefaultConfigDir
|
|
}
|
|
|
|
expanded, err := expandHome(candidate)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Clean(expanded), nil
|
|
}
|
|
|
|
func expandHome(path string) (string, error) {
|
|
if path == "~" {
|
|
homeDirectory, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", fmt.Errorf("expand home directory: %w", err)
|
|
}
|
|
return homeDirectory, nil
|
|
}
|
|
if strings.HasPrefix(path, "~/") {
|
|
homeDirectory, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", fmt.Errorf("expand home directory: %w", err)
|
|
}
|
|
return filepath.Join(homeDirectory, path[2:]), nil
|
|
}
|
|
return path, nil
|
|
}
|
|
|
|
func ensureConfigDir(dir string) error {
|
|
if err := os.MkdirAll(dir, 0o750); err != nil {
|
|
return fmt.Errorf("create config dir %q: %w", dir, err)
|
|
}
|
|
return nil
|
|
}
|