2024-09-09 18:02:53 +01:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-09-10 08:41:31 +01:00
|
|
|
"strings"
|
2024-09-09 18:02:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2024-09-10 08:41:31 +01:00
|
|
|
Directories configDirectories `json:"directories"`
|
|
|
|
Git configGit `json:"git"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type configDirectories struct {
|
|
|
|
UseDefaultDirectories bool `json:"useDefaultDirectories"`
|
|
|
|
IncludeXDGDirectories bool `json:"includeXDGDirectories"`
|
|
|
|
AdditionalDirectories []string `json:"additionalDirectories"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type configGit struct {
|
|
|
|
GpgSign bool `json:"gpgSign"`
|
|
|
|
User configGitUser `json:"user"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type configGitUser struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
User string `json:"user"`
|
|
|
|
SigningKey string `json:"signingKey"`
|
2024-09-09 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newConfig() (config, error) {
|
2024-09-10 08:41:31 +01:00
|
|
|
cfg := defaultConfig()
|
2024-09-09 18:02:53 +01:00
|
|
|
|
2024-09-10 08:41:31 +01:00
|
|
|
path, err := configFilePath()
|
|
|
|
if err != nil {
|
|
|
|
return config{}, fmt.Errorf("unable to calculate the config file path: %w", err)
|
|
|
|
}
|
2024-09-09 18:02:53 +01:00
|
|
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2024-09-10 08:41:31 +01:00
|
|
|
return config{}, fmt.Errorf("unable to open the file: %w", err)
|
2024-09-09 18:02:53 +01:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err = json.NewDecoder(file).Decode(&cfg); err != nil {
|
2024-09-10 08:41:31 +01:00
|
|
|
return config{}, fmt.Errorf("unable to decode the JSON file: %w", err)
|
2024-09-09 18:02:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2024-09-10 08:41:31 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultConfig() config {
|
|
|
|
return config{
|
|
|
|
Directories: configDirectories{
|
|
|
|
UseDefaultDirectories: true,
|
|
|
|
IncludeXDGDirectories: true,
|
|
|
|
AdditionalDirectories: []string{},
|
|
|
|
},
|
|
|
|
Git: configGit{
|
|
|
|
GpgSign: false,
|
|
|
|
User: configGitUser{
|
|
|
|
Email: "",
|
|
|
|
User: "",
|
|
|
|
SigningKey: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|