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

Add cleanup command #64

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

package cmd

import (
"fmt"
"os"

"github.com/spf13/afero"
"github.com/spf13/cobra"
)

func CleanupCmd(fs afero.Fs) *cobra.Command {
cleanupCmd := &cobra.Command{
Use: "cleanup",
Long: "Delete all fragments",
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
path, err := cmd.Flags().GetString("path")
Copy link
Member

Choose a reason for hiding this comment

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

if err != nil {
return fmt.Errorf("error parsing flag 'path': %w", err)
}

err = fs.RemoveAll(path)
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering if this implementation (where the entire folder is removed and recreated) is too greedy and can produce unexpected results. I'm thinking about the case in which users adds different files (non-YAML) in the fragments folder that get deleted by this implementation.

What do you think?

if err != nil {
return fmt.Errorf("error deleting fragments: %w", err)
}

err = fs.Mkdir(path, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating fragments folder: %w", err)
}

return nil
},
}

cleanupCmd.Flags().String("path", "changelog/fragments", "The folder where fragments are stored.")
Copy link
Member

Choose a reason for hiding this comment

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

Remove this flag, as is possible to override this value overriding fragment_path configuration setting.


return cleanupCmd
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {
rootCmd.AddCommand(cmd.NewCmd())
rootCmd.AddCommand(cmd.PrHasFragmentCommand(appFs))
rootCmd.AddCommand(cmd.VersionCmd())
rootCmd.AddCommand(cmd.CleanupCmd(appFs))

err := rootCmd.Execute()
if err != nil {
Expand Down