-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (68 loc) · 2.01 KB
/
main.go
File metadata and controls
77 lines (68 loc) · 2.01 KB
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
package main
import (
"fmt"
"os"
"strings"
"github.com/CircleCI-Public/chunk-cli/internal/cmd"
"github.com/CircleCI-Public/chunk-cli/internal/ui"
)
var version = "dev"
func main() {
rewriteColonSyntax()
rootCmd := cmd.NewRootCmd(version)
if err := rootCmd.Execute(); err != nil {
m, d, s := errorDetails(err)
_, _ = fmt.Fprint(os.Stderr, ui.FormatError(m, d, s))
os.Exit(1)
}
}
func errorDetails(err error) (msg, detail, suggestion string) {
msg = "An unknown error occurred."
detail = err.Error()
suggestion = errorSuggestion(err)
if um, ok := err.(interface{ UserMessage() string }); ok {
msg = um.UserMessage()
}
if d, ok := err.(interface{ Detail() string }); ok {
detail = d.Detail()
}
if s, ok := err.(interface{ Suggestion() string }); ok {
suggestion = s.Suggestion()
}
return msg, detail, suggestion
}
// errorSuggestion returns a contextual hint for common error patterns.
func errorSuggestion(err error) string {
msg := err.Error()
lower := strings.ToLower(msg)
switch {
case strings.Contains(lower, "authentication") ||
strings.Contains(lower, "invalid api key") ||
strings.Contains(lower, "401"):
return "Hint: Run `chunk auth set anthropic` to set up your API key."
case strings.Contains(lower, "no such host") ||
strings.Contains(lower, "connection refused") ||
strings.Contains(lower, "network is unreachable") ||
strings.Contains(lower, "dial tcp"):
return "Hint: Check your internet connection."
}
return ""
}
// rewriteColonSyntax rewrites "validate:name" to "validate" "name" in os.Args
// before cobra parses, matching the TypeScript CLI's colon syntax support.
func rewriteColonSyntax() {
for i, arg := range os.Args {
if strings.HasPrefix(arg, "validate:") {
name := strings.TrimPrefix(arg, "validate:")
if name == "" {
continue
}
newArgs := make([]string, 0, len(os.Args)+1)
newArgs = append(newArgs, os.Args[:i]...)
newArgs = append(newArgs, "validate", name)
newArgs = append(newArgs, os.Args[i+1:]...)
os.Args = newArgs
return
}
}
}