-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcommand.go
87 lines (72 loc) · 2.11 KB
/
command.go
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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package slackbot
import (
"fmt"
"os/exec"
)
// Command is the interface the bot uses to run things
type Command interface {
Run(channel string, args []string) (string, error)
ShowResult() bool // Whether to output result back to channel
Description() string
}
// execCommand is a Command that does an exec.Command(...) on the system
type execCommand struct {
exec string // Command to execute
args []string // Args for exec.Command
showResult bool
description string
config Config
}
// NewExecCommand creates an ExecCommand
func NewExecCommand(exec string, args []string, showResult bool, description string, config Config) Command {
return execCommand{
exec: exec,
args: args,
showResult: showResult,
description: description,
config: config,
}
}
// Run runs the exec command
func (c execCommand) Run(_ string, _ []string) (string, error) {
if c.config.DryRun() {
return fmt.Sprintf("I'm in dry run mode. I would have run `%s` with args: %s", c.exec, c.args), nil
}
out, err := exec.Command(c.exec, c.args...).CombinedOutput()
outAsString := string(out)
return outAsString, err
}
// ShowResult decides whether to show the results from the exec
func (c execCommand) ShowResult() bool {
return c.config.DryRun() || c.showResult
}
// Description describes the command
func (c execCommand) Description() string {
return c.description
}
// CommandFn is the function that is run for this command
type CommandFn func(channel string, args []string) (string, error)
// NewFuncCommand creates a new function command
func NewFuncCommand(fn CommandFn, desc string, config Config) Command {
return funcCommand{
fn: fn,
desc: desc,
config: config,
}
}
type funcCommand struct {
desc string
fn CommandFn
config Config
}
func (c funcCommand) Run(channel string, args []string) (string, error) {
return c.fn(channel, args)
}
func (c funcCommand) ShowResult() bool {
return true
}
func (c funcCommand) Description() string {
return c.desc
}