Skip to content

Commit

Permalink
Start moving flag config into the config module
Browse files Browse the repository at this point in the history
  • Loading branch information
morestatic committed Jul 7, 2022
1 parent 0790277 commit 1d8a290
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 263 deletions.
4 changes: 2 additions & 2 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
addClientsPaginationFlags(clientsListCmd)
addClientsSearchFlag(clientsListCmd)
clientsCmd.AddCommand(clientsListCmd)
clientCmd.Flags().StringP(controllers.ClientNameFlag, "n", "", "Get client by name")
clientCmd.Flags().StringP(config.ClientNameFlag, "n", "", "Get client by name")
clientCmd.Flags().BoolP("all", "a", false, "Show client info with additional details")
clientsCmd.AddCommand(clientCmd)
rootCmd.AddCommand(clientsCmd)
Expand Down Expand Up @@ -72,7 +72,7 @@ var clientCmd = &cobra.Command{
var clientName string
var clientID string
if len(args) == 0 {
cn, err := cmd.Flags().GetString(controllers.ClientNameFlag)
cn, err := cmd.Flags().GetString(config.ClientNameFlag)
if err != nil {
return err
}
Expand Down
95 changes: 1 addition & 94 deletions cmd/command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"strconv"

options "github.com/breathbath/go_utils/v2/pkg/config"

"github.com/cloudradar-monitoring/rportcli/internal/pkg/controllers"
Expand Down Expand Up @@ -68,96 +66,5 @@ func makeWsCommandURLProvider(params *options.ParameterBag) (wsURLBuilder utils.
}

func getCommandRequirements() []config.ParameterRequirement {
return []config.ParameterRequirement{
config.GetNoPromptFlagSpec(),
config.GetReadYAMLFlagSpec(),
{
Field: controllers.ClientIDs,
Help: "Enter comma separated client IDs",
Validate: config.RequiredValidate,
Description: "[required] Comma separated client ids for which the command should be executed. " +
"Alternatively use -n to execute a command by client name(s), or use --search flag.",
ShortName: "d",
IsEnabled: func(providedParams *options.ParameterBag) bool {
return providedParams.ReadString(controllers.ClientNameFlag, "") == "" && providedParams.ReadString(controllers.SearchFlag, "") == ""
},
},
{
Field: controllers.ClientNameFlag,
Description: "Comma separated client names for which the command should be executed",
ShortName: "n",
},
{
Field: controllers.SearchFlag,
Description: "Search clients on all fields, supports wildcards (*).",
},
{
Field: controllers.Command,
Help: "Enter command",
Validate: config.RequiredValidate,
Description: "[required] Command which should be executed on the clients",
ShortName: "c",
IsRequired: true,
},
{
Field: controllers.Timeout,
Help: "Enter timeout in seconds",
Description: "timeout in seconds that was used to observe the command execution",
Default: strconv.Itoa(controllers.DefaultCmdTimeoutSeconds),
ShortName: "t",
},
{
Field: controllers.GroupIDs,
Help: "Enter comma separated group IDs",
Description: "Comma separated client group IDs",
ShortName: "g",
},
{
Field: controllers.ExecConcurrently,
Help: "execute the command concurrently on multiple clients",
Description: "execute the command concurrently on multiple clients",
ShortName: "r",
Type: config.BoolRequirementType,
Default: false,
},
{
Field: controllers.IsFullOutput,
Help: "output detailed information of a job execution",
Description: "output detailed information of a job execution",
ShortName: "f",
Type: config.BoolRequirementType,
Default: false,
},
{
Field: controllers.IsSudo,
Help: "should execute command as sudo",
Description: "execute command as sudo",
ShortName: "u",
Type: config.BoolRequirementType,
Default: false,
},
{
Field: controllers.Interpreter,
Help: "enter interpreter/shell name for the command execution",
Description: "interpreter/shell name for the command execution",
ShortName: "i",
Type: config.StringRequirementType,
},
{
Field: controllers.AbortOnError,
Description: "if true and command fails on one client, it's not executed on others",
Help: "should abort command if it fails on any client",
ShortName: "a",
Type: config.BoolRequirementType,
Default: false,
},
{
Field: controllers.Cwd,
Help: "enter current working directory",
Description: "current working directory",
ShortName: "w",
Type: config.StringRequirementType,
Default: "",
},
}
return config.GetCommandFlagSpecs()
}
88 changes: 1 addition & 87 deletions cmd/script.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"strconv"

options "github.com/breathbath/go_utils/v2/pkg/config"

"github.com/cloudradar-monitoring/rportcli/internal/pkg/controllers"
Expand Down Expand Up @@ -68,89 +66,5 @@ func makeWsScriptsURLProvider(params *options.ParameterBag) (wsURLBuilder utils.
}

func getScriptRequirements() []config.ParameterRequirement {
return []config.ParameterRequirement{
config.GetNoPromptFlagSpec(),
config.GetReadYAMLFlagSpec(),
{
Field: controllers.ClientIDs,
Help: "Enter comma separated client IDs",
Validate: config.RequiredValidate,
Description: "[required] Comma separated client ids on which the script should be executed. " +
"Alternatively use -n to execute a script by client name(s), or use --search flag.",
ShortName: "d",
IsEnabled: func(providedParams *options.ParameterBag) bool {
return providedParams.ReadString(controllers.ClientNameFlag, "") == "" && providedParams.ReadString(controllers.SearchFlag, "") == ""
},
IsRequired: true,
},
{
Field: controllers.ClientNameFlag,
Description: "Comma separated client names on which the script should be executed",
ShortName: "n",
},
{
Field: controllers.SearchFlag,
Description: "Search clients on all fields, supports wildcards (*).",
},
{
Field: controllers.Script,
Help: "Enter script path",
Validate: config.RequiredValidate,
Description: "[required] Path to the script file",
ShortName: "s",
IsRequired: true,
},
{
Field: controllers.Timeout,
Help: "Enter timeout in seconds",
Description: "timeout in seconds that was used to observe the script execution",
Default: strconv.Itoa(controllers.DefaultCmdTimeoutSeconds),
ShortName: "t",
},
{
Field: controllers.GroupIDs,
Help: "Enter comma separated group IDs",
Description: "Comma separated client group IDs",
ShortName: "g",
},
{
Field: controllers.ExecConcurrently,
Help: "execute the script concurrently on multiple clients",
Description: "execute the script concurrently on multiple clients",
ShortName: "r",
Type: config.BoolRequirementType,
Default: false,
},
{
Field: controllers.IsFullOutput,
Help: "output detailed information of a script execution",
Description: "output detailed information of a script execution",
ShortName: "f",
Type: config.BoolRequirementType,
Default: false,
},
{
Field: controllers.IsSudo,
Help: "execute script as sudo",
Description: "execute script as sudo",
ShortName: "u",
Type: config.BoolRequirementType,
Default: false,
},
{
Field: controllers.Interpreter,
Help: "enter interpreter/shell name for the script execution",
Description: "interpreter/shell name for the script execution",
ShortName: "i",
Type: config.StringRequirementType,
},
{
Field: controllers.Cwd,
Help: "enter current working directory",
Description: "current working directory",
ShortName: "w",
Type: config.StringRequirementType,
Default: "",
},
}
return config.GetScriptFlagSpecs()
}
10 changes: 5 additions & 5 deletions cmd/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func init() {
addClientsPaginationFlags(tunnelListCmd)
addClientsSearchFlag(tunnelListCmd)
tunnelListCmd.Flags().StringP(controllers.ClientNameFlag, "n", "", "Get tunnels of a client by name")
tunnelListCmd.Flags().StringP(config.ClientNameFlag, "n", "", "Get tunnels of a client by name")
tunnelListCmd.Flags().StringP(controllers.ClientID, "c", "", "Get tunnels of a client by client id")
tunnelsCmd.AddCommand(tunnelListCmd)

Expand Down Expand Up @@ -164,12 +164,12 @@ func getCreateTunnelRequirements() []config.ParameterRequirement {
ShortName: "c",
IsRequired: true,
IsEnabled: func(providedParams *options.ParameterBag) bool {
return providedParams.ReadString(controllers.ClientNameFlag, "") == ""
return providedParams.ReadString(config.ClientNameFlag, "") == ""
},
Help: "Enter a client ID",
},
{
Field: controllers.ClientNameFlag,
Field: config.ClientNameFlag,
Description: `client name, if no client id is provided`,
ShortName: "n",
},
Expand Down Expand Up @@ -266,12 +266,12 @@ func getDeleteTunnelRequirements() []config.ParameterRequirement {
ShortName: "c",
IsRequired: true,
IsEnabled: func(providedParams *options.ParameterBag) bool {
return providedParams.ReadString(controllers.ClientNameFlag, "") == ""
return providedParams.ReadString(config.ClientNameFlag, "") == ""
},
Help: "Enter a client id",
},
{
Field: controllers.ClientNameFlag,
Field: config.ClientNameFlag,
Description: `client name, if no client id is provided`,
ShortName: "n",
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func newWsClient(ctx context.Context, urlBuilder utils.WsURLBuilder) (wsc *utils
func newExecutionHelper(params *options.ParameterBag,
wsc *utils.WsClient,
rportAPI *api.Rport) (helper *controllers.ExecutionHelper) {
isFullJobOutput := params.ReadBool(controllers.IsFullOutput, false)
isFullJobOutput := params.ReadBool(config.IsFullOutput, false)
helper = &controllers.ExecutionHelper{
ReadWriter: wsc,
JobRenderer: &output.JobRenderer{
Expand Down
Loading

0 comments on commit 1d8a290

Please sign in to comment.