-
Notifications
You must be signed in to change notification settings - Fork 7
/
support_err.go
90 lines (75 loc) · 2.15 KB
/
support_err.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"io"
"sort"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/flomesh-io/fsm/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
# fsm support error-info E1000
Get the description for all error codes
# fsm 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(80)
if errCode != "" {
// Print the error code description mapping only for the given error code
e, err := errcode.FromStr(errCode)
if err != nil {
return fmt.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 fmt.Errorf("error code '%s' is not a valid error code recognized by FSM", 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]
desc = strings.Trim(desc, "\n") // Trim leading and trailing newlines for consistent formatting
table.Append([]string{key.String(), desc})
}
}
table.Render()
return nil
}