Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix force option behavior #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions statik.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func main() {
// rename tries to os.Rename, but fall backs to copying from src
// to dest and unlink the source if os.Rename fails.
func rename(src, dest string) error {
if _, err := os.Stat(dest); !os.IsNotExist(err) && !*flagForce {
return fmt.Errorf("file %q already exists; use -f to overwrite", dest)
}

// Try to rename generated source.
if err := os.Rename(src, dest); err == nil {
return nil
Expand All @@ -91,16 +95,6 @@ func rename(src, dest string) error {
os.Remove(src) // ignore the error, source is in tmp.
}()

if _, err = os.Stat(dest); !os.IsNotExist(err) {
if *flagForce {
if err = os.Remove(dest); err != nil {
return fmt.Errorf("file %q could not be deleted", dest)
}
} else {
return fmt.Errorf("file %q already exists; use -f to overwrite", dest)
}
}

wc, err := os.Create(dest)
if err != nil {
return err
Expand Down