1package keybase 2 3import ( 4 "errors" 5 "fmt" 6 "os" 7 "os/exec" 8 "time" 9) 10 11// KeybaseBin is cli binary 12const KeybaseBin = "keybase" 13 14// Errors when parsing notification settings 15var ( 16 ErrorMissingConversation = errors.New("keybase: missing conversation (team or username) to send to") 17 ErrorMissingMessage = errors.New("keybase: missing message content") 18 ErrorBadExplodingTime = errors.New("keybase: explodingLifetime must be a time between 30s and 168h0m0s") 19) 20 21// Notification is a Keybase notification. 22type Notification struct { 23 // Conversation is the team name or users (comma-separated) to notify. 24 Conversation string 25 // ChannelName is the team's chat channel to send to. If empty, the team's 26 // default channel will be used (typically "general"). 27 ChannelName string 28 // Public toggles broadcasting a message to everyone (when Conversation is your 29 // username), or to teams (when Conversation is your team name). 30 Public bool 31 // ExplodingLifetime will delete the message in the time given. 32 ExplodingLifetime time.Duration 33 // Message contents to be sent 34 Message string 35} 36 37// prepareArgs builds the `keybase` cli arguments from the Notification settings 38func prepareArgs(n *Notification) ([]string, error) { 39 switch { 40 case n.Conversation == "": 41 return nil, ErrorMissingConversation 42 case n.Message == "": 43 return nil, ErrorMissingMessage 44 case n.ExplodingLifetime < 0: 45 return nil, ErrorBadExplodingTime 46 } 47 48 args := []string{"chat", "send"} 49 if n.ChannelName != "" { 50 args = append(args, "--channel", n.ChannelName) 51 } 52 if n.Public { 53 args = append(args, "--public") 54 } 55 if n.ExplodingLifetime > 0 { 56 args = append(args, "--exploding-lifetime", fmt.Sprint(n.ExplodingLifetime)) 57 } 58 args = append(args, n.Conversation, n.Message) 59 return args, nil 60} 61 62// Send triggers a Keybase notification. 63func (n *Notification) Send() error { 64 args, err := prepareArgs(n) 65 if err != nil { 66 return err 67 } 68 69 cmd := exec.Command(KeybaseBin, args...) 70 cmd.Stderr = os.Stderr 71 err = cmd.Run() 72 if err != nil { 73 return fmt.Errorf("keybase: command error: %v", err) 74 } 75 return nil 76} 77