diff --git a/hosts/falcon.json b/hosts/falcon.json index 4166702..8b58611 100644 --- a/hosts/falcon.json +++ b/hosts/falcon.json @@ -193,5 +193,11 @@ "user-dirs.dirs", "user-dirs.locale", "zk" - ] + ], + "neovim": { + "manage": true, + "gitRepoURL": "https://codeflow.dananglin.me.uk/linux-home/nvim.d.git", + "gitRef": "746f07efdb203a46a7a02ada987cd97b68fa92d6", + "gitRepoPath": "nvim" + } } diff --git a/magefiles/neovim.go b/magefiles/neovim.go index 45442a0..b16314d 100644 --- a/magefiles/neovim.go +++ b/magefiles/neovim.go @@ -5,7 +5,9 @@ package main import ( "fmt" "os" - "path/filepath" + "slices" + + "github.com/magefile/mage/sh" ) // Neovim downloads and manages neovim configuration from a remote git repository. @@ -22,30 +24,28 @@ func Neovim() error { return nil } - homeConfigDirectory, err := os.UserConfigDir() - if err != nil { - return fmt.Errorf( - "unable to get the user's home configuration directory: %w", - err, - ) - } + //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") + // neovimManagedDir = filepath.Join(rootManagedDir, "nvim") + // versionLabelFile = filepath.Join(neovimManagedDir, ".managed.version") + // neovimConfigDir = filepath.Join(homeConfigDirectory, "nvim") ) - tempLocalRepo, err := cloneNvimConfigRepo(config.Neovim., config.Neovim.) + tempLocalRepo, err := cloneNvimConfigRepo(config.Neovim.GitRepoURL, config.Neovim.GitRef) + if err != nil { + return fmt.Errorf("unable to clone the git repository: %w", err) + } - defer func() { - if err := os.Remove(tempLocalRepo); err != nil { - return fmt.Errorf( - "received an error while trying to remove %s: %w", - err, - ) - } - }() + fmt.Println("Git repository cloned to:", tempLocalRepo) + + // defer os.Remove(tempLocalRepo) // TODO: copy the files from temp folder to managed folder @@ -57,12 +57,25 @@ func Neovim() error { } func cloneNvimConfigRepo(repoURL, repoRef string) (string, error) { - tempDir, err := os.MkdirTemp("/tmp", "neovim-config-") + cloneDir, 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 + git := sh.RunCmd("git", "-C", cloneDir) - return tempDir, nil + commands := [][]string{ + {"init"}, + {"remote", "add", "origin", repoURL}, + {"fetch", "origin", repoRef}, + {"checkout", "FETCH_HEAD"}, + } + + for _, command := range slices.All(commands) { + if err := git(command...); err != nil { + return "", err + } + } + + return cloneDir, nil }