@@ -29,24 +29,33 @@ import (
29
29
"github.com/falcosecurity/falcoctl/pkg/options"
30
30
)
31
31
32
- type indexAddOptions struct {
32
+ // IndexAddOptions contains the options for the index add command.
33
+ type IndexAddOptions struct {
33
34
* options.CommonOptions
34
35
}
35
36
36
- func (o * indexAddOptions ) Validate (args []string ) error {
37
+ // Validate is used to make sure that required directories are existing in the filesystem.
38
+ func (o * IndexAddOptions ) Validate (args []string ) error {
37
39
// TODO(loresuso): we should move this logic elsewhere
38
40
if _ , err := os .Stat (config .FalcoctlPath ); os .IsNotExist (err ) {
39
41
err = os .Mkdir (config .FalcoctlPath , 0o700 )
40
42
if err != nil {
41
43
return err
42
44
}
43
45
}
46
+
47
+ if _ , err := os .Stat (config .IndexesDir ); os .IsNotExist (err ) {
48
+ err = os .Mkdir (config .IndexesDir , 0o700 )
49
+ if err != nil {
50
+ return err
51
+ }
52
+ }
44
53
return nil
45
54
}
46
55
47
56
// NewIndexAddCmd returns the index add command.
48
57
func NewIndexAddCmd (ctx context.Context , opt * options.CommonOptions ) * cobra.Command {
49
- o := indexAddOptions {
58
+ o := IndexAddOptions {
50
59
CommonOptions : opt ,
51
60
}
52
61
@@ -67,12 +76,12 @@ func NewIndexAddCmd(ctx context.Context, opt *options.CommonOptions) *cobra.Comm
67
76
return cmd
68
77
}
69
78
70
- func (o * indexAddOptions ) RunIndexAdd (ctx context.Context , args []string ) error {
79
+ // RunIndexAdd implements the index add command.
80
+ func (o * IndexAddOptions ) RunIndexAdd (ctx context.Context , args []string ) error {
71
81
name := args [0 ]
72
82
nameYaml := fmt .Sprintf ("%s%s" , name , ".yaml" )
73
83
url := args [1 ]
74
-
75
- indexFile := filepath .Join (config .FalcoctlPath , nameYaml )
84
+ indexFile := filepath .Join (config .IndexesDir , nameYaml )
76
85
77
86
indexConfig , err := index .NewConfig (config .IndexesFile )
78
87
if err != nil {
@@ -110,5 +119,26 @@ func (o *indexAddOptions) RunIndexAdd(ctx context.Context, args []string) error
110
119
return err
111
120
}
112
121
122
+ currentIndexes , err := config .Indexes ()
123
+ if err != nil {
124
+ return fmt .Errorf ("unable to get indexes from viper: %w" , err )
125
+ }
126
+
127
+ for _ , i := range currentIndexes {
128
+ if i .Name == name {
129
+ o .Printer .Verbosef ("index with name %q already exists in the config file %q" , name , config .ConfigPath )
130
+ return nil
131
+ }
132
+ }
133
+
134
+ currentIndexes = append (currentIndexes , config.Index {
135
+ Name : name ,
136
+ URL : url ,
137
+ })
138
+
139
+ if err := config .UpdateConfigFile (config .IndexesKey , currentIndexes , o .ConfigFile ); err != nil {
140
+ return fmt .Errorf ("unable to update indexes list in the config file %q: %w" , config .ConfigPath , err )
141
+ }
142
+
113
143
return nil
114
144
}
0 commit comments