Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Adding command checks for 'account' and 'cluster' commands #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/uhc/account/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ limitations under the License.
package account

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/openshift-online/uhc-cli/cmd/uhc/account/orgs"
"github.com/openshift-online/uhc-cli/cmd/uhc/account/quota"
"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 ...
Expand All @@ -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)
}
}
19 changes: 19 additions & 0 deletions cmd/uhc/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
}
45 changes: 45 additions & 0 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
@@ -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
}