Skip to content

Commit

Permalink
Add cleanup command (#64)
Browse files Browse the repository at this point in the history
Add cleanup command which removes all fragments from path.
  • Loading branch information
lucian-ioan committed Jul 18, 2022
1 parent 2022107 commit b9aac9b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b9aac9b

Please sign in to comment.