Delve代码分析笔记(4)——构建command tree

Delve使用cobra来构造command tree。先看root command,也就是dlv

func New() *cobra.Command {
    ......
    // Main dlv root command.
    RootCommand = &cobra.Command{
        Use:   "dlv",
        Short: "Delve is a debugger for the Go programming language.",
        Long:  dlvCommandLongDesc,
    }

    RootCommand.PersistentFlags().StringVarP(&Addr, "listen", "l", "localhost:0", "Debugging server listen address.")
    RootCommand.PersistentFlags().BoolVarP(&Log, "log", "", false, "Enable debugging server logging.")
    RootCommand.PersistentFlags().BoolVarP(&Headless, "headless", "", false, "Run debug server only, in headless mode.")
    RootCommand.PersistentFlags().BoolVarP(&AcceptMulti, "accept-multiclient", "", false, "Allows a headless server to accept multiple client connections. Note that the server API is not reentrant and clients will have to coordinate")
    RootCommand.PersistentFlags().IntVar(&ApiVersion, "api-version", 1, "Selects API version when headless")
    RootCommand.PersistentFlags().StringVar(&InitFile, "init", "", "Init file, executed by the terminal client.")
    RootCommand.PersistentFlags().StringVar(&BuildFlags, "build-flags", buildFlagsDefault, "Build flags, to be passed to the compiler.")
    ......
}

因为dlv command没有实现run函数,所以单独运行dlv命令只会打印cobra帮忙生成的默认输出:

# dlv
Delve is a source level debugger for Go programs.

......

Usage:
  dlv [command]

Available Commands:
  version     Prints version.
  ......
Flags:
      --accept-multiclient[=false]: Allows a headless server to accept multiple client connections. Note that the server API is not reentrant and clients will have to coordinate
    ......

依次为Long descriptionUsageAvailable Commands等等。

再以trace subcommand为例,看如何把subcommand加到root command里:

......
// 'trace' subcommand.
traceCommand := &cobra.Command{
    Use:   "trace [package] regexp",
    Short: "Compile and begin tracing program.",
    Long:  "Trace program execution. Will set a tracepoint on every function matching the provided regular expression and output information when tracepoint is hit.",
    Run:   traceCmd,
}
traceCommand.Flags().IntVarP(&traceAttachPid, "pid", "p", 0, "Pid to attach to.")
traceCommand.Flags().IntVarP(&traceStackDepth, "stack", "s", 0, "Show stack trace with given depth.")
RootCommand.AddCommand(traceCommand)
......

Cobra提供两种flags
a)Persistent Flags:对当前命令及其子命令都有效;
b)Local Flags:只对当前命令有效。

 

Delve代码分析笔记(2)——version

Version包只包含了一个version.go

package version

import "fmt"

// Version represents the current version of Delve.
type Version struct {
    Major    string
    Minor    string
    Patch    string
    Metadata string
    Build    string
}

var (
    // DelveVersion is the current version of Delve.
    DelveVersion = Version{Major: "0", Minor: "11", Patch: "0", Metadata: "alpha"}
)

func (v Version) String() string {
    return fmt.Sprintf("Version: %s.%s.%s-%s\nBuild: %s", v.Major, v.Minor, v.Patch, v.Metadata, v.Build)
}

Version包定义了一个Version类型的变量:DelveVersion。而Version类型的String()方法就是用来构造执行dlv version命令时,输出的字符串(cmd/dlv/cmds/commands.go):

......
// 'version' subcommand.
    versionCommand := &cobra.Command{
        Use:   "version",
        Short: "Prints version.",
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Printf("Delve Debugger\n%s\n", version.DelveVersion)
        },
    }
......

执行dlv version命令:

# dlv version
Delve Debugger
Version: 0.11.0-alpha
Build:

 

Delve代码分析笔记(1)——main.go

delve的项目主页:https://github.com/derekparker/delve
main.go的代码比较简单:

package main

import (
    "github.com/derekparker/delve/cmd/dlv/cmds"
    "github.com/derekparker/delve/version"
)

// Build is the git sha of this binaries build.
var Build string

func main() {
    version.DelveVersion.Build = Build
    cmds.New().Execute()
}

main函数就干了两件事:
(1)把Build的值赋给version.DelveVersion变量中的Build成员;
(2)cmds.New()返回一个cobra.Commandtree,然后调用Execute()函数执行相应的子命令,例如dlv version

 

Kubernetes笔记(3)—— kubectl和cobra

kubectl是控制k8s clustet manager的命令行工具:

$ kubectl
kubectl controls the Kubernetes cluster manager.

Find more information at https://github.com/kubernetes/kubernetes.

Usage:
  kubectl [flags]
  kubectl [command]

Available Commands:
  get            Display one or many resources
  describe       Show details of a specific resource or group of resources
......

kubectl使用了cobra这个项目作为构建命令行的工具,它包含commandsargsflags的概念:

Commands represent actions, Args are things and Flags are modifiers for those actions.

The pattern to follow is APPNAME VERB NOUN --ADJECTIVE. or APPNAME COMMAND ARG --FLAG

举个例子:

$ kubectl get node --v=10

kubectlappnamegetcommandnodearg--v=10flag