2024-09-14 05:58:43 +01:00
|
|
|
package config
|
2024-09-12 16:35:06 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
const configDir string = "hosts"
|
|
|
|
|
|
|
|
type Config struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
ManagedConfigurations []string `json:"managedConfigurations"`
|
2024-09-14 05:58:43 +01:00
|
|
|
BashProfile ConfigBashProfile `json:"bashProfile"`
|
|
|
|
Directories ConfigDirectories `json:"directories"`
|
|
|
|
Git ConfigGit `json:"git"`
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
type ConfigDirectories struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
UseDefaultDirectories bool `json:"useDefaultDirectories"`
|
|
|
|
IncludeXDGDirectories bool `json:"includeXDGDirectories"`
|
|
|
|
AdditionalDirectories []string `json:"additionalDirectories"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
type ConfigGit struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
GpgSign bool `json:"gpgSign"`
|
2024-09-14 05:58:43 +01:00
|
|
|
User ConfigGitUser `json:"user"`
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
type ConfigGitUser struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
Email string `json:"email"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SigningKey string `json:"signingKey"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
type ConfigBashProfile struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
Manage bool `json:"manage"`
|
|
|
|
Filename string `json:"filename"`
|
2024-09-14 05:58:43 +01:00
|
|
|
SessionPaths []ConfigBashProfileSessionPath `json:"sessionPaths"`
|
2024-09-12 16:35:06 +01:00
|
|
|
XdgDirectories map[string]string `json:"xdgDirectories"`
|
|
|
|
EnvironmentVariables map[string]string `json:"environmentVariables"`
|
|
|
|
Aliases map[string]string `json:"aliases"`
|
2024-09-14 05:58:43 +01:00
|
|
|
Commands []ConfigBashProfileCommand `json:"commands"`
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
type ConfigBashProfileSessionPath struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
Path string `json:"path"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
type ConfigBashProfileCommand struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
Command string `json:"command"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
func NewConfig() (Config, error) {
|
2024-09-12 16:35:06 +01:00
|
|
|
cfg := defaultConfig()
|
|
|
|
|
|
|
|
path, err := configFilePath()
|
|
|
|
if err != nil {
|
2024-09-14 05:58:43 +01:00
|
|
|
return Config{}, fmt.Errorf("unable to calculate the config file path: %w", err)
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2024-09-14 05:58:43 +01:00
|
|
|
return Config{}, fmt.Errorf("unable to open the file: %w", err)
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err = json.NewDecoder(file).Decode(&cfg); err != nil {
|
2024-09-14 05:58:43 +01:00
|
|
|
return Config{}, fmt.Errorf("unable to decode the JSON file: %w", err)
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func configFilePath() (string, error) {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to get the machine's hostname: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostnameParts := strings.SplitN(hostname, "-", 3)
|
|
|
|
|
|
|
|
if len(hostnameParts) != 3 {
|
|
|
|
return "", fmt.Errorf("unexpected hostname format")
|
|
|
|
}
|
|
|
|
|
|
|
|
identifier := hostnameParts[1]
|
|
|
|
|
|
|
|
return filepath.Join(configDir, identifier+".json"), nil
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
func defaultConfig() Config {
|
|
|
|
return Config{
|
|
|
|
Directories: ConfigDirectories{
|
2024-09-12 16:35:06 +01:00
|
|
|
UseDefaultDirectories: true,
|
|
|
|
IncludeXDGDirectories: true,
|
|
|
|
AdditionalDirectories: []string{},
|
|
|
|
},
|
2024-09-14 05:58:43 +01:00
|
|
|
Git: ConfigGit{
|
2024-09-12 16:35:06 +01:00
|
|
|
GpgSign: false,
|
2024-09-14 05:58:43 +01:00
|
|
|
User: ConfigGitUser{
|
2024-09-12 16:35:06 +01:00
|
|
|
Email: "",
|
|
|
|
Name: "",
|
|
|
|
SigningKey: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ManagedConfigurations: []string{},
|
|
|
|
}
|
|
|
|
}
|