mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
|
||
|
"github.com/kolide/fleet/server/service"
|
||
|
"github.com/pkg/errors"
|
||
|
"github.com/urfave/cli"
|
||
|
)
|
||
|
|
||
|
func deleteCommand() cli.Command {
|
||
|
var (
|
||
|
flFilename string
|
||
|
flDebug bool
|
||
|
)
|
||
|
return cli.Command{
|
||
|
Name: "delete",
|
||
|
Usage: "Specify files to declaratively batch delete osquery configurations",
|
||
|
UsageText: `fleetctl delete [options]`,
|
||
|
Flags: []cli.Flag{
|
||
|
configFlag(),
|
||
|
contextFlag(),
|
||
|
cli.StringFlag{
|
||
|
Name: "f",
|
||
|
EnvVar: "FILENAME",
|
||
|
Value: "",
|
||
|
Destination: &flFilename,
|
||
|
Usage: "A file to apply",
|
||
|
},
|
||
|
cli.BoolFlag{
|
||
|
Name: "debug",
|
||
|
EnvVar: "DEBUG",
|
||
|
Destination: &flDebug,
|
||
|
Usage: "Whether or not to enable debug logging",
|
||
|
},
|
||
|
},
|
||
|
Action: func(c *cli.Context) error {
|
||
|
if flFilename == "" {
|
||
|
return errors.New("-f must be specified")
|
||
|
}
|
||
|
|
||
|
b, err := ioutil.ReadFile(flFilename)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
fleet, err := clientFromCLI(c)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
specs, err := specGroupFromBytes(b)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, query := range specs.Queries {
|
||
|
fmt.Printf("[+] deleting query %q\n", query.Name)
|
||
|
if err := fleet.DeleteQuery(query.Name); err != nil {
|
||
|
switch err.(type) {
|
||
|
case service.NotFoundErr:
|
||
|
fmt.Printf("[!] query %q doesn't exist\n", query.Name)
|
||
|
continue
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for _, pack := range specs.Packs {
|
||
|
fmt.Printf("[+] deleting pack %q\n", pack.Name)
|
||
|
if err := fleet.DeletePack(pack.Name); err != nil {
|
||
|
switch err.(type) {
|
||
|
case service.NotFoundErr:
|
||
|
fmt.Printf("[!] pack %q doesn't exist\n", pack.Name)
|
||
|
continue
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for _, label := range specs.Labels {
|
||
|
fmt.Printf("[+] deleting label %q\n", label.Name)
|
||
|
if err := fleet.DeleteLabel(label.Name); err != nil {
|
||
|
switch err.(type) {
|
||
|
case service.NotFoundErr:
|
||
|
fmt.Printf("[!] label %q doesn't exist\n", label.Name)
|
||
|
continue
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
},
|
||
|
}
|
||
|
}
|