Repository navigation
Expand file tree
/
Copy pathcli.go
More file actions
152 lines (140 loc) · 4.77 KB
/
Copy pathcli.go
File metadata and controls
152 lines (140 loc) · 4.77 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package devflow
import "strings"
// ParseCLIArgs parses command line arguments for devflow tools (codejob, gopush).
// It returns the message, tag, whether help was requested, and whether -release flag is present.
// Flags like -release and --release are detected and excluded from message/tag assignment.
func ParseCLIArgs(args []string) (message, tag string, isHelp, isRelease bool) {
if len(args) > 1 {
arg := strings.ToLower(args[1])
switch arg {
case "help", "-help", "--help", "h", "-h", "?", "-?":
return "", "", true, false
}
message = args[1]
}
if len(args) > 2 {
arg := args[2]
// Don't assign -release or --release as tag
if arg != "-release" && arg != "--release" {
tag = arg
}
}
// Scan all args for -release or --release flag
for _, arg := range args[1:] {
if arg == "-release" || arg == "--release" {
isRelease = true
break
}
}
return
}
const (
cmdDispatch = "dispatch"
cmdPull = "pull"
cmdReply = "reply"
cmdApprove = "approve"
cmdClose = "close"
)
// CodeJobCLIOpts holds parsed options for the codejob CLI.
type CodeJobCLIOpts struct {
Command string // cmdDispatch, cmdPull, cmdReply, cmdApprove, cmdClose, or ""
Message string // only filled for close
Tag string // only filled for close
ReplyText string // only filled for reply
ParseError string // set if parsing fails
IsHelp bool
IsRelease bool // only valid with close
IsResetGHToken bool
CIPhase string // "dispatch", "review", "verdict", "publish"
InitAction bool
Force bool
Org string
Visibility string
}
// ParseCodeJobFlags parses the complete set of flags and positional arguments for the codejob CLI.
func ParseCodeJobFlags(args []string) CodeJobCLIOpts {
var opts CodeJobCLIOpts
var remaining []string
if len(args) == 0 {
return opts
}
for i := 1; i < len(args); i++ {
arg := args[i]
if arg == "-h" || arg == "--help" || arg == "help" {
opts.IsHelp = true
} else if arg == "-release" || arg == "--release" {
opts.IsRelease = true
} else if arg == "--reset-gh-token" {
opts.IsResetGHToken = true
} else if arg == "--init-action" {
opts.InitAction = true
} else if arg == "--force" {
opts.Force = true
} else if strings.HasPrefix(arg, "--ci=") {
opts.CIPhase = strings.TrimPrefix(arg, "--ci=")
} else if arg == "--ci" && i+1 < len(args) {
opts.CIPhase = args[i+1]
i++
} else if strings.HasPrefix(arg, "--org=") {
opts.Org = strings.TrimPrefix(arg, "--org=")
} else if arg == "--org" && i+1 < len(args) {
opts.Org = args[i+1]
i++
} else if strings.HasPrefix(arg, "--visibility=") {
opts.Visibility = strings.TrimPrefix(arg, "--visibility=")
} else if arg == "--visibility" && i+1 < len(args) {
opts.Visibility = args[i+1]
i++
} else {
remaining = append(remaining, arg)
}
}
if opts.IsHelp || opts.IsResetGHToken || opts.InitAction || opts.CIPhase != "" {
// When flags like --ci or --init-action or --help are passed, we don't strictly require a command.
// However, if there are remaining args, we shouldn't necessarily error out, as some tests pass --ci alongside message.
// Actually, let's process remaining just in case.
}
if len(remaining) > 0 {
cmd := remaining[0]
switch cmd {
case cmdDispatch, cmdPull, cmdApprove:
opts.Command = cmd
if len(remaining) > 1 {
opts.ParseError = "codejob: unknown command \"" + strings.Join(remaining[1:], " ") + "\"; run codejob for help"
}
case cmdReply:
opts.Command = cmd
if len(remaining) > 1 {
opts.ReplyText = remaining[1]
if len(remaining) > 2 {
opts.ParseError = "codejob: unknown command \"" + strings.Join(remaining[2:], " ") + "\"; run codejob for help"
}
} else {
opts.ParseError = "codejob: reply needs a message: codejob reply \"text\""
}
case cmdClose:
opts.Command = cmd
if len(remaining) > 1 {
opts.Message = remaining[1]
if len(remaining) > 2 {
opts.Tag = remaining[2]
}
if len(remaining) > 3 {
opts.ParseError = "codejob: unknown command \"" + strings.Join(remaining[3:], " ") + "\"; run codejob for help"
}
} else {
opts.ParseError = "codejob: close needs a commit message: codejob close \"message\" [tag]"
}
default:
opts.ParseError = "codejob: unknown command \"" + cmd + "\"; run codejob for help"
}
}
if opts.IsRelease && opts.Command != cmdClose && opts.Command != "" {
opts.ParseError = "codejob: --release can only be used with the close command"
}
// Ensure bare commands don't have release flag if it's strictly enforced.
if opts.IsRelease && opts.Command == "" && len(remaining) == 0 && !opts.IsHelp && !opts.InitAction && !opts.IsResetGHToken && opts.CIPhase == "" {
opts.ParseError = "codejob: --release can only be used with the close command"
}
return opts
}