44 "fmt"
55 "os"
66 "reflect"
7+ "regexp"
78 "strings"
89
910 trace "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/trace"
@@ -76,6 +77,7 @@ type SoftlayerPlugin struct {
7677}
7778
7879func (sl * SoftlayerPlugin ) Run (context plugin.PluginContext , args []string ) {
80+
7981 trace .Logger = trace .NewLogger (context .Trace ())
8082 terminal .UserAskedForColors = context .ColorEnabled ()
8183 terminal .InitColorSupport ()
@@ -93,19 +95,51 @@ func (sl *SoftlayerPlugin) Run(context plugin.PluginContext, args []string) {
9395 cobraCommand .SetArgs (args )
9496 cobraErr := cobraCommand .Execute ()
9597 if cobraErr != nil {
98+ cobraErrorString := fmt .Sprintf ("%v" , cobraErr )
9699 // Since we surpress the help message on errors, lets show the help message if the error is 'unknown flag'
97- helpTextTriggers := []string {"unknown flag" , T ("Incorrect Usage: " ), T ("Invalid input for" )}
100+ helpTextTriggers := []string {
101+ "unknown flag" ,
102+ "unknown command" ,
103+ "unknown shorthand flag" ,
104+ T ("Incorrect Usage: " ),
105+ T ("Invalid input for" )}
98106 for _ , trigger := range helpTextTriggers {
99- if strings .Contains (fmt . Sprintf ( "%v" , cobraErr ) , trigger ) {
107+ if strings .Contains (cobraErrorString , trigger ) {
100108 realCommand , _ , _ := cobraCommand .Find (args )
101109 _ = realCommand .Help ()
102110 }
103111 }
112+ sl .ui .Failed (terminal .FailureColor (TranslateError (cobraErrorString )))
104113 os .Exit (1 )
105114 }
106115
107116}
108117
118+ // This function helps to translate errors coming from Cobra, the common ones in any case.
119+ func TranslateError (errorMessage string ) string {
120+ if strings .HasPrefix (errorMessage , "unknown command" ) {
121+ r , _ := regexp .Compile (`unknown command "(\w+)"` )
122+ matches := r .FindStringSubmatch (errorMessage )
123+ fmt .Println (matches )
124+ subs := map [string ]interface {}{"CMD" : matches [1 ]}
125+ return T ("Unknown Command '{{.CMD}}'" ,subs )
126+ } else if strings .HasPrefix (errorMessage , "unknown flag" ) {
127+ r , _ := regexp .Compile (`unknown flag: (\S+)` )
128+ matches := r .FindStringSubmatch (errorMessage )
129+ fmt .Println (matches )
130+ subs := map [string ]interface {}{"CMD" : matches [1 ]}
131+ return T ("Unknown Flag '{{.CMD}}'" , subs )
132+ } else if strings .HasPrefix (errorMessage , "unknown shorthand flag" ) {
133+ r , _ := regexp .Compile (`unknown shorthand flag: '(\S+)'` )
134+ matches := r .FindStringSubmatch (errorMessage )
135+ fmt .Println (matches )
136+ subs := map [string ]interface {}{"CMD" : matches [1 ]}
137+ return T ("Unknown Flag '{{.CMD}}'" , subs )
138+ } else {
139+ return T (errorMessage )
140+ }
141+ }
142+
109143func Namespaces () []plugin.Namespace {
110144 return []plugin.Namespace {
111145 metadata .SoftlayerNamespace (),
@@ -218,11 +252,12 @@ func GetTopCobraCommand(ui terminal.UI, session *session.Session) *cobra.Command
218252 slCommand := metadata .NewSoftlayerCommand (ui , session )
219253 helpFlag := false
220254 cobraCmd := & cobra.Command {
221- Use : "sl" ,
222- Short : T ("Manage Classic infrastructure services" ),
223- Long : T ("Manage Classic infrastructure services" ),
224- RunE : nil ,
225- SilenceUsage : true , // Surpresses help text on errors
255+ Use : "sl" ,
256+ Short : T ("Manage Classic infrastructure services" ),
257+ Long : T ("Manage Classic infrastructure services" ),
258+ RunE : nil ,
259+ SilenceUsage : true , // Surpresses help text on errors
260+ SilenceErrors : true ,
226261 }
227262
228263 versionCommand := & cobra.Command {
0 commit comments