From e98c422970edba97c3e6a9b7d60a08d11b60602d Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 13 Sep 2024 12:50:43 +0100 Subject: [PATCH] checkpoint: began to write the neovim target --- magefiles/internal/config/config.go | 58 +++++++++++++----------- magefiles/neovim.go | 68 +++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 25 deletions(-) create mode 100644 magefiles/neovim.go diff --git a/magefiles/internal/config/config.go b/magefiles/internal/config/config.go index d9bb9cb..c3c2eea 100644 --- a/magefiles/internal/config/config.go +++ b/magefiles/internal/config/config.go @@ -1,4 +1,6 @@ -package config +//go:build mage + +package main import ( "encoding/json" @@ -8,68 +10,74 @@ import ( "strings" ) -const configDir string = "hosts" - -type Config struct { +type config struct { ManagedConfigurations []string `json:"managedConfigurations"` - BashProfile ConfigBashProfile `json:"bashProfile"` - Directories ConfigDirectories `json:"directories"` - Git ConfigGit `json:"git"` + BashProfile configBashProfile `json:"bashProfile"` + Directories configDirectories `json:"directories"` + Git configGit `json:"git"` + Neovim configNeovim `json:"neovim"` } -type ConfigDirectories struct { +type configDirectories struct { UseDefaultDirectories bool `json:"useDefaultDirectories"` IncludeXDGDirectories bool `json:"includeXDGDirectories"` AdditionalDirectories []string `json:"additionalDirectories"` } -type ConfigGit struct { +type configGit struct { GpgSign bool `json:"gpgSign"` - User ConfigGitUser `json:"user"` + User configGitUser `json:"user"` } -type ConfigGitUser struct { +type configGitUser struct { Email string `json:"email"` Name string `json:"name"` SigningKey string `json:"signingKey"` } -type ConfigBashProfile struct { +type configBashProfile struct { Manage bool `json:"manage"` Filename string `json:"filename"` - SessionPaths []ConfigBashProfileSessionPath `json:"sessionPaths"` + SessionPaths []configBashProfileSessionPath `json:"sessionPaths"` XdgDirectories map[string]string `json:"xdgDirectories"` EnvironmentVariables map[string]string `json:"environmentVariables"` Aliases map[string]string `json:"aliases"` - Commands []ConfigBashProfileCommand `json:"commands"` + Commands []configBashProfileCommand `json:"commands"` } -type ConfigBashProfileSessionPath struct { +type configBashProfileSessionPath struct { Path string `json:"path"` Description string `json:"description"` } -type ConfigBashProfileCommand struct { +type configBashProfileCommand struct { Command string `json:"command"` 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() path, err := configFilePath() 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) 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() 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 @@ -92,16 +100,16 @@ func configFilePath() (string, error) { return filepath.Join(configDir, identifier+".json"), nil } -func defaultConfig() Config { - return Config{ - Directories: ConfigDirectories{ +func defaultConfig() config { + return config{ + Directories: configDirectories{ UseDefaultDirectories: true, IncludeXDGDirectories: true, AdditionalDirectories: []string{}, }, - Git: ConfigGit{ + Git: configGit{ GpgSign: false, - User: ConfigGitUser{ + User: configGitUser{ Email: "", Name: "", SigningKey: "", diff --git a/magefiles/neovim.go b/magefiles/neovim.go new file mode 100644 index 0000000..45442a0 --- /dev/null +++ b/magefiles/neovim.go @@ -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 +}