func main() { ctx.LoadFromHome() setupLogging() app := os.Args[0] args := os.Args[1:] for _, arg := range args { if arg == "-v" || arg == "--version" { newArgs := make([]string, len(args)+1) newArgs[0] = "version" copy(newArgs[1:], args) args = newArgs break } if arg == "--generate-bash-completion" { for name := range commands { fmt.Println(name) } for _, cmd := range ctx.Aliases() { fmt.Println(cmd) } return } } c := cli.NewCLI(app, gafka.Version+"-"+gafka.BuildId+"-"+gafka.BuiltAt) c.Args = os.Args[1:] if len(os.Args) > 1 { // command given, convert alias if alias, present := ctx.Alias(os.Args[1]); present { cargs := make([]string, 0) cargs = append(cargs, strings.Split(alias, " ")...) if len(os.Args) > 2 { cargs = append(cargs, os.Args[2:]...) } c.Args = cargs } } c.Commands = commands c.HelpFunc = func(m map[string]cli.CommandFactory) string { var buf bytes.Buffer buf.WriteString(fmt.Sprintf("Unified multi-datacenter multi-cluster kafka swiss-knife management console\n\n")) buf.WriteString(cli.BasicHelpFunc(app)(m)) return buf.String() } exitCode, err := c.Run() if err != nil { fmt.Fprintf(os.Stderr, "%+v\n", err) os.Exit(1) } else if c.IsVersion() { os.Exit(0) } os.Exit(exitCode) }
func (this *Console) doHelp() { outline := "" width := 0 cmds := make([]string, 0) for cmd, _ := range this.Cmds { cmds = append(cmds, cmd) } for _, cmd := range ctx.Aliases() { cmds = append(cmds, cmd) } cmds = append(cmds, this.builtinCmds...) sort.Strings(cmds) for _, cmd := range cmds { outline += cmd + " " width += len(cmd) if width > 80 { outline += "\n" width = 0 } } this.Ui.Output(outline) }
func (this *Console) Run(args []string) (exitCode int) { this.builtinCmds = []string{"help", "history"} this.Line = liner.NewLiner() this.Line.SetCtrlCAborts(false) this.Line.SetCompleter(func(line string) (c []string) { for cmd, _ := range this.Cmds { if strings.HasPrefix(cmd, strings.ToLower(line)) { c = append(c, cmd) } } for _, cmd := range ctx.Aliases() { if strings.HasPrefix(cmd, strings.ToLower(line)) { c = append(c, cmd) } } for _, cmd := range this.builtinCmds { if strings.HasPrefix(cmd, strings.ToLower(line)) { c = append(c, cmd) } } c = append(c, this.builtinCmds...) return }) defer this.Line.Close() if usr, err := user.Current(); err == nil { this.historyFile = filepath.Join(usr.HomeDir, fmt.Sprintf(".%s_history", this.Cmd)) if f, e := os.Open(this.historyFile); e == nil { this.Line.ReadHistory(f) f.Close() } } for { line, err := this.Line.Prompt(fmt.Sprintf("%s> ", this.Cmd)) if err != nil { break } line = strings.TrimSpace(line) if line == "" { continue } if line == "bye" || line == "q" || line == "quit" || line == "exit" { break } if this.runCommand(line) { // write out the history if len(this.historyFile) > 0 { this.Line.AppendHistory(line) if f, e := os.Create(this.historyFile); e == nil { this.Line.WriteHistory(f) f.Close() } } } } return }
func main() { ctx.LoadFromHome() setupLogging() app := os.Args[0] args := os.Args[1:] for _, arg := range args { if arg == "-v" || arg == "--version" { newArgs := make([]string, len(args)+1) newArgs[0] = "version" copy(newArgs[1:], args) args = newArgs break } if arg == "--generate-bash-completion" { if len(args) > 1 { // contextual auto complete lastArg := args[len(args)-2] switch lastArg { case "-z": // zone for _, zone := range ctx.SortedZones() { fmt.Println(zone) } return case "-c": // cluster zone := ctx.ZkDefaultZone() for i := 0; i < len(args)-1; i++ { if args[i] == "-z" { zone = args[i+1] } } zkzone := zk.NewZkZone(zk.DefaultConfig(zone, ctx.ZoneZkAddrs(zone))) zkzone.ForSortedClusters(func(zkcluster *zk.ZkCluster) { fmt.Println(zkcluster.Name()) }) return case "-t": // topic zone := ctx.ZkDefaultZone() var cluster string for i := 0; i < len(args)-1; i++ { if args[i] == "-z" { zone = args[i+1] } if args[i] == "-c" { cluster = args[i+1] } } if cluster != "" { zkzone := zk.NewZkZone(zk.DefaultConfig(zone, ctx.ZoneZkAddrs(zone))) zkcluster := zkzone.NewCluster(cluster) topics, _ := zkcluster.Topics() for _, t := range topics { fmt.Println(t) } } return } } for name := range commands { fmt.Println(name) } for _, cmd := range ctx.Aliases() { fmt.Println(cmd) } return } } c := cli.NewCLI(app, gafka.Version+"-"+gafka.BuildId+"-"+gafka.BuiltAt) c.Args = os.Args[1:] if len(os.Args) > 1 { // command given, convert alias if alias, present := ctx.Alias(os.Args[1]); present { var cargs []string cargs = append(cargs, strings.Split(alias, " ")...) if len(os.Args) > 2 { cargs = append(cargs, os.Args[2:]...) } c.Args = cargs } } c.Commands = commands c.HelpFunc = func(m map[string]cli.CommandFactory) string { var buf bytes.Buffer buf.WriteString(fmt.Sprintf("Unified multi-datacenter multi-cluster kafka swiss-knife management console\n\n")) buf.WriteString(cli.BasicHelpFunc(app)(m)) return buf.String() } exitCode, err := c.Run() if err != nil { fmt.Fprintf(os.Stderr, "%+v\n", err) os.Exit(1) } else if c.IsVersion() { os.Exit(0) } os.Exit(exitCode) }