fleet/cmd/fleetctl/hosts.go
Tomas Touceda 484c6153e3
Issue 1359 fleetctl team transfer (#1413)
* wip

* Add delete user command and translator

* Add host transfer command

* Add changes file

* Undo bad refactor

* Fix copypaste error

* Implement with interfaces instead of assertions

* Ad documentation and simplify implementation further

* Update docs/1-Using-Fleet/3-REST-API.md

Co-authored-by: Zach Wasserman <zach@fleetdm.com>

Co-authored-by: Zach Wasserman <zach@fleetdm.com>
2021-07-21 14:03:10 -03:00

83 lines
2.0 KiB
Go

package main
import (
"github.com/pkg/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",
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)
},
}
}