|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/falcosecurity/falcoctl/cmd/internal/validate" |
| 6 | + "github.com/falcosecurity/falcoctl/pkg/repo" |
| 7 | + "github.com/go-playground/validator/v10" |
| 8 | + "github.com/mitchellh/go-homedir" |
| 9 | + logger "github.com/sirupsen/logrus" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "text/tabwriter" |
| 14 | +) |
| 15 | + |
| 16 | +// Defaults |
| 17 | +const () |
| 18 | + |
| 19 | +var _ CommandOptions = &ListRepoOptions{} |
| 20 | + |
| 21 | +// ListRepoOption represents the `list repo` command options |
| 22 | +type ListRepoOptions struct { |
| 23 | +} |
| 24 | + |
| 25 | +// AddFlags adds flag to c |
| 26 | +func (o *ListRepoOptions) AddFlags(c *cobra.Command) { |
| 27 | +} |
| 28 | + |
| 29 | +// Validate validates the `list repo` command options |
| 30 | +func (o *ListRepoOptions) Validate(c *cobra.Command, args []string) error { |
| 31 | + if err := validate.V.Struct(o); err != nil { |
| 32 | + return err.(validator.ValidationErrors) |
| 33 | + } |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +// NewRepoAddOptions instantiates the `list repo` command options |
| 38 | +func NewListRepoOptions() *ListRepoOptions { |
| 39 | + return &ListRepoOptions{} |
| 40 | +} |
| 41 | + |
| 42 | +func NewListRepoCmd(options CommandOptions) *cobra.Command { |
| 43 | + o := options.(*ListOptions) |
| 44 | + |
| 45 | + cmd := &cobra.Command{ |
| 46 | + Use: "repo", |
| 47 | + DisableFlagsInUseLine: true, |
| 48 | + Short: "Print artifact repositories managed by falcoctl", |
| 49 | + Long: "Print artifact repositories managed by falcoctl", |
| 50 | + PreRunE: o.Validate, |
| 51 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 52 | + if len(args) != 0 { |
| 53 | + return fmt.Errorf("expected 0 arguments") |
| 54 | + } |
| 55 | + home, err := homedir.Dir() |
| 56 | + if err != nil { |
| 57 | + logger.WithError(err).Fatal("error getting the home directory") |
| 58 | + } |
| 59 | + // HOME/.falcoctl/sources.yaml |
| 60 | + path := filepath.Join(home, o.RepoPath, o.RepoFile) |
| 61 | + r, err := repo.LoadRepos(path) |
| 62 | + if err != nil { |
| 63 | + if os.IsNotExist(err) { |
| 64 | + r = &repo.RepoList{} |
| 65 | + } else { |
| 66 | + |
| 67 | + logger.Fatal(err.Error()) |
| 68 | + return err |
| 69 | + } |
| 70 | + } |
| 71 | + w := new(tabwriter.Writer) |
| 72 | + w.Init(os.Stdout, 8, 8, 0, '\t', 0) |
| 73 | + defer w.Flush() |
| 74 | + fmt.Fprintf(w, "\n %s\t%s\t%s\t", "NAMEs", "URL", "UPDATED") |
| 75 | + for _, k := range r.Sources { |
| 76 | + fmt.Fprintf(w, "\n %s\t%s\t%s\t", k.Name, k.Url, k.Date) |
| 77 | + } |
| 78 | + fmt.Fprintf(w, "\n\n") |
| 79 | + return nil |
| 80 | + }, |
| 81 | + } |
| 82 | + o.ListRepoOptions.AddFlags(cmd) |
| 83 | + return cmd |
| 84 | +} |
0 commit comments