File: command.go

package info (click to toggle)
nebula 1.6.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,376 kB
  • sloc: makefile: 149; sh: 100; python: 16
file content (162 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download
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
package sshd

import (
	"errors"
	"flag"
	"fmt"
	"sort"
	"strings"

	"github.com/armon/go-radix"
)

// CommandFlags is a function called before help or command execution to parse command line flags
// It should return a flag.FlagSet instance and a pointer to the struct that will contain parsed flags
type CommandFlags func() (*flag.FlagSet, interface{})

// CommandCallback is the function called when your command should execute.
// fs will be a a pointer to the struct provided by Command.Flags callback, if there was one. -h and -help are reserved
// and handled automatically for you.
// a will be any unconsumed arguments, if no Command.Flags was available this will be all the flags passed in.
// w is the writer to use when sending messages back to the client.
// If an error is returned by the callback it is logged locally, the callback should handle messaging errors to the user
// where appropriate
type CommandCallback func(fs interface{}, a []string, w StringWriter) error

type Command struct {
	Name             string
	ShortDescription string
	Help             string
	Flags            CommandFlags
	Callback         CommandCallback
}

func execCommand(c *Command, args []string, w StringWriter) error {
	var (
		fl *flag.FlagSet
		fs interface{}
	)

	if c.Flags != nil {
		fl, fs = c.Flags()
		if fl != nil {
			//TODO: handle the error
			fl.Parse(args)
			args = fl.Args()
		}
	}

	return c.Callback(fs, args, w)
}

func dumpCommands(c *radix.Tree, w StringWriter) {
	err := w.WriteLine("Available commands:")
	if err != nil {
		//TODO: log
		return
	}

	cmds := make([]string, 0)
	for _, l := range allCommands(c) {
		cmds = append(cmds, fmt.Sprintf("%s - %s", l.Name, l.ShortDescription))
	}

	sort.Strings(cmds)
	err = w.Write(strings.Join(cmds, "\n") + "\n\n")
	if err != nil {
		//TODO: log
	}
}

func lookupCommand(c *radix.Tree, sCmd string) (*Command, error) {
	cmd, ok := c.Get(sCmd)
	if !ok {
		return nil, nil
	}

	command, ok := cmd.(*Command)
	if !ok {
		return nil, errors.New("failed to cast command")
	}

	return command, nil
}

func matchCommand(c *radix.Tree, cmd string) []string {
	cmds := make([]string, 0)
	c.WalkPrefix(cmd, func(found string, v interface{}) bool {
		cmds = append(cmds, found)
		return false
	})
	sort.Strings(cmds)
	return cmds
}

func allCommands(c *radix.Tree) []*Command {
	cmds := make([]*Command, 0)
	c.WalkPrefix("", func(found string, v interface{}) bool {
		cmd, ok := v.(*Command)
		if ok {
			cmds = append(cmds, cmd)
		}
		return false
	})
	return cmds
}

func helpCallback(commands *radix.Tree, a []string, w StringWriter) (err error) {
	// Just typed help
	if len(a) == 0 {
		dumpCommands(commands, w)
		return nil
	}

	// We are printing a specific commands help text
	cmd, err := lookupCommand(commands, a[0])
	if err != nil {
		//TODO: handle error
		//TODO: message the user
		return
	}

	if cmd != nil {
		err = w.WriteLine(fmt.Sprintf("%s - %s", cmd.Name, cmd.ShortDescription))
		if err != nil {
			return err
		}

		if cmd.Help != "" {
			err = w.WriteLine(fmt.Sprintf("  %s", cmd.Help))
			if err != nil {
				return err
			}
		}

		if cmd.Flags != nil {
			fs, _ := cmd.Flags()
			if fs != nil {
				fs.SetOutput(w.GetWriter())
				fs.PrintDefaults()
			}
		}

		return nil
	}

	err = w.WriteLine("Command not available " + a[0])
	if err != nil {
		return err
	}

	return nil
}

func checkHelpArgs(args []string) bool {
	for _, a := range args {
		if a == "-h" || a == "-help" {
			return true
		}
	}

	return false
}