Skip to content

Commit

Permalink
Add new fleetctl binary
Browse files Browse the repository at this point in the history
Signed-off-by: Heathcliff <[email protected]>
  • Loading branch information
heathcliff26 committed Dec 23, 2024
1 parent 16375c4 commit 0ebaa68
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/fleetctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/heathcliff26/fleetlock/pkg/fleetctl"

func main() {
fleetctl.Execute()
}
33 changes: 33 additions & 0 deletions pkg/fleetctl/lock.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions pkg/fleetctl/release.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions pkg/fleetctl/root.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
13 changes: 13 additions & 0 deletions pkg/fleetctl/root_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
34 changes: 34 additions & 0 deletions pkg/fleetctl/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 0ebaa68

Please sign in to comment.