From 38c09eae670575ea7cdbd5b7cb78cb03f1c237ba Mon Sep 17 00:00:00 2001 From: alima Date: Mon, 19 Aug 2019 13:17:06 -0400 Subject: [PATCH] Adding command checks for 'account' and 'cluster' commands --- cmd/uhc/account/cmd.go | 19 ++++++++++++++++++ cmd/uhc/cluster/cmd.go | 19 ++++++++++++++++++ pkg/command/command.go | 45 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 pkg/command/command.go diff --git a/cmd/uhc/account/cmd.go b/cmd/uhc/account/cmd.go index 43b176d..93b6933 100644 --- a/cmd/uhc/account/cmd.go +++ b/cmd/uhc/account/cmd.go @@ -17,6 +17,9 @@ limitations under the License. package account import ( + "fmt" + "os" + "github.com/spf13/cobra" "github.com/openshift-online/uhc-cli/cmd/uhc/account/orgs" @@ -24,6 +27,7 @@ import ( "github.com/openshift-online/uhc-cli/cmd/uhc/account/roles" "github.com/openshift-online/uhc-cli/cmd/uhc/account/status" "github.com/openshift-online/uhc-cli/cmd/uhc/account/users" + c "github.com/openshift-online/uhc-cli/pkg/command" ) // Cmd ... @@ -41,3 +45,18 @@ func init() { Cmd.AddCommand(roles.Cmd) Cmd.AddCommand(users.Cmd) } + +func run(cmd *cobra.Command, argv []string) { + // Check there is at least one argument + if len(argv) < 1 { + fmt.Fprintf(os.Stderr, "Expected at least one argument\n") + os.Exit(1) + } + + // Check argument is a valid command + commandsSlice := cmd.Commands() + if !c.StringInArray(argv[0], c.GetCommandNames(commandsSlice)) { + fmt.Fprintf(os.Stderr, "INVALID COMMAND\n%s", cmd.UsageString()) + os.Exit(1) + } +} diff --git a/cmd/uhc/cluster/cmd.go b/cmd/uhc/cluster/cmd.go index 921d901..337366c 100644 --- a/cmd/uhc/cluster/cmd.go +++ b/cmd/uhc/cluster/cmd.go @@ -17,10 +17,14 @@ limitations under the License. package cluster import ( + "fmt" + "os" + "github.com/openshift-online/uhc-cli/cmd/uhc/cluster/describe" "github.com/openshift-online/uhc-cli/cmd/uhc/cluster/list" "github.com/openshift-online/uhc-cli/cmd/uhc/cluster/login" "github.com/openshift-online/uhc-cli/cmd/uhc/cluster/status" + c "github.com/openshift-online/uhc-cli/pkg/command" "github.com/spf13/cobra" ) @@ -37,3 +41,18 @@ func init() { Cmd.AddCommand(describe.Cmd) Cmd.AddCommand(login.Cmd) } + +func run(cmd *cobra.Command, argv []string) { + // Check there is at least one argument + if len(argv) < 1 { + fmt.Fprintf(os.Stderr, "Expected at least one argument\n") + os.Exit(1) + } + + // Check argument is a valid command + commandsSlice := cmd.Commands() + if !c.StringInArray(argv[0], c.GetCommandNames(commandsSlice)) { + fmt.Fprintf(os.Stderr, "INVALID COMMAND\n%s", cmd.UsageString()) + os.Exit(1) + } +} diff --git a/pkg/command/command.go b/pkg/command/command.go new file mode 100644 index 0000000..060443f --- /dev/null +++ b/pkg/command/command.go @@ -0,0 +1,45 @@ +package command + +import "github.com/spf13/cobra" + +/* +Copyright (c) 2019 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// This file contains the types and functions used to manage the configuration of the command line +// client. + +// StringInArray returns true if it finds the given string in the given array, else false +func StringInArray(str string, arr []string) bool { + + for _, element := range arr { + if str == element { + return true + } + } + + return false +} + +// GetCommandNames takes a list of commands and returns a list of their names +func GetCommandNames(commandList []*cobra.Command) []string { + var commandNames []string + + for _, command := range commandList { + commandNames = append(commandNames, command.Name()) + } + + return commandNames +}