Sam Hofius
2 years ago
3 changed files with 89 additions and 73 deletions
@ -1,52 +1,55 @@ |
|||||||
package keybase |
package keybase |
||||||
|
|
||||||
import ( |
import ( |
||||||
"log" |
"fmt" |
||||||
"os/exec" |
"os/exec" |
||||||
) |
) |
||||||
|
|
||||||
// Options holds... run... options...
|
// Options holds... run... options...
|
||||||
type Options struct { |
type Options struct { |
||||||
KeybaseLoction string // Optional, but required if keybase is not in your path
|
// Optional, but required if keybase is not in your path
|
||||||
HomeDir string // Only use this if you know what you're doing
|
KeybaseLocation string |
||||||
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
|
|
||||||
} |
|
||||||
|
|
||||||
// NewOptions returns a new instance of *Options with sensible defaults
|
// Only use this if you know what you're doing
|
||||||
func NewOptions() *Options { |
HomeDir string |
||||||
return &Options{ |
|
||||||
BotLiteMode: true, |
// Show others a typing notification while the bot is working on a command
|
||||||
ChannelBufferSize: 10, |
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:
|
// 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]
|
// 1. What the user has specified as the location [user specified]
|
||||||
// 2. Looks up the binary location using exec.LookPath [default]
|
// 2. Looks up the binary location using exec.LookPath [default]
|
||||||
// 3. Returns "keybase" and hopes its pathed on the users system [fallback]
|
// 3. Returns "keybase" and hopes its pathed on the users system [fallback]
|
||||||
func (opt *Options) locateKeybase() string { |
func locateKeybase() (string, error) { |
||||||
if opt.KeybaseLoction != "" { |
|
||||||
return opt.KeybaseLoction |
|
||||||
} |
|
||||||
path, err := exec.LookPath("keybase") |
path, err := exec.LookPath("keybase") |
||||||
if err != nil { |
if err != nil { |
||||||
log.Println("INFO: Could not detect keybase in path") |
return "", fmt.Errorf("could not determine path for keybase binary: %v", err) |
||||||
return "keybase" |
|
||||||
} |
} |
||||||
return path |
return path, nil |
||||||
} |
} |
||||||
|
|
||||||
// buildBaseCommand adds the homedirectory before the args, when required
|
// buildBaseCommand adds the homedirectory before the args, when required
|
||||||
func (opt *Options) buildArgs(args ...string) []string { |
func buildArgs(opts Options, args ...string) ([]string, error) { |
||||||
var cmd []string |
if len(args) == 0 { |
||||||
if opt.HomeDir != "" { |
return nil, fmt.Errorf("no arguments") |
||||||
cmd = append(cmd, "--home", opt.HomeDir) |
} |
||||||
|
|
||||||
|
// 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, "--enable-bot-lite-mode") |
||||||
} |
} |
||||||
cmd = append(cmd, args...) |
cmd = append(cmd, args...) |
||||||
return cmd |
|
||||||
|
return cmd, nil |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue