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

Introduce --clobber option to save command #79

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 15 additions & 7 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ var (
// savePath is where the output of the command is written to.
savePath string
// overwriteSavePath controls behaviour when the directory indicated by savePath already exists.
// If true, the directory will be replaced. If false, the command will fail.
// If true, the directory will be replaced. If false (and clobberSavePath is true), the command will fail.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If false (and clobberSavePath is true), the command will fail.

I don't think this is actually enforced anywhere.

overwriteSavePath bool

// clobberSavePath also controls behaviour when the directory indicated by savePath already exists.
// If true, any existing contents of the directory will be ignored and go-licenses will provide output with reckless abandon.
clobberSavePath bool
)

func init() {
Expand All @@ -57,6 +61,8 @@ func init() {

saveCmd.Flags().BoolVar(&overwriteSavePath, "force", false, "Delete the destination directory if it already exists.")

saveCmd.Flags().BoolVar(&clobberSavePath, "clobber", false, "Clobber the destination directory if it already exists.")

rootCmd.AddCommand(saveCmd)
}

Expand All @@ -67,13 +73,15 @@ func saveMain(_ *cobra.Command, args []string) error {
}
}

// Check that the save path doesn't exist, otherwise it'd end up with a mix of
// If clobberSavePath is false, check that the save path doesn't exist, otherwise it'd end up with a mix of
// existing files and the output of this command.
if d, err := os.Open(savePath); err == nil {
d.Close()
return fmt.Errorf("%s already exists", savePath)
} else if !os.IsNotExist(err) {
return err
if !clobberSavePath {
if d, err := os.Open(savePath); err == nil {
d.Close()
return fmt.Errorf("%s already exists", savePath)
} else if !os.IsNotExist(err) {
return err
}
}

classifier, err := licenses.NewClassifier(confidenceThreshold)
Expand Down