Skip to content

Commit b59757b

Browse files
andreabonannoloresuso
authored andcommitted
new: adds list repo command
Signed-off-by: Andrea Bonanno <[email protected]> Co-authored-by: Lorenzo Susini <[email protected]> Signed-off-by: Andrea Bonanno <[email protected]>
1 parent 2a38d47 commit b59757b

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

cmd/list.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
//Defaults
6+
const ()
7+
8+
// ListOptions represents the install command options
9+
type ListOptions struct {
10+
*ListRepoOptions
11+
RepoPath string
12+
RepoFile string
13+
}
14+
15+
// Validate validates the `list` command options
16+
func (o *ListOptions) Validate(c *cobra.Command, args []string) error {
17+
//TODO
18+
return nil
19+
}
20+
21+
func (o *ListOptions) AddFlags(c *cobra.Command) {
22+
}
23+
24+
// NewRepoOptions instantiates the `repo` command options
25+
func NewListOptions() CommandOptions {
26+
return &ListOptions{
27+
ListRepoOptions: NewListRepoOptions(),
28+
RepoPath: defaultRepoPath,
29+
RepoFile: defaultRepoFile,
30+
}
31+
}
32+
33+
func NewListCmd(options CommandOptions) *cobra.Command {
34+
o := options.(*ListOptions)
35+
cmd := &cobra.Command{
36+
Use: "list",
37+
DisableFlagsInUseLine: true,
38+
Short: "Print list of resources",
39+
Long: "Print list of resources",
40+
}
41+
42+
cmd.AddCommand(NewListRepoCmd(o))
43+
44+
return cmd
45+
}

cmd/list_repo.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

cmd/root.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
)
1818

1919
const (
20-
configName = "config"
21-
configDir = ".falcoctl"
20+
configName = "config"
21+
configDir = ".falcoctl"
22+
defaultRepoPath = ".falcoctl"
23+
defaultRepoFile = "sources.yaml"
2224
)
2325

2426
func init() {
@@ -76,6 +78,7 @@ func New(configOptions *ConfigOptions) *cobra.Command {
7678
rootCmd.AddCommand(NewInstallCmd(NewInstallOptions()))
7779
rootCmd.AddCommand(NewSearchCmd(NewSearchOptions()))
7880
rootCmd.AddCommand(NewRepoCmd(NewRepoOptions()))
81+
rootCmd.AddCommand(NewListCmd(NewListOptions()))
7982

8083
return rootCmd
8184
}

0 commit comments

Comments
 (0)