Skip to content

Commit df39339

Browse files
authored
refactor: move external command checks into commands lib (#198)
* refactor: move environment-based command restrictions to be stored with the command itself
1 parent 0cd468a commit df39339

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

command.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,39 @@ type Command struct {
9090
// simple typo in a sub command will invoke the parent command and may
9191
// end up returning a cryptic error to the user.
9292
Subcommands map[string]*Command
93+
94+
// NoRemote denotes that a command cannot be executed in a remote environment
95+
NoRemote bool
96+
97+
// NoLocal denotes that a command cannot be executed in a local environment
98+
NoLocal bool
99+
100+
// Extra contains a set of other command-specific parameters
101+
Extra *Extra
102+
}
103+
104+
// Extra is a set of tag information for a command
105+
type Extra struct {
106+
m map[interface{}]interface{}
107+
}
108+
109+
func (e *Extra) SetValue(key, value interface{}) *Extra {
110+
if e == nil {
111+
e = &Extra{}
112+
}
113+
if e.m == nil {
114+
e.m = make(map[interface{}]interface{})
115+
}
116+
e.m[key] = value
117+
return e
118+
}
119+
120+
func (e *Extra) GetValue(key interface{}) (interface{}, bool) {
121+
if e == nil || e.m == nil {
122+
return nil, false
123+
}
124+
val, found := e.m[key]
125+
return val, found
93126
}
94127

95128
var (
@@ -141,6 +174,7 @@ func (c *Command) call(req *Request, re ResponseEmitter, env Environment) error
141174
}
142175

143176
// Resolve returns the subcommands at the given path
177+
// The returned set of subcommands starts with this command and therefore is always at least size 1
144178
func (c *Command) Resolve(pth []string) ([]*Command, error) {
145179
cmds := make([]*Command, len(pth)+1)
146180
cmds[0] = c

http/parse.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
2828
getPath = pth[:len(pth)-1]
2929
)
3030

31-
cmd, err := root.Get(getPath)
31+
cmdPath, err := root.Resolve(getPath)
3232
if err != nil {
3333
// 404 if there is no command at that path
3434
return nil, ErrNotFound
3535
}
3636

37+
for _, c := range cmdPath {
38+
if c.NoRemote {
39+
return nil, ErrNotFound
40+
}
41+
}
42+
43+
cmd := cmdPath[len(cmdPath)-1]
3744
sub := cmd.Subcommands[pth[len(pth)-1]]
3845

3946
if sub == nil {
@@ -49,6 +56,10 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
4956
cmd = sub
5057
}
5158

59+
if cmd.NoRemote {
60+
return nil, ErrNotFound
61+
}
62+
5263
opts, stringArgs2 := parseOptions(r)
5364
optDefs, err := root.GetOptions(pth)
5465
if err != nil {

0 commit comments

Comments
 (0)