From 51835c4d1cb315e39ead456932cb07c03c279f7d Mon Sep 17 00:00:00 2001 From: LucianPy Date: Thu, 7 Jul 2022 17:44:35 +0300 Subject: [PATCH 1/2] add cleanup command --- cmd/cleanup.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 46 insertions(+) create mode 100644 cmd/cleanup.go diff --git a/cmd/cleanup.go b/cmd/cleanup.go new file mode 100644 index 0000000..a7e63b0 --- /dev/null +++ b/cmd/cleanup.go @@ -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") + if err != nil { + return fmt.Errorf("error parsing flag 'path': %w", err) + } + + err = fs.RemoveAll(path) + 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.") + + return cleanupCmd +} diff --git a/main.go b/main.go index 6af9313..a3d5562 100644 --- a/main.go +++ b/main.go @@ -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 { From 92d3b9b73ce957c11d4c18f17203a77b6e764d07 Mon Sep 17 00:00:00 2001 From: LucianPy Date: Fri, 15 Jul 2022 17:11:49 +0300 Subject: [PATCH 2/2] refactor cleanup cmd --- cmd/cleanup.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/cmd/cleanup.go b/cmd/cleanup.go index a7e63b0..90b2635 100644 --- a/cmd/cleanup.go +++ b/cmd/cleanup.go @@ -6,10 +6,12 @@ package cmd import ( "fmt" - "os" + "io/ioutil" + "path/filepath" "github.com/spf13/afero" "github.com/spf13/cobra" + "github.com/spf13/viper" ) func CleanupCmd(fs afero.Fs) *cobra.Command { @@ -20,26 +22,27 @@ func CleanupCmd(fs afero.Fs) *cobra.Command { return nil }, RunE: func(cmd *cobra.Command, args []string) error { - path, err := cmd.Flags().GetString("path") - if err != nil { - return fmt.Errorf("error parsing flag 'path': %w", err) - } + fragmentLocation := viper.GetString("fragment_location") - err = fs.RemoveAll(path) + fragments, err := ioutil.ReadDir(fragmentLocation) if err != nil { - return fmt.Errorf("error deleting fragments: %w", err) + return fmt.Errorf("could not get fragments folder: %w", err) } - err = fs.Mkdir(path, os.ModePerm) - if err != nil { - return fmt.Errorf("error creating 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 }, } - cleanupCmd.Flags().String("path", "changelog/fragments", "The folder where fragments are stored.") - return cleanupCmd }