Dan Anglin
59a6969c25
Download and install configurations from external sources. At the moment downloading from external git repositories is only supported. Download and install the Neovim configuration for sparrow. Add an external script to install lazy.nvim.
121 lines
3 KiB
Go
121 lines
3 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const dir string = "hosts"
|
|
|
|
type Config struct {
|
|
ManagedConfigurations []string `json:"managedConfigurations"`
|
|
BashProfile BashProfile `json:"bashProfile"`
|
|
Directories Directories `json:"directories"`
|
|
Git Git `json:"git"`
|
|
ExternalConfigurations []ExternalConfig `json:"externalConfigurations"`
|
|
}
|
|
|
|
type Directories struct {
|
|
UseDefaultDirectories bool `json:"useDefaultDirectories"`
|
|
IncludeXDGDirectories bool `json:"includeXDGDirectories"`
|
|
AdditionalDirectories []string `json:"additionalDirectories"`
|
|
}
|
|
|
|
type Git struct {
|
|
GpgSign bool `json:"gpgSign"`
|
|
User GitUser `json:"user"`
|
|
}
|
|
|
|
type GitUser struct {
|
|
Email string `json:"email"`
|
|
Name string `json:"name"`
|
|
SigningKey string `json:"signingKey"`
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
type BashProfileSessionPath struct {
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type BashProfileCommand struct {
|
|
Command string `json:"command"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type ExternalConfig struct {
|
|
Label string `json:"label"`
|
|
GitRepoURL string `json:"gitRepoURL"`
|
|
GitRef string `json:"gitRef"`
|
|
GitRepoPath string `json:"gitRepoPath"`
|
|
}
|
|
|
|
func NewConfig() (Config, error) {
|
|
cfg := defaultConfig()
|
|
|
|
path, err := configFilePath()
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("unable to calculate the config file path: %w", err)
|
|
}
|
|
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("unable to open the file: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
if err = json.NewDecoder(file).Decode(&cfg); err != nil {
|
|
return Config{}, fmt.Errorf("unable to decode the JSON file: %w", err)
|
|
}
|
|
|
|
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(dir, identifier+".json"), nil
|
|
}
|
|
|
|
func defaultConfig() Config {
|
|
return Config{
|
|
Directories: Directories{
|
|
UseDefaultDirectories: true,
|
|
IncludeXDGDirectories: true,
|
|
AdditionalDirectories: []string{},
|
|
},
|
|
Git: Git{
|
|
GpgSign: false,
|
|
User: GitUser{
|
|
Email: "",
|
|
Name: "",
|
|
SigningKey: "",
|
|
},
|
|
},
|
|
ManagedConfigurations: []string{},
|
|
ExternalConfigurations: []ExternalConfig{},
|
|
}
|
|
}
|