Allow add to merge into existing package
This commit is contained in:
@@ -11,7 +11,7 @@ go run . <command>
|
||||
## Commands
|
||||
|
||||
- `sigil apply` - apply symlinks from `~/.dotfiles`
|
||||
- `sigil add <path>` - add an existing file or folder
|
||||
- `sigil add <path>` - add an existing file or folder (merges into existing package for single files)
|
||||
|
||||
## Repo layout
|
||||
|
||||
|
||||
14
main.go
14
main.go
@@ -247,8 +247,9 @@ func addCmd(args []string) error {
|
||||
}
|
||||
|
||||
pkgDir := filepath.Join(repo, pkgName)
|
||||
pkgExists := false
|
||||
if _, err := os.Stat(pkgDir); err == nil {
|
||||
return fmt.Errorf("package %q already exists", pkgName)
|
||||
pkgExists = true
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
@@ -259,11 +260,15 @@ func addCmd(args []string) error {
|
||||
}
|
||||
|
||||
configPath := filepath.Join(pkgDir, configFileName)
|
||||
if !pkgExists {
|
||||
if err := writeConfig(configPath, targetRoot); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
if info.IsDir() {
|
||||
if pkgExists {
|
||||
return errors.New("cannot merge a directory into an existing package yet")
|
||||
}
|
||||
if err := moveDirContents(absPath, filesDir); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -280,6 +285,11 @@ func addCmd(args []string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(destPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := os.Stat(destPath); err == nil {
|
||||
return fmt.Errorf("destination already exists: %s", destPath)
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(absPath, destPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user