checkpoint: began to write the neovim target

This commit is contained in:
Dan Anglin 2024-09-13 12:50:43 +01:00
parent a909803b29
commit e98c422970
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 101 additions and 25 deletions

View file

@ -1,4 +1,6 @@
package config //go:build mage
package main
import ( import (
"encoding/json" "encoding/json"
@ -8,68 +10,74 @@ import (
"strings" "strings"
) )
const configDir string = "hosts" type config struct {
type Config struct {
ManagedConfigurations []string `json:"managedConfigurations"` ManagedConfigurations []string `json:"managedConfigurations"`
BashProfile ConfigBashProfile `json:"bashProfile"` BashProfile configBashProfile `json:"bashProfile"`
Directories ConfigDirectories `json:"directories"` Directories configDirectories `json:"directories"`
Git ConfigGit `json:"git"` Git configGit `json:"git"`
Neovim configNeovim `json:"neovim"`
} }
type ConfigDirectories struct { type configDirectories struct {
UseDefaultDirectories bool `json:"useDefaultDirectories"` UseDefaultDirectories bool `json:"useDefaultDirectories"`
IncludeXDGDirectories bool `json:"includeXDGDirectories"` IncludeXDGDirectories bool `json:"includeXDGDirectories"`
AdditionalDirectories []string `json:"additionalDirectories"` AdditionalDirectories []string `json:"additionalDirectories"`
} }
type ConfigGit struct { type configGit struct {
GpgSign bool `json:"gpgSign"` GpgSign bool `json:"gpgSign"`
User ConfigGitUser `json:"user"` User configGitUser `json:"user"`
} }
type ConfigGitUser struct { type configGitUser struct {
Email string `json:"email"` Email string `json:"email"`
Name string `json:"name"` Name string `json:"name"`
SigningKey string `json:"signingKey"` SigningKey string `json:"signingKey"`
} }
type ConfigBashProfile struct { type configBashProfile struct {
Manage bool `json:"manage"` Manage bool `json:"manage"`
Filename string `json:"filename"` Filename string `json:"filename"`
SessionPaths []ConfigBashProfileSessionPath `json:"sessionPaths"` SessionPaths []configBashProfileSessionPath `json:"sessionPaths"`
XdgDirectories map[string]string `json:"xdgDirectories"` XdgDirectories map[string]string `json:"xdgDirectories"`
EnvironmentVariables map[string]string `json:"environmentVariables"` EnvironmentVariables map[string]string `json:"environmentVariables"`
Aliases map[string]string `json:"aliases"` Aliases map[string]string `json:"aliases"`
Commands []ConfigBashProfileCommand `json:"commands"` Commands []configBashProfileCommand `json:"commands"`
} }
type ConfigBashProfileSessionPath struct { type configBashProfileSessionPath struct {
Path string `json:"path"` Path string `json:"path"`
Description string `json:"description"` Description string `json:"description"`
} }
type ConfigBashProfileCommand struct { type configBashProfileCommand struct {
Command string `json:"command"` Command string `json:"command"`
Description string `json:"description"` Description string `json:"description"`
} }
func NewConfig() (Config, error) { type configNeovim struct {
Manage bool `json:"manage"`
GitRepoURL string `json:"gitRepoURL"`
GitRef string `json:"gitRef"`
GitRepoPath string `json:"gitRepoPath"`
}
func newConfig() (config, error) {
cfg := defaultConfig() cfg := defaultConfig()
path, err := configFilePath() path, err := configFilePath()
if err != nil { if err != nil {
return Config{}, fmt.Errorf("unable to calculate the config file path: %w", err) return config{}, fmt.Errorf("unable to calculate the config file path: %w", err)
} }
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
return Config{}, fmt.Errorf("unable to open the file: %w", err) return config{}, fmt.Errorf("unable to open the file: %w", err)
} }
defer file.Close() defer file.Close()
if err = json.NewDecoder(file).Decode(&cfg); err != nil { if err = json.NewDecoder(file).Decode(&cfg); err != nil {
return Config{}, fmt.Errorf("unable to decode the JSON file: %w", err) return config{}, fmt.Errorf("unable to decode the JSON file: %w", err)
} }
return cfg, nil return cfg, nil
@ -92,16 +100,16 @@ func configFilePath() (string, error) {
return filepath.Join(configDir, identifier+".json"), nil return filepath.Join(configDir, identifier+".json"), nil
} }
func defaultConfig() Config { func defaultConfig() config {
return Config{ return config{
Directories: ConfigDirectories{ Directories: configDirectories{
UseDefaultDirectories: true, UseDefaultDirectories: true,
IncludeXDGDirectories: true, IncludeXDGDirectories: true,
AdditionalDirectories: []string{}, AdditionalDirectories: []string{},
}, },
Git: ConfigGit{ Git: configGit{
GpgSign: false, GpgSign: false,
User: ConfigGitUser{ User: configGitUser{
Email: "", Email: "",
Name: "", Name: "",
SigningKey: "", SigningKey: "",

68
magefiles/neovim.go Normal file
View file

@ -0,0 +1,68 @@
//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
)
// Neovim downloads and manages neovim configuration from a remote git repository.
func Neovim() error {
config, err := newConfig()
if err != nil {
return fmt.Errorf(
"unable to load the configuration: %w",
err,
)
}
if !config.Neovim.Manage {
return nil
}
homeConfigDirectory, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf(
"unable to get the user's home configuration directory: %w",
err,
)
}
var (
neovimManagedDir = filepath.Join(rootManagedDir, "nvim")
versionLabelFile = filepath.Join(neovimManagedDir, ".managed.version")
neovimConfigDir = filepath.Join(homeConfigDirectory, "nvim")
)
tempLocalRepo, err := cloneNvimConfigRepo(config.Neovim., config.Neovim.)
defer func() {
if err := os.Remove(tempLocalRepo); err != nil {
return fmt.Errorf(
"received an error while trying to remove %s: %w",
err,
)
}
}()
// TODO: copy the files from temp folder to managed folder
// TODO: add commit/tag ref to .managed.version
// TODO: symlink all inside managed neovim folder
return nil
}
func cloneNvimConfigRepo(repoURL, repoRef string) (string, error) {
tempDir, err := os.MkdirTemp("/tmp", "neovim-config-")
if err != nil {
return "", fmt.Errorf("unable to create the temporary directory: %w", err)
}
// TODO: clone the repo to the tempDir and return tempDir
return tempDir, nil
}