From 0ebaa68e489568c6c836342a2740f43cd389a401 Mon Sep 17 00:00:00 2001 From: Heathcliff Date: Mon, 23 Dec 2024 15:58:35 +0100 Subject: [PATCH] Add new fleetctl binary Signed-off-by: Heathcliff --- cmd/fleetctl/main.go | 7 +++++++ pkg/fleetctl/lock.go | 33 ++++++++++++++++++++++++++++++++ pkg/fleetctl/release.go | 33 ++++++++++++++++++++++++++++++++ pkg/fleetctl/root.go | 40 +++++++++++++++++++++++++++++++++++++++ pkg/fleetctl/root_test.go | 13 +++++++++++++ pkg/fleetctl/utils.go | 34 +++++++++++++++++++++++++++++++++ 6 files changed, 160 insertions(+) create mode 100644 cmd/fleetctl/main.go create mode 100644 pkg/fleetctl/lock.go create mode 100644 pkg/fleetctl/release.go create mode 100644 pkg/fleetctl/root.go create mode 100644 pkg/fleetctl/root_test.go create mode 100644 pkg/fleetctl/utils.go diff --git a/cmd/fleetctl/main.go b/cmd/fleetctl/main.go new file mode 100644 index 0000000..89a295d --- /dev/null +++ b/cmd/fleetctl/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/heathcliff26/fleetlock/pkg/fleetctl" + +func main() { + fleetctl.Execute() +} diff --git a/pkg/fleetctl/lock.go b/pkg/fleetctl/lock.go new file mode 100644 index 0000000..80a3ff8 --- /dev/null +++ b/pkg/fleetctl/lock.go @@ -0,0 +1,33 @@ +package fleetctl + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// Create a new lock command +func NewLockCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "lock", + Short: "lock the slot in the server", + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := getClientFromCMD(cmd, args) + if err != nil { + return err + } + + err = client.Lock() + if err != nil { + exitError(cmd, err) + } + + fmt.Println("Success") + return nil + }, + } + addCommonFlagsToCMD(cmd) + + return cmd +} diff --git a/pkg/fleetctl/release.go b/pkg/fleetctl/release.go new file mode 100644 index 0000000..aeb8df1 --- /dev/null +++ b/pkg/fleetctl/release.go @@ -0,0 +1,33 @@ +package fleetctl + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// Create a new lock command +func NewReleaseCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "release", + Short: "release the slot in the server", + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + RunE: func(cmd *cobra.Command, args []string) error { + client, err := getClientFromCMD(cmd, args) + if err != nil { + return err + } + + err = client.Release() + if err != nil { + exitError(cmd, err) + } + + fmt.Println("Success") + return nil + }, + } + addCommonFlagsToCMD(cmd) + + return cmd +} diff --git a/pkg/fleetctl/root.go b/pkg/fleetctl/root.go new file mode 100644 index 0000000..bcbe8f7 --- /dev/null +++ b/pkg/fleetctl/root.go @@ -0,0 +1,40 @@ +package fleetctl + +import ( + "github.com/heathcliff26/fleetlock/pkg/version" + "github.com/spf13/cobra" +) + +const Name = "fleetctl" + +func NewRootCommand() *cobra.Command { + cobra.AddTemplateFunc( + "ProgramName", func() string { + return Name + }, + ) + + rootCmd := &cobra.Command{ + Use: Name, + Short: Name + " assists with debugging or manually controlling a fleetlock server", + RunE: func(cmd *cobra.Command, _ []string) error { + return cmd.Help() + }, + } + + rootCmd.AddCommand( + NewLockCommand(), + NewReleaseCommand(), + version.NewCommand(Name), + ) + + return rootCmd +} + +func Execute() { + cmd := NewRootCommand() + err := cmd.Execute() + if err != nil { + exitError(cmd, err) + } +} diff --git a/pkg/fleetctl/root_test.go b/pkg/fleetctl/root_test.go new file mode 100644 index 0000000..b855703 --- /dev/null +++ b/pkg/fleetctl/root_test.go @@ -0,0 +1,13 @@ +package fleetctl + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewRootCommand(t *testing.T) { + cmd := NewRootCommand() + + assert.Equal(t, Name, cmd.Use) +} diff --git a/pkg/fleetctl/utils.go b/pkg/fleetctl/utils.go new file mode 100644 index 0000000..d32c239 --- /dev/null +++ b/pkg/fleetctl/utils.go @@ -0,0 +1,34 @@ +package fleetctl + +import ( + "fmt" + "os" + + "github.com/heathcliff26/fleetlock/pkg/server/client" + "github.com/spf13/cobra" +) + +const ( + flagNameGroup = "group" +) + +func addCommonFlagsToCMD(cmd *cobra.Command) { + cmd.Flags().StringP(flagNameGroup, "g", "default", "Name of the lock group") +} + +func getClientFromCMD(cmd *cobra.Command, args []string) (*client.FleetlockClient, error) { + group, err := cmd.Flags().GetString(flagNameGroup) + if err != nil { + return nil, err + } + if len(args) < 1 { + return nil, fmt.Errorf("missing url") + } + return client.NewClient(args[0], group) +} + +// Print the error information on stderr and exit with code 1 +func exitError(cmd *cobra.Command, err error) { + fmt.Fprintln(cmd.Root().ErrOrStderr(), "Fatal: "+err.Error()) + os.Exit(1) +}