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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
package kubectl
import (
"strings"
)
type SubcommandInfo struct {
Subcommand Subcommand
FormatOption FormatOption
NoHeader bool
Watch bool
Help bool
Recursive bool
Short bool
IsKrew bool
}
type FormatOption int
const (
None FormatOption = iota
Wide
Json
Yaml
)
type Subcommand int
const (
Create Subcommand = iota + 1
Expose
Run
Set
Explain
Get
Edit
Delete
Rollout
Scale
Autoscale
Certificate
ClusterInfo
Top
Cordon
Uncordon
Drain
Taint
Describe
Logs
Attach
Exec
PortForward
Proxy
Cp
Auth
Diff
Apply
Patch
Replace
Wait
Convert
Kustomize
Label
Annotate
Completion
APIResources
APIVersions
Config
Plugin
Version
Options
)
var strToSubcommand = map[string]Subcommand{
"create": Create,
"expose": Expose,
"run": Run,
"set": Set,
"explain": Explain,
"get": Get,
"edit": Edit,
"delete": Delete,
"rollout": Rollout,
"scale": Scale,
"autoscale": Autoscale,
"certificate": Certificate,
"cluster-info": ClusterInfo,
"top": Top,
"cordon": Cordon,
"uncordon": Uncordon,
"drain": Drain,
"taint": Taint,
"describe": Describe,
"logs": Logs,
"attach": Attach,
"exec": Exec,
"port-forward": PortForward,
"proxy": Proxy,
"cp": Cp,
"auth": Auth,
"diff": Diff,
"apply": Apply,
"patch": Patch,
"replace": Replace,
"wait": Wait,
"convert": Convert,
"kustomize": Kustomize,
"label": Label,
"annotate": Annotate,
"completion": Completion,
"api-resources": APIResources,
"api-versions": APIVersions,
"config": Config,
"plugin": Plugin,
"version": Version,
"options": Options,
}
func InspectSubcommand(command string) (Subcommand, bool) {
sc, ok := strToSubcommand[command]
return sc, ok
}
func CollectCommandlineOptions(args []string, info *SubcommandInfo) {
for i := range args {
if strings.HasPrefix(args[i], "--output") {
switch args[i] {
case "--output=json":
info.FormatOption = Json
case "--output=yaml":
info.FormatOption = Yaml
case "--output=wide":
info.FormatOption = Wide
default:
if len(args)-1 > i {
formatOption := args[i+1]
switch formatOption {
case "json":
info.FormatOption = Json
case "yaml":
info.FormatOption = Yaml
case "wide":
info.FormatOption = Wide
default:
// custom-columns, go-template, etc are currently not supported
}
}
}
} else if strings.HasPrefix(args[i], "-o") {
switch args[i] {
// both '-ojson' and '-o=json' works
case "-ojson", "-o=json":
info.FormatOption = Json
case "-oyaml", "-o=yaml":
info.FormatOption = Yaml
case "-owide", "-o=wide":
info.FormatOption = Wide
default:
// otherwise, look for next arg because '-o json' also works
if len(args)-1 > i {
formatOption := args[i+1]
switch formatOption {
case "json":
info.FormatOption = Json
case "yaml":
info.FormatOption = Yaml
case "wide":
info.FormatOption = Wide
default:
// custom-columns, go-template, etc are currently not supported
}
}
}
} else if strings.HasPrefix(args[i], "--short") {
switch args[i] {
case "--short=true":
info.Short = true
case "--short=false":
info.Short = false
default:
info.Short = true
}
} else if args[i] == "--no-headers" {
info.NoHeader = true
} else if args[i] == "-w" || args[i] == "--watch" {
info.Watch = true
} else if args[i] == "--recursive=true" {
info.Recursive = true
} else if args[i] == "-h" || args[i] == "--help" {
info.Help = true
}
}
}
// TODO: return shouldColorize = false when the given args is for plugin
func InspectSubcommandInfo(args []string) (*SubcommandInfo, bool) {
ret := &SubcommandInfo{}
CollectCommandlineOptions(args, ret)
for i := range args {
cmd, ok := InspectSubcommand(args[i])
if !ok {
continue
}
ret.Subcommand = cmd
return ret, true
}
return ret, false
}
|