add flag to attach files to outbound messages

This commit is contained in:
Finn 2021-07-16 10:49:14 -07:00
parent 7ed5558491
commit 033bb59c76

View file

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
@ -28,11 +29,13 @@ import (
"gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/common"
"gitlab.com/signald/signald-go/cmd/signaldctl/config" "gitlab.com/signald/signald-go/cmd/signaldctl/config"
v0 "gitlab.com/signald/signald-go/signald/client-protocol/v0"
"gitlab.com/signald/signald-go/signald/client-protocol/v1" "gitlab.com/signald/signald-go/signald/client-protocol/v1"
) )
var ( var (
account string account string
attachments []string
SendMessageCmd = &cobra.Command{ SendMessageCmd = &cobra.Command{
Use: "send <group id | phone number> <message>", Use: "send <group id | phone number> <message>",
@ -56,6 +59,7 @@ var (
req := v1.SendRequest{ req := v1.SendRequest{
Username: account, Username: account,
MessageBody: strings.Join(args[1:], " "), MessageBody: strings.Join(args[1:], " "),
Attachments: []*v0.JsonAttachment{},
} }
if strings.HasPrefix(args[0], "+") { if strings.HasPrefix(args[0], "+") {
@ -64,6 +68,15 @@ var (
req.RecipientGroupID = args[0] req.RecipientGroupID = args[0]
} }
for _, attachment := range attachments {
path, err := filepath.Abs(attachment)
if err != nil {
log.Fatal("error resolving attachment", err)
}
log.Println(path)
req.Attachments = append(req.Attachments, &v0.JsonAttachment{Filename: path})
}
resp, err := req.Submit(common.Signald) resp, err := req.Submit(common.Signald)
if err != nil { if err != nil {
log.Fatal("error sending request to signald: ", err) log.Fatal("error sending request to signald: ", err)
@ -121,4 +134,5 @@ var (
func init() { func init() {
SendMessageCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") SendMessageCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use")
SendMessageCmd.Flags().StringSliceVarP(&attachments, "attachment", "A", []string{}, "attach a file to your outbound message. may be specified multiple times.")
} }