Skip to content

Commit 70f331d

Browse files
loresusoalacuku
authored andcommitted
update: load config in index related commands
Signed-off-by: Lorenzo Susini <[email protected]> Co-authored-by: Aldo Lacuku <[email protected]>
1 parent 71c678c commit 70f331d

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

internal/index/add/index_add.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,33 @@ import (
2929
"github.com/falcosecurity/falcoctl/pkg/options"
3030
)
3131

32-
type indexAddOptions struct {
32+
// IndexAddOptions contains the options for the index add command.
33+
type IndexAddOptions struct {
3334
*options.CommonOptions
3435
}
3536

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 {
3739
// TODO(loresuso): we should move this logic elsewhere
3840
if _, err := os.Stat(config.FalcoctlPath); os.IsNotExist(err) {
3941
err = os.Mkdir(config.FalcoctlPath, 0o700)
4042
if err != nil {
4143
return err
4244
}
4345
}
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+
}
4453
return nil
4554
}
4655

4756
// NewIndexAddCmd returns the index add command.
4857
func NewIndexAddCmd(ctx context.Context, opt *options.CommonOptions) *cobra.Command {
49-
o := indexAddOptions{
58+
o := IndexAddOptions{
5059
CommonOptions: opt,
5160
}
5261

@@ -67,12 +76,12 @@ func NewIndexAddCmd(ctx context.Context, opt *options.CommonOptions) *cobra.Comm
6776
return cmd
6877
}
6978

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 {
7181
name := args[0]
7282
nameYaml := fmt.Sprintf("%s%s", name, ".yaml")
7383
url := args[1]
74-
75-
indexFile := filepath.Join(config.FalcoctlPath, nameYaml)
84+
indexFile := filepath.Join(config.IndexesDir, nameYaml)
7685

7786
indexConfig, err := index.NewConfig(config.IndexesFile)
7887
if err != nil {
@@ -110,5 +119,26 @@ func (o *indexAddOptions) RunIndexAdd(ctx context.Context, args []string) error
110119
return err
111120
}
112121

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+
113143
return nil
114144
}

internal/index/remove/remove.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,38 @@ func NewIndexRemoveCmd(ctx context.Context, opt *options.CommonOptions) *cobra.C
7474
}
7575

7676
func (o *indexRemoveOptions) RunIndexRemove(ctx context.Context, args []string) error {
77+
currentIndexes, err := config.Indexes()
78+
if err != nil {
79+
return fmt.Errorf("unable to get indexes from viper: %w", err)
80+
}
81+
7782
for _, name := range args {
7883
nameYaml := fmt.Sprintf("%s%s", name, ".yaml")
79-
indexFile := filepath.Join(config.FalcoctlPath, nameYaml)
84+
indexFile := filepath.Join(config.IndexesDir, nameYaml)
8085
if err := o.indexConfig.Remove(name); err != nil {
8186
return err
8287
}
8388

8489
if err := os.Remove(indexFile); err != nil {
8590
return err
8691
}
92+
93+
for i, ind := range currentIndexes {
94+
if ind.Name == name {
95+
o.Printer.Verbosef("index with name %q exists in the config file %q, removing", name, config.ConfigPath)
96+
currentIndexes = append(currentIndexes[:i], currentIndexes[i+1:]...)
97+
break
98+
}
99+
}
87100
}
88101

89102
if err := o.indexConfig.Write(config.IndexesFile); err != nil {
90103
return err
91104
}
92105

106+
if err := config.UpdateConfigFile(config.IndexesKey, currentIndexes, o.ConfigFile); err != nil {
107+
return fmt.Errorf("unable to update indexes list in the config file %q: %w", config.ConfigPath, err)
108+
}
109+
93110
return nil
94111
}

internal/index/update/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (o *indexUpdateOptions) RunIndexUpdate(ctx context.Context, args []string)
7878

7979
for _, name := range args {
8080
nameYaml := fmt.Sprintf("%s%s", name, ".yaml")
81-
indexFile := filepath.Join(config.FalcoctlPath, nameYaml)
81+
indexFile := filepath.Join(config.IndexesDir, nameYaml)
8282

8383
indexConfigEntry, err := o.indexConfig.Get(name)
8484
if err != nil {

0 commit comments

Comments
 (0)