checkpoint: began to write the neovim target
This commit is contained in:
parent
a909803b29
commit
e98c422970
2 changed files with 101 additions and 25 deletions
|
@ -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: "",
|
||||
|
|
68
magefiles/neovim.go
Normal file
68
magefiles/neovim.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue