Sam Hofius
2 years ago
3 changed files with 89 additions and 73 deletions
@ -1,52 +1,55 @@
@@ -1,52 +1,55 @@
|
||||
package keybase |
||||
|
||||
import ( |
||||
"log" |
||||
"fmt" |
||||
"os/exec" |
||||
) |
||||
|
||||
// Options holds... run... options...
|
||||
type Options struct { |
||||
KeybaseLoction string // Optional, but required if keybase is not in your path
|
||||
HomeDir string // Only use this if you know what you're doing
|
||||
EnableTyping bool // Show others a typing notification while the bot is working on a command
|
||||
BotLiteMode bool // Defaults to true - only disable if you need to.
|
||||
ChannelBufferSize int // The size of the channel buffers, may vary on rate of message ingestion
|
||||
} |
||||
// Optional, but required if keybase is not in your path
|
||||
KeybaseLocation string |
||||
|
||||
// NewOptions returns a new instance of *Options with sensible defaults
|
||||
func NewOptions() *Options { |
||||
return &Options{ |
||||
BotLiteMode: true, |
||||
ChannelBufferSize: 10, |
||||
} |
||||
// Only use this if you know what you're doing
|
||||
HomeDir string |
||||
|
||||
// Show others a typing notification while the bot is working on a command
|
||||
EnableTyping bool |
||||
|
||||
// Turn off Keybase's 'bot lite' mode. Only disable if you need to.
|
||||
DisableLiteMode bool |
||||
|
||||
// The size of the channel buffers, may vary on rate of message ingestion
|
||||
ChannelBufferSize int |
||||
} |
||||
|
||||
// locateKeybase attempts to find the location of the keybase binary in the following order:
|
||||
// 1. What the user has specified as the location [user specified]
|
||||
// 2. Looks up the binary location using exec.LookPath [default]
|
||||
// 3. Returns "keybase" and hopes its pathed on the users system [fallback]
|
||||
func (opt *Options) locateKeybase() string { |
||||
if opt.KeybaseLoction != "" { |
||||
return opt.KeybaseLoction |
||||
} |
||||
func locateKeybase() (string, error) { |
||||
path, err := exec.LookPath("keybase") |
||||
if err != nil { |
||||
log.Println("INFO: Could not detect keybase in path") |
||||
return "keybase" |
||||
return "", fmt.Errorf("could not determine path for keybase binary: %v", err) |
||||
} |
||||
return path |
||||
return path, nil |
||||
} |
||||
|
||||
// buildBaseCommand adds the homedirectory before the args, when required
|
||||
func (opt *Options) buildArgs(args ...string) []string { |
||||
var cmd []string |
||||
if opt.HomeDir != "" { |
||||
cmd = append(cmd, "--home", opt.HomeDir) |
||||
func buildArgs(opts Options, args ...string) ([]string, error) { |
||||
if len(args) == 0 { |
||||
return nil, fmt.Errorf("no arguments") |
||||
} |
||||
|
||||
// initialize slice so we never return a nil object
|
||||
cmd := make([]string, 0) |
||||
if opts.HomeDir != "" { |
||||
cmd = append(cmd, "--home", opts.HomeDir) |
||||
} |
||||
if opt.BotLiteMode { |
||||
if !opts.DisableLiteMode { |
||||
cmd = append(cmd, "--enable-bot-lite-mode") |
||||
} |
||||
cmd = append(cmd, args...) |
||||
return cmd |
||||
|
||||
return cmd, nil |
||||
} |
||||
|
Loading…
Reference in new issue