-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (50 loc) · 1.95 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"strings"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
"vonage-cloud-runtime-cli/pkg/api"
"vonage-cloud-runtime-cli/pkg/cmdutil"
"vonage-cloud-runtime-cli/pkg/format"
"vonage-cloud-runtime-cli/vcr/root"
)
var (
// use build flags to set these values - ie: go build -ldflags "-X main.version=1.0.0"
apiVersion = "v0.3"
version = "dev"
buildDate = "2021-09-01T00:00:00Z"
commit = "0000"
releaseURL = "https://api.github.com/repos/Vonage/vonage-cloud-runtime-cli"
)
func main() {
f := cmdutil.NewDefaultFactory(apiVersion, releaseURL)
ctx := context.Background()
updateMessageChan := make(chan string)
rootCmd := root.NewCmdRoot(f, version, buildDate, commit, updateMessageChan)
cmd, err := rootCmd.ExecuteContextC(ctx)
if err != nil {
printError(f.IOStreams(), err, cmd, updateMessageChan)
return
}
}
func printError(out *iostreams.IOStreams, err error, cmd *cobra.Command, updateMessageChan chan string) {
c := out.ColorScheme()
var flagError *cmdutil.FlagError
var httpErr api.Error
//nolint
if errors.As(err, &flagError) || strings.HasPrefix(err.Error(), "unknown command ") {
fmt.Fprintf(out.ErrOut, "%s\n", err)
fmt.Fprintln(out.ErrOut, cmd.UsageString())
} else if errors.As(err, &httpErr) {
fmt.Println(c.Red(`────────────────────────────────────────────────────────`))
fmt.Fprintf(out.ErrOut, "%s %s\n", c.FailureIcon(), format.PrintAPIError(out, err, &httpErr))
fmt.Println(c.Red(`────────────────────────────────────────────────────────`))
format.PrintUpdateMessage(out, version, updateMessageChan)
} else {
fmt.Fprintf(out.ErrOut, "%s %s\n", c.FailureIcon(), err)
format.PrintUpdateMessage(out, version, updateMessageChan)
}
}