fleet/cmd/fleetctl/delete.go
guangwu 33858d7301
chore: remove refs to deprecated io/ioutil (#14485)
# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [ ] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [ ] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)
- [ ] Documented any permissions changes (docs/Using
Fleet/manage-access.md)
- [ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)
- [ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.
- [ ] Added/updated tests
- [ ] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [ ] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
2023-10-27 15:28:54 -03:00

95 lines
2.1 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"github.com/fleetdm/fleet/v4/pkg/spec"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/service"
"github.com/urfave/cli/v2"
)
func deleteCommand() *cli.Command {
var flFilename string
return &cli.Command{
Name: "delete",
Usage: "Specify files to declaratively batch delete osquery configurations",
UsageText: `fleetctl delete [options]`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "f",
EnvVars: []string{"FILENAME"},
Value: "",
Destination: &flFilename,
Usage: "A file to apply",
},
configFlag(),
contextFlag(),
debugFlag(),
},
Action: func(c *cli.Context) error {
if flFilename == "" {
return errors.New("-f must be specified")
}
b, err := os.ReadFile(flFilename)
if err != nil {
return err
}
fleet, err := clientFromCLI(c)
if err != nil {
return err
}
specs, err := spec.GroupFromBytes(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 {
root := ctxerr.Cause(err)
switch root.(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 {
root := ctxerr.Cause(err)
switch root.(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 {
root := ctxerr.Cause(err)
switch root.(type) {
case service.NotFoundErr:
fmt.Printf("[!] label %q doesn't exist\n", label.Name)
continue
}
return err
}
}
return nil
},
}
}