forked from openservicemesh/osm
-
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.
cli: add support command to list error code description
Adds a new command `osm support error-info` that lists the mapping of one or all error codes to their description. Part of openservicemesh#2866 Signed-off-by: Shashank Ram <shashr2204@gmail.com>
- Loading branch information
1 parent
230b321
commit 72ac7e5
Showing
5 changed files
with
185 additions
and
7 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
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,24 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const supportCmdDescription = ` | ||
This command consists of multiple subcommands related supportability and | ||
associated tooling, such as examining error codes. | ||
` | ||
|
||
func newSupportCmd(out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "support", | ||
Short: "supportability tooling", | ||
Long: supportCmdDescription, | ||
Args: cobra.NoArgs, | ||
} | ||
cmd.AddCommand(newSupportErrInfoCmd(out)) | ||
|
||
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,88 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"sort" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/openservicemesh/osm/pkg/errcode" | ||
) | ||
|
||
const errInfoDescription = ` | ||
This command lists the mapping of one or all error codes to their description.` | ||
|
||
const errInfoExample = ` | ||
Get the description for the error code E1000 | ||
# osm support error-info E1000 | ||
Get the description for all error codes | ||
# osm support error-info | ||
` | ||
|
||
type errInfoCmd struct { | ||
out io.Writer | ||
} | ||
|
||
func newSupportErrInfoCmd(out io.Writer) *cobra.Command { | ||
errInfoCmd := &errInfoCmd{ | ||
out: out, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "error-info", | ||
Short: "lists mapping of error code to its description", | ||
Long: errInfoDescription, | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
var errCode string | ||
if len(args) != 0 { | ||
errCode = args[0] | ||
} | ||
return errInfoCmd.run(errCode) | ||
}, | ||
Example: errInfoExample, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (cmd *errInfoCmd) run(errCode string) error { | ||
table := tablewriter.NewWriter(cmd.out) | ||
table.SetHeader([]string{"Error code", "Description"}) | ||
table.SetRowLine(true) | ||
table.SetColWidth(70) | ||
|
||
if errCode != "" { | ||
// Print the error code description mapping only for the given error code | ||
e, err := errcode.FromStr(errCode) | ||
if err != nil { | ||
return errors.Errorf("Error code '%s' is not a valid error code format. Should be of the form Exxxx, ex. E1000.", errCode) | ||
} | ||
description, ok := errcode.ErrCodeMap[e] | ||
if !ok { | ||
return errors.Errorf("Error code '%s' is not a valid error code recognized by OSM", errCode) | ||
} | ||
table.Append([]string{errCode, description}) | ||
} else { | ||
// Print the error code description mapping for all error codes | ||
var sortedErrKeys []errcode.ErrCode | ||
for err := range errcode.ErrCodeMap { | ||
sortedErrKeys = append(sortedErrKeys, err) | ||
} | ||
sort.Slice(sortedErrKeys, func(i, j int) bool { | ||
return sortedErrKeys[i] < sortedErrKeys[j] | ||
}) | ||
|
||
for _, key := range sortedErrKeys { | ||
desc := errcode.ErrCodeMap[key] | ||
table.Append([]string{key.String(), desc}) | ||
} | ||
} | ||
|
||
table.Render() | ||
|
||
return nil | ||
} |
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,51 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
tassert "github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestErrInfoRun tests errInfoCmd.run() | ||
|
||
func TestErrInfoRun(t *testing.T) { | ||
assert := tassert.New(t) | ||
|
||
testCases := []struct { | ||
name string | ||
errCode string | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "valid error code as input", | ||
errCode: "E1000", | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "invalid error code format as input", | ||
errCode: "Foo", | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "valid error code format but unrecognized code as input", | ||
errCode: "E10000", | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "list all error codes", | ||
errCode: "", | ||
expectErr: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var out bytes.Buffer | ||
cmd := &errInfoCmd{out: &out} | ||
err := cmd.run(tc.errCode) | ||
|
||
assert.Equal(tc.expectErr, err != nil) | ||
}) | ||
} | ||
} |
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