fleet/cmd/fleetctl/hosts.go
jacobshandling ec11e3d1d0
fleetctl, API, copy updates around host identifiers (#20220)
## Addresses #19127 
![Screenshot 2024-07-08 at 4 49
33 PM](https://github.com/fleetdm/fleet/assets/61553566/b4704eb9-9707-4cbf-8959-ec67dde57103)
- Also replace all ocurrences of "comma separated" with
"comma-separated"

- [x] Changes file added for user-visible changes in `changes/`
- [x] `SELECT *` is avoided, SQL injection is prevented (using
placeholders for values in statements)
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2024-07-09 10:25:01 -07:00

83 lines
2 KiB
Go

package main
import (
"errors"
"github.com/urfave/cli/v2"
)
const (
hostsFlagName = "hosts"
labelFlagName = "label"
statusFlagName = "status"
searchQueryFlagName = "search_query"
)
func hostsCommand() *cli.Command {
return &cli.Command{
Name: "hosts",
Usage: "Manage Fleet hosts",
Subcommands: []*cli.Command{
transferCommand(),
},
}
}
func transferCommand() *cli.Command {
return &cli.Command{
Name: "transfer",
Usage: "Transfer one or more hosts to a team",
UsageText: `This command will gather the set of hosts specified and transfer them to the team.`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: teamFlagName,
Usage: "Team name hosts will be transferred to. Use '' for No team",
Required: true,
},
&cli.StringSliceFlag{
Name: hostsFlagName,
Usage: "Comma-separated hostnames to transfer",
},
&cli.StringFlag{
Name: labelFlagName,
Usage: "Label name to transfer",
},
&cli.StringFlag{
Name: statusFlagName,
Usage: "Status to use when filtering hosts",
},
&cli.StringFlag{
Name: searchQueryFlagName,
Usage: "A search query that returns matching hostnames to be transferred",
},
configFlag(),
contextFlag(),
yamlFlag(),
debugFlag(),
},
Action: func(c *cli.Context) error {
client, err := clientFromCLI(c)
if err != nil {
return err
}
team := c.String(teamFlagName)
hosts := c.StringSlice(hostsFlagName)
label := c.String(labelFlagName)
status := c.String(statusFlagName)
searchQuery := c.String(searchQueryFlagName)
if hosts != nil {
if label != "" || searchQuery != "" || status != "" {
return errors.New("--hosts cannot be used along side any other flag")
}
} else {
if label == "" && searchQuery == "" && status == "" {
return errors.New("You need to define either --hosts, or one or more of --label, --status, --search_query")
}
}
return client.TransferHosts(hosts, label, status, searchQuery, team)
},
}
}