-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Heathcliff <[email protected]>
- Loading branch information
1 parent
16375c4
commit 0ebaa68
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |