feat: treat files, dirs in config as the same (#8)
* feat: use paths for bpth files and dirs * refactor: rename file to path when config install/update Co-authored-by: Tran Hau <ngtranhau@gmail.com>main
parent
992bba6640
commit
d655736596
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"apps": {
|
"apps": {
|
||||||
"nvim": {
|
"nvim": {
|
||||||
"files": [
|
"paths": [
|
||||||
{
|
{
|
||||||
"internal": "config/nvim/init.vim",
|
"internal": "config/nvim/init.vim",
|
||||||
"external": "~/.config/nvim/init.vim"
|
"external": "~/.config/nvim/init.vim"
|
||||||
|
|
|
@ -27,8 +27,7 @@ type configApps struct {
|
||||||
|
|
||||||
// Read from file
|
// Read from file
|
||||||
type App struct {
|
type App struct {
|
||||||
Files []Path `json:"files"`
|
Paths []Path `json:"paths"`
|
||||||
Dirs []Path `json:"dirs"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Path struct {
|
type Path struct {
|
||||||
|
@ -64,18 +63,12 @@ func LoadConfig(path string) (*config, *configDemo, error) {
|
||||||
return &cfg, &cfgDemo, nil
|
return &cfg, &cfgDemo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal -> external
|
// Install replace src internal dst external
|
||||||
func (c *config) Install() error {
|
func (c *config) Install() error {
|
||||||
for _, app := range c.Apps {
|
for _, app := range c.Apps {
|
||||||
for _, file := range app.Files {
|
for _, path := range app.Paths {
|
||||||
if err := copy.Replace(file.Internal, file.External); err != nil {
|
if err := copy.Replace(path.Internal, path.External); err != nil {
|
||||||
return fmt.Errorf("failed to remove and copy from %s to %s: %w", file.Internal, file.External, err)
|
return fmt.Errorf("failed to replace src %s dst %s: %w", path.Internal, path.External, err)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dir := range app.Dirs {
|
|
||||||
if err := copy.Replace(dir.Internal, dir.External); err != nil {
|
|
||||||
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.Internal, dir.External, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,18 +76,12 @@ func (c *config) Install() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// external -> internal
|
// Update replace src external dst internal
|
||||||
func (c *config) Update() error {
|
func (c *config) Update() error {
|
||||||
for _, app := range c.Apps {
|
for _, app := range c.Apps {
|
||||||
for _, file := range app.Files {
|
for _, path := range app.Paths {
|
||||||
if err := copy.Replace(file.External, file.Internal); err != nil {
|
if err := copy.Replace(path.External, path.Internal); err != nil {
|
||||||
return fmt.Errorf("failed to remove and copy from %s to %s: %w", file.External, file.Internal, err)
|
return fmt.Errorf("failed to replace src %s dst %s: %w", path.External, path.Internal, err)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dir := range app.Dirs {
|
|
||||||
if err := copy.Replace(dir.External, dir.Internal); err != nil {
|
|
||||||
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.External, dir.Internal, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,15 +89,17 @@ func (c *config) Update() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean remove unused config inside config dir
|
||||||
func (c *config) Clean() error {
|
func (c *config) Clean() error {
|
||||||
files, err := os.ReadDir(configDirPath)
|
files, err := os.ReadDir(configDirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read dir %s: %w", configDirPath, err)
|
return fmt.Errorf("failed to read dir %s: %w", configDirPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get all dirs inside config dir
|
// Get all dirs inside config dir
|
||||||
unusedDirs := make(map[string]struct{})
|
unusedDirs := make(map[string]struct{})
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
// Ignore config file
|
||||||
if file.Name() == configFile {
|
if file.Name() == configFile {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -118,12 +107,12 @@ func (c *config) Clean() error {
|
||||||
unusedDirs[file.Name()] = struct{}{}
|
unusedDirs[file.Name()] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// removed used dirs
|
// Removed used dirs
|
||||||
for name := range c.Apps {
|
for name := range c.Apps {
|
||||||
delete(unusedDirs, name)
|
delete(unusedDirs, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete ununsed dirs to save some space
|
// Delete ununsed dirs to save some space
|
||||||
for dir := range unusedDirs {
|
for dir := range unusedDirs {
|
||||||
dirPath := filepath.Join(configDirPath, dir)
|
dirPath := filepath.Join(configDirPath, dir)
|
||||||
if err := os.RemoveAll(dirPath); err != nil {
|
if err := os.RemoveAll(dirPath); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue