Files
matchbox/matchbox/cli/group_create.go
Dalton Hubble e8b6a33bca Fix notable Go standard library warnings and deprecations
* Replace ioutil methods with equivalent io and os methods
* Remove unneccesary types from slice literals
* Update the minimum Go version to v1.18
2022-09-27 21:57:10 -07:00

58 lines
1.2 KiB
Go

package cli
import (
"context"
"os"
"github.com/spf13/cobra"
pb "github.com/poseidon/matchbox/matchbox/server/serverpb"
"github.com/poseidon/matchbox/matchbox/storage/storagepb"
)
// groupPutCmd creates and updates Groups.
var (
groupPutCmd = &cobra.Command{
Use: "create --file FILENAME",
Short: "Create a machine group",
Long: `Create a machine group`,
Run: runGroupPutCmd,
}
)
func init() {
groupCmd.AddCommand(groupPutCmd)
groupPutCmd.Flags().StringVarP(&flagFilename, "filename", "f", "", "filename to use to create a Group")
groupPutCmd.MarkFlagRequired("filename")
groupPutCmd.MarkFlagFilename("filename", "json")
}
func runGroupPutCmd(cmd *cobra.Command, args []string) {
if len(flagFilename) == 0 {
cmd.Help()
return
}
if err := validateArgs(cmd, args); err != nil {
return
}
client := mustClientFromCmd(cmd)
group, err := loadGroup(flagFilename)
if err != nil {
exitWithError(ExitError, err)
}
req := &pb.GroupPutRequest{Group: group}
_, err = client.Groups.GroupPut(context.TODO(), req)
if err != nil {
exitWithError(ExitError, err)
}
}
func loadGroup(filename string) (*storagepb.Group, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return storagepb.ParseGroup(data)
}