diff --git a/cmd/cleanup.go b/cmd/cleanup.go new file mode 100644 index 0000000..90b2635 --- /dev/null +++ b/cmd/cleanup.go @@ -0,0 +1,48 @@ +// 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" + "io/ioutil" + "path/filepath" + + "github.com/spf13/afero" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +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 { + fragmentLocation := viper.GetString("fragment_location") + + fragments, err := ioutil.ReadDir(fragmentLocation) + if err != nil { + return fmt.Errorf("could not get fragments folder: %w", err) + } + + for _, f := range fragments { + ext := filepath.Ext(f.Name()) + + if ext == ".yaml" || ext == ".yml" { + err = fs.Remove(filepath.Join(fragmentLocation, f.Name())) + if err != nil { + return fmt.Errorf("could not remove fragment: %w", err) + } + } + } + + return nil + }, + } + + return cleanupCmd +} diff --git a/main.go b/main.go index 21c8715..a3d5562 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ func main() { rootCmd.AddCommand(cmd.NewCmd()) rootCmd.AddCommand(cmd.PrHasFragmentCommand(appFs)) rootCmd.AddCommand(cmd.VersionCmd()) - rootCmd.AddCommand(cmd.RenderCmd(appFs)) + rootCmd.AddCommand(cmd.CleanupCmd(appFs)) err := rootCmd.Execute() if err != nil {