//AddCommand adds a new command to your application
func (a *App) AddCommand(command Command) {
	command.App = a
	a.Commands = append(a.Commands, command)

	//Completer options
	var children []*readline.PrefixCompleter
	for _, child := range command.CompleteOptions {
		children = append(children, readline.PcItem(child))
	}
	a.completeOptions = append(a.completeOptions, readline.PcItem(command.Use, children...))
}
Exemple #2
0
	"time"

	"github.com/chzyer/readline"
)

func usage(w io.Writer) {
	io.WriteString(w, `
setprompt <prompt>
say <hello>
bye
`[1:])
}

var completer = readline.NewPrefixCompleter(
	readline.PcItem("say",
		readline.PcItem("hello"),
		readline.PcItem("bye"),
	),
	readline.PcItem("setprompt"),
	readline.PcItem("bye"),
	readline.PcItem("help"),
	readline.PcItem("go",
		readline.PcItem("build"),
		readline.PcItem("install"),
		readline.PcItem("test"),
	),
)

func main() {
	l, err := readline.NewEx(&readline.Config{
		Prompt:       "\033[31m»\033[0m ",
		HistoryFile:  "/tmp/readline.tmp",
Exemple #3
0
	insert <id> <json-string-template>
	bulk-insert <first-id> <last-id> <json-string-template>
	update <id> <new-json-string-template>
	find <id>
	bulk-delete <first-id> <last-id>
	delete <id>
	search <attribute> <value>
	set-log-level <log-level>
	[TODO] inspect-block <data-block-id>
	show-tree
	exit
`[1:])
}

var completer = readline.NewPrefixCompleter(
	readline.PcItem("insert"),
	readline.PcItem("bulk-insert"),
	readline.PcItem("update"),
	readline.PcItem("find"),
	readline.PcItem("help"),
	readline.PcItem("delete"),
	readline.PcItem("bulk-delete"),
	readline.PcItem("search"),
	readline.PcItem("set-log-level",
		readline.PcItem("debug"),
		readline.PcItem("info"),
		readline.PcItem("warn"),
	),
	readline.PcItem("show-tree"),
	readline.PcItem("exit"),
)
Exemple #4
0
// Function constructor - constructs new function for listing given directory
func listFiles(path string) func(string) []string {
	return func(line string) []string {
		names := make([]string, 0)
		files, _ := ioutil.ReadDir(path)
		for _, f := range files {
			names = append(names, f.Name())
		}
		return names
	}
}

var completer = readline.NewPrefixCompleter(
	readline.PcItem("mode",
		readline.PcItem("vi"),
		readline.PcItem("emacs"),
	),
	readline.PcItem("login"),
	readline.PcItem("say",
		readline.PcItemDynamic(listFiles("./"),
			readline.PcItem("with",
				readline.PcItem("following"),
				readline.PcItem("items"),
			),
		),
		readline.PcItem("hello"),
		readline.PcItem("bye"),
	),
	readline.PcItem("setprompt"),
	readline.PcItem("setpassword"),
	readline.PcItem("bye"),
Exemple #5
0
	"log"
	"strconv"
	"strings"
	"time"

	"github.com/chzyer/readline"
)

func usage(w io.Writer) {
	io.WriteString(w, "commands:\n")
	io.WriteString(w, completer.Tree("    "))
}

var completer = readline.NewPrefixCompleter(
	readline.PcItem("mode",
		readline.PcItem("vi"),
		readline.PcItem("emacs"),
	),
	readline.PcItem("login"),
	readline.PcItem("say",
		readline.PcItem("hello"),
		readline.PcItem("bye"),
	),
	readline.PcItem("setprompt"),
	readline.PcItem("setpassword"),
	readline.PcItem("bye"),
	readline.PcItem("help"),
	readline.PcItem("go",
		readline.PcItem("build", readline.PcItem("-o"), readline.PcItem("-v")),
		readline.PcItem("install",
			readline.PcItem("-v"),
			readline.PcItem("-vv"),
Exemple #6
0
	"github.com/chzyer/readline"
)

func usage(w io.Writer) {
	io.WriteString(w, `
login
setprompt <prompt>
say <hello>
bye
`[1:])
}

var completer = readline.NewPrefixCompleter(
	readline.PcItem("mode",
		readline.PcItem("vi"),
		readline.PcItem("emacs"),
	),
	readline.PcItem("login"),
	readline.PcItem("say",
		readline.PcItem("hello"),
		readline.PcItem("bye"),
	),
	readline.PcItem("setprompt"),
	readline.PcItem("bye"),
	readline.PcItem("help"),
	readline.PcItem("go",
		readline.PcItem("build"),
		readline.PcItem("install"),
		readline.PcItem("test"),
	),
)
Exemple #7
0
func main() {
	kingpin.Parse()

	var conn net.Conn
	var err error
	if *rawtls {
		conn, err = tls.Dial("tcp", *address, &tls.Config{
			InsecureSkipVerify: true,
		})
		if err != nil {
			fmt.Fprintf(os.Stderr, "Connect failed: %v\n", err)
			return
		}
	} else {
		conn, err = net.Dial("tcp", *address)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Connect failed: %v\n", err)
			return
		}
	}

	c := client.New(conn)
	go func() {
		err := c.Serve()
		if err != nil {
			fmt.Fprintf(os.Stderr, "\nConnection lost: %v\n", err)
			os.Exit(-1)
		}
	}()

	_, _, err = c.Version(1024*1024, qp.Version)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Version failed: %v\n", err)
		return
	}

	root, _, err := c.Attach(nil, *user, *service)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Attach failed: %v\n", err)
		return
	}

	cwd := root

	confirmation, err = readline.New("")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not make confirmation readline instance: %v\n", err)
		return
	}

	if len(*command) > 0 {
		args := ""
		for i := 1; i < len(*command); i++ {
			if i != 1 {
				args += " "
			}
			args += (*command)[i]
		}

		f, ok := cmds[(*command)[0]]
		if !ok {
			fmt.Fprintf(os.Stderr, "no such command: [%s]\n", *command)
			return
		}
		cwd, err = f(root, cwd, args)
		if err != nil {
			fmt.Fprintf(os.Stderr, "\ncommand %s failed: %v\n", *command, err)
		}
		return
	}

	completer := readline.NewPrefixCompleter()
	for k := range cmds {
		completer.Children = append(completer.Children, readline.PcItem(k))
	}

	rl, err := readline.NewEx(&readline.Config{
		Prompt:       "9p> ",
		AutoComplete: completer,
	})

	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create readline: %v\n", err)
		return
	}

	defer rl.Close()

	fmt.Fprintf(os.Stderr, "Welcome to the qptools 9P cli.\nPress tab to see available commands.\n")

	for {
		line, err := rl.Readline()
		if err != nil { // io.EOF
			break
		}

		idx := strings.Index(line, " ")
		var cmd, args string
		if idx != -1 {
			cmd = line[:idx]
			args = line[idx+1:]
		} else {
			cmd = line
		}

		f, ok := cmds[cmd]
		if !ok {
			fmt.Fprintf(os.Stderr, "no such command: [%s]\n", cmd)
			continue
		}
		cwd, err = f(root, cwd, args)
		if err != nil {
			fmt.Fprintf(os.Stderr, "\ncommand %s failed: %v\n", cmd, err)
		}
	}
}
Exemple #8
0
func main() {
	loop := true
	if len(os.Args) < 4 {
		fmt.Printf("Too few arguments\n")
		return
	}

	addr := os.Args[1]
	user := os.Args[2]
	service := os.Args[3]

	c := &convenience.Client{}
	err := c.Dial("tcp", addr, user, service)
	if err != nil {
		fmt.Printf("Connect failed: %v\n", err)
		return
	}

	var cmds map[string]func(string)
	cmds = map[string]func(string){
		"ls": func(s string) {
			if s == "" {
				s = "/"
			}
			strs, err := c.List(s)
			if err != nil {
				fmt.Printf("ls failed: %v\n", err)
				return
			}
			fmt.Printf("%v\n", strs)
		},
		"cat": func(s string) {
			strs, err := c.Read(s)
			if err != nil {
				fmt.Printf("cat failed: %v\n", err)
				return
			}
			fmt.Printf("%v\n", strs)
		},
		"get": func(string) {
			fmt.Printf("get is not yet implemented\n")
		},
		"put": func(string) {
			fmt.Printf("put is not yet implemented\n")
		},
		"mkdir": func(s string) {
			err := c.Create(s, true)
			if err != nil {
				fmt.Printf("mkdir failed: %v\n", err)
				return
			}
		},
		"rm": func(s string) {
			err := c.Remove(s)
			if err != nil {
				fmt.Printf("rm failed: %v\n", err)
				return
			}
		},
		"chmod": func(string) {
			fmt.Printf("chmod is not yet implemented\n")
		},
		"quit": func(string) {
			fmt.Printf("bye\n")
			loop = false
		},
		"help": func(string) {
			fmt.Printf("Available commands: \n")
			for k := range cmds {
				fmt.Printf("\t%s\n", k)
			}
		},
	}

	completer := readline.NewPrefixCompleter()
	for k := range cmds {
		completer.Children = append(completer.Children, readline.PcItem(k))
	}

	rl, err := readline.NewEx(&readline.Config{
		Prompt:       "9p> ",
		AutoComplete: completer,
	})

	if err != nil {
		fmt.Printf("failed to create readline: %v\n", err)
		return
	}

	defer rl.Close()

	for loop {
		line, err := rl.Readline()
		if err != nil { // io.EOF
			break
		}

		idx := strings.Index(line, " ")
		var cmd, args string
		if idx != -1 {
			cmd = line[:idx]
			args = line[idx+1:]
		} else {
			cmd = line
		}

		f, ok := cmds[cmd]
		if !ok {
			fmt.Printf("no such command: [%s]\n", cmd)
			continue
		}
		f(args)
	}
}