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 11:41:58 +01:00
|
|
|
const dir string = "hosts"
|
2024-09-14 05:58:43 +01:00
|
|
|
|
|
|
|
type Config struct {
|
2024-09-14 11:41:58 +01:00
|
|
|
ManagedConfigurations []string `json:"managedConfigurations"`
|
|
|
|
BashProfile BashProfile `json:"bashProfile"`
|
|
|
|
Directories Directories `json:"directories"`
|
|
|
|
Git Git `json:"git"`
|
|
|
|
ExternalConfigurations []ExternalConfig `json:"externalConfigurations"`
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
type Directories struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
UseDefaultDirectories bool `json:"useDefaultDirectories"`
|
|
|
|
IncludeXDGDirectories bool `json:"includeXDGDirectories"`
|
|
|
|
AdditionalDirectories []string `json:"additionalDirectories"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
type Git struct {
|
|
|
|
GpgSign bool `json:"gpgSign"`
|
|
|
|
User GitUser `json:"user"`
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
type GitUser struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
Email string `json:"email"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SigningKey string `json:"signingKey"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
type BashProfile struct {
|
|
|
|
Manage bool `json:"manage"`
|
|
|
|
Filename string `json:"filename"`
|
|
|
|
SessionPaths []BashProfileSessionPath `json:"sessionPaths"`
|
|
|
|
XdgDirectories map[string]string `json:"xdgDirectories"`
|
|
|
|
EnvironmentVariables map[string]string `json:"environmentVariables"`
|
|
|
|
Aliases map[string]string `json:"aliases"`
|
|
|
|
Commands []BashProfileCommand `json:"commands"`
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
type BashProfileSessionPath struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
Path string `json:"path"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
type BashProfileCommand struct {
|
2024-09-12 16:35:06 +01:00
|
|
|
Command string `json:"command"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
type ExternalConfig struct {
|
|
|
|
Label string `json:"label"`
|
|
|
|
GitRepoURL string `json:"gitRepoURL"`
|
|
|
|
GitRef string `json:"gitRef"`
|
|
|
|
GitRepoPath string `json:"gitRepoPath"`
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
|
2024-09-14 11:41:58 +01:00
|
|
|
return filepath.Join(dir, identifier+".json"), nil
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
func defaultConfig() Config {
|
|
|
|
return Config{
|
2024-09-14 11:41:58 +01:00
|
|
|
Directories: Directories{
|
2024-09-12 16:35:06 +01:00
|
|
|
UseDefaultDirectories: true,
|
|
|
|
IncludeXDGDirectories: true,
|
|
|
|
AdditionalDirectories: []string{},
|
|
|
|
},
|
2024-09-14 11:41:58 +01:00
|
|
|
Git: Git{
|
2024-09-12 16:35:06 +01:00
|
|
|
GpgSign: false,
|
2024-09-14 11:41:58 +01:00
|
|
|
User: GitUser{
|
2024-09-12 16:35:06 +01:00
|
|
|
Email: "",
|
|
|
|
Name: "",
|
|
|
|
SigningKey: "",
|
|
|
|
},
|
|
|
|
},
|
2024-09-14 11:41:58 +01:00
|
|
|
ManagedConfigurations: []string{},
|
|
|
|
ExternalConfigurations: []ExternalConfig{},
|
2024-09-12 16:35:06 +01:00
|
|
|
}
|
|
|
|
}
|