Skip to content

Commit

Permalink
uninstall: add -f option
Browse files Browse the repository at this point in the history
  • Loading branch information
lufia committed Mar 4, 2022
1 parent 50ffe11 commit ae06aee
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ dotsync changes

$ dotsync pull [-n]

$ dotsync uninstall path
$ dotsync uninstall [-f] path

$ dotsync export
```
Expand All @@ -23,7 +23,7 @@ $ dotsync export

`dotsync install` links *source* to *dest* to manage changes. When *-f* option is passed, it overwrites *dest* even if file is already exists.

`dotsync uninstall` unlink and remove *path*.
`dotsync uninstall` unlink and remove *path*. When *-f* option is passed, it removes *path* even if file is locally modified.

`dotsync changes` prints locally changed filenames.

Expand Down
5 changes: 5 additions & 0 deletions testdata/uninstall/discard.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
uninstall -f ~/out/.exrc
-- out/.exrc --
set ts=8
-- .local/state/dotsync/store/.exrc --
7970153163bfaaba0780d262461fb7b692380f6d249da3aef109f93ba2a7d643 644 $TEST_DIR/out/.exrc
9 changes: 9 additions & 0 deletions testdata/uninstall/modified.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
uninstall ~/out/.exrc // /\.exrc: locally modified; will not remove
-- out/.exrc --
set ts=8
-- out/.exrc.golden --
set ts=8
-- .local/state/dotsync/store/.exrc --
7970153163bfaaba0780d262461fb7b692380f6d249da3aef109f93ba2a7d643 644 $TEST_DIR/out/.exrc
-- .local/state/dotsync/store/.exrc.golden --
7970153163bfaaba0780d262461fb7b692380f6d249da3aef109f93ba2a7d643 644 $TEST_DIR/out/.exrc
8 changes: 4 additions & 4 deletions uninstall.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package main

import (
"fmt"
"io"
"log"
"os"
"path/filepath"
)

func runUninstall(r *Repository, args []string, w io.Writer) error {
f := NewFlagSet("uninstall", "path")
force := f.Bool("f", false, "discard locally changes and remove")

if err := f.Parse(args); err != nil {
return err
Expand Down Expand Up @@ -46,9 +47,8 @@ func runUninstall(r *Repository, args []string, w io.Writer) error {
if err != nil {
return err
}
if !ok || h != state.Hash {
log.Printf("%s: locally modified; will not remove", state.Target)
return &alreadyShownError{err: err}
if !*force && (!ok || h != state.Hash) {
return fmt.Errorf("%s: locally modified; will not remove", state.Target)
}
if err := remove(p); err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ func TestRunUninstall(t *testing.T) {
"~/.local/state/dotsync/store/.exrc",
},
},
{
script: "testdata/uninstall/modified.script",
label: "occurs an error if file is modified",
},
{
script: "testdata/uninstall/discard.script",
label: "remove a target file and its state if modified",
files: []string{
"~/out/.exrc",
"~/.local/state/dotsync/store/.exrc",
},
},
}
for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
Expand Down

0 comments on commit ae06aee

Please sign in to comment.