欢迎关注我的公众号:

目前刚开始写一个月,一共写了18篇原创文章,文章目录如下:
istio防故障利器,你知道几个,istio新手不要读,太难!
不懂envoyfilter也敢说精通istio系列-http-rbac-不要只会用AuthorizationPolicy配置权限
不懂envoyfilter也敢说精通istio系列-02-http-corsFilter-不要只会vs
不懂envoyfilter也敢说精通istio系列-03-http-csrf filter-再也不用再代码里写csrf逻辑了
不懂envoyfilter也敢说精通istio系列http-jwt_authn-不要只会RequestAuthorization
不懂envoyfilter也敢说精通istio系列-05-fault-filter-故障注入不止是vs
不懂envoyfilter也敢说精通istio系列-06-http-match-配置路由不只是vs
不懂envoyfilter也敢说精通istio系列-07-负载均衡配置不止是dr
不懂envoyfilter也敢说精通istio系列-08-连接池和断路器
不懂envoyfilter也敢说精通istio系列-09-http-route filter
不懂envoyfilter也敢说精通istio系列-network filter-redis proxy
不懂envoyfilter也敢说精通istio系列-network filter-HttpConnectionManager
不懂envoyfilter也敢说精通istio系列-ratelimit-istio ratelimit完全手册
————————————————
var (//支持的shell及函数
completionShells = map[string]func(out io.Writer, boilerPlate string, cmd *cobra.Command) error{
"bash": runCompletionBash,
"zsh": runCompletionZsh,
}
)
//创建completion命令
func NewCmdCompletion(out io.Writer, boilerPlate string) *cobra.Command {
shells := []string{}
for s := range completionShells {
shells = append(shells, s)
}
cmd := &cobra.Command{//创建cobra命令
Use: "completion SHELL",
DisableFlagsInUseLine: true,
Short: i18n.T("Output shell completion code for the specified shell (bash or zsh)"),
Long: completionLong,
Example: completionExample,
Run: func(cmd *cobra.Command, args []string) {
err := RunCompletion(out, boilerPlate, cmd, args)//运行
cmdutil.CheckErr(err)
},
ValidArgs: shells,//有效参数
}
return cmd
}
//运行
func RunCompletion(out io.Writer, boilerPlate string, cmd *cobra.Command, args []string) error {
if len(args) == 0 {//参数不能是0个
return cmdutil.UsageErrorf(cmd, "Shell not specified.")
}
if len(args) > 1 {//参数不能大于1个
return cmdutil.UsageErrorf(cmd, "Too many arguments. Expected only the shell type.")
}
run, found := completionShells[args[0]]//获取运行函数
if !found {
return cmdutil.UsageErrorf(cmd, "Unsupported shell type %q.", args[0])
}
return run(out, boilerPlate, cmd.Parent())//运行
}
//运行bash completion
func runCompletionBash(out io.Writer, boilerPlate string, kubectl *cobra.Command) error {
if len(boilerPlate) == 0 {//设置license
boilerPlate = defaultBoilerPlate
}
if _, err := out.Write([]byte(boilerPlate)); err != nil {//输出license
return err
}
return kubectl.GenBashCompletion(out)//产生命令补全
}
//产生命令补全
func (c *Command) GenBashCompletion(w io.Writer) error {
buf := new(bytes.Buffer)//创建buffer
writePreamble(buf, c.Name())//写序言
if len(c.BashCompletionFunction) > 0 {//如果有BashCompletionFunction函数,则写函数
buf.WriteString(c.BashCompletionFunction + "\n")
}
gen(buf, c)//写具体命令
writePostscript(buf, c.Name())//写post脚本
_, err := buf.WriteTo(w)//buffer写到writer里
return err
}
//递归调用写命令completion
func gen(buf *bytes.Buffer, cmd *Command) {
for _, c := range cmd.Commands() {//遍历命令
if !c.IsAvailableCommand() || c == cmd.helpCommand {
continue
}
gen(buf, c)//递归调用
}
commandName := cmd.CommandPath()//获取命令名称
commandName = strings.Replace(commandName, " ", "_", -1)
commandName = strings.Replace(commandName, ":", "__", -1)
if cmd.Root() == cmd {//命令是root命令
buf.WriteString(fmt.Sprintf("_%s_root_command()\n{\n", commandName))
} else {
buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName))
}
buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
buf.WriteString("\n")
buf.WriteString(" command_aliases=()\n")
buf.WriteString("\n")
writeCommands(buf, cmd)//写命令
writeFlags(buf, cmd)//写flags
writeRequiredFlag(buf, cmd)//写reuqiredflag
writeRequiredNouns(buf, cmd)//写requiredNouns
writeArgAliases(buf, cmd)//写别名
buf.WriteString("}\n\n")
}
8834

被折叠的 条评论
为什么被折叠?



