dotfiles/config.go

126 lines
2.7 KiB
Go
Raw Normal View History

2021-01-12 03:53:51 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)
const (
configDirPath = "config"
configFile = "config.json"
)
2021-01-12 03:53:51 +00:00
type Config struct {
2021-01-18 04:28:06 +00:00
// Read from file
2021-01-12 03:53:51 +00:00
Apps map[string]App `json:"apps"`
}
type App struct {
Files []Path `json:"files"`
Dirs []Path `json:"dirs"`
2021-01-18 04:28:06 +00:00
}
type Path struct {
Internal string `json:"internal"`
External string `json:"external"`
2021-01-12 03:53:51 +00:00
}
// Load config from file
2021-01-18 04:28:06 +00:00
func LoadConfig(path string) (result Config, err error) {
2021-01-12 03:53:51 +00:00
configPath := getConfigPath(path)
bytes, err := os.ReadFile(configPath)
2021-01-12 03:53:51 +00:00
if err != nil {
if errors.Is(err, os.ErrNotExist) {
2021-01-18 04:28:06 +00:00
err = fmt.Errorf("file not exist %s: %w", configPath, err)
return
2021-01-12 03:53:51 +00:00
}
err = fmt.Errorf("failed to read file%s: %w", configPath, err)
2021-01-18 04:28:06 +00:00
return
2021-01-12 03:53:51 +00:00
}
2021-01-18 08:37:40 +00:00
if err = json.Unmarshal(bytes, &result); err != nil {
2021-01-18 04:28:06 +00:00
err = fmt.Errorf("failed to unmarshal: %w", err)
return
2021-01-12 03:53:51 +00:00
}
2021-01-18 04:28:06 +00:00
return
2021-01-12 03:53:51 +00:00
}
// internal -> external
func (c *Config) Install() error {
for _, app := range c.Apps {
for _, file := range app.Files {
if err := replaceFile(file.Internal, file.External); err != nil {
2021-01-18 09:50:36 +00:00
return fmt.Errorf("failed to remove and copy from %s to %s: %w", file.Internal, file.External, err)
}
}
for _, dir := range app.Dirs {
if err := replaceDir(dir.Internal, dir.External); err != nil {
2021-01-18 09:50:36 +00:00
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.Internal, dir.External, err)
}
}
}
return nil
}
// external -> internal
func (c *Config) Update() error {
2021-01-18 08:32:25 +00:00
for _, app := range c.Apps {
for _, file := range app.Files {
if err := replaceFile(file.External, file.Internal); err != nil {
2021-01-18 09:50:36 +00:00
return fmt.Errorf("failed to remove and copy from %s to %s: %w", file.External, file.Internal, err)
2021-01-18 08:32:25 +00:00
}
}
for _, dir := range app.Dirs {
if err := replaceDir(dir.External, dir.Internal); err != nil {
2021-01-18 09:50:36 +00:00
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.External, dir.Internal, err)
2021-01-18 08:32:25 +00:00
}
}
}
return nil
}
2021-01-19 11:06:13 +00:00
func (c *Config) Clean() error {
files, err := os.ReadDir(configDirPath)
if err != nil {
return fmt.Errorf("failed to read dir %s: %w", configDirPath, err)
}
// get all dirs inside config dir
unusedDirs := make(map[string]struct{})
for _, file := range files {
if file.Name() == configFile {
continue
}
unusedDirs[file.Name()] = struct{}{}
}
// removed used dirs
for name := range c.Apps {
delete(unusedDirs, name)
}
// delete ununsed dirs to save some space
for dir := range unusedDirs {
dirPath := filepath.Join(configDirPath, dir)
if err := os.RemoveAll(dirPath); err != nil {
return fmt.Errorf("failed to remove %s: %w", dir, err)
}
}
2021-01-19 11:06:13 +00:00
return nil
}
2021-01-12 03:53:51 +00:00
func getConfigPath(path string) string {
return filepath.Join(path, configDirPath, configFile)
2021-01-12 03:53:51 +00:00
}