Compare commits
2 commits
main
...
signaldctl
Author | SHA1 | Date | |
---|---|---|---|
9df33c1ca0 | |||
25501ecdf6 |
3 changed files with 69 additions and 7 deletions
62
cmd/signaldctl/cmd/subscribe.go
Normal file
62
cmd/signaldctl/cmd/subscribe.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
|
||||
"gitlab.com/signald/signald-go/cmd/signaldctl/config"
|
||||
client_protocol "gitlab.com/signald/signald-go/signald/client-protocol"
|
||||
v1 "gitlab.com/signald/signald-go/signald/client-protocol/v1"
|
||||
)
|
||||
|
||||
var (
|
||||
accountIdentifier string
|
||||
subscribeCmd = &cobra.Command{
|
||||
Use: "subscribe",
|
||||
Short: "subscribe to incoming messages from signald",
|
||||
Long: `subscribe to incoming messages from signald.
|
||||
|
||||
Currently the only output option is json`,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
if accountIdentifier == "" {
|
||||
accountIdentifier = config.Config.DefaultAccount
|
||||
}
|
||||
if accountIdentifier == "" {
|
||||
common.Must(cmd.Help())
|
||||
log.Fatal("No account specified. Please specify with --account or set a default")
|
||||
}
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
incoming := make(chan client_protocol.BasicResponse)
|
||||
go common.Signald.Listen(incoming)
|
||||
|
||||
req := v1.SubscribeRequest{Account: accountIdentifier}
|
||||
err := req.Submit(common.Signald)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for msg := range incoming {
|
||||
switch common.OutputFormat {
|
||||
case common.OutputFormatJSON, common.OutputFormatDefault:
|
||||
err := json.NewEncoder(os.Stdout).Encode(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
default:
|
||||
log.Fatal("unsupported output format")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
subscribeCmd.Flags().StringVarP(&accountIdentifier, "account", "a", "", "the signald account to use")
|
||||
RootCmd.AddCommand(subscribeCmd)
|
||||
}
|
|
@ -5,10 +5,10 @@ import (
|
|||
)
|
||||
|
||||
type BasicResponse struct {
|
||||
ID string
|
||||
Type string
|
||||
ErrorType string
|
||||
Error json.RawMessage
|
||||
Data json.RawMessage
|
||||
Account string
|
||||
ID string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
ErrorType string `json:"error_type,omitempty"`
|
||||
Error json.RawMessage `json:"error,omitempty"`
|
||||
Data json.RawMessage `json:"data,omitempty"`
|
||||
Account string `json:"account,omitempty"`
|
||||
}
|
||||
|
|
|
@ -125,7 +125,7 @@ func (s *Signald) Listen(c chan client_protocol.BasicResponse) {
|
|||
subscribers <- msg
|
||||
}
|
||||
|
||||
if c != nil && msg.ID == "" && msg.Type != "version" {
|
||||
if c != nil && !(msg.ID == "" && msg.Type == "version") {
|
||||
c <- msg
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue