From 0f8b17383f00da4b33fda28090659fb4de66e207 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 24 Sep 2021 02:24:04 -0700 Subject: [PATCH] add a subcommand to get the current protocol document from a running signald instance --- cmd/signaldctl/cmd/protocol.go | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 cmd/signaldctl/cmd/protocol.go diff --git a/cmd/signaldctl/cmd/protocol.go b/cmd/signaldctl/cmd/protocol.go new file mode 100644 index 0000000..5a0db72 --- /dev/null +++ b/cmd/signaldctl/cmd/protocol.go @@ -0,0 +1,41 @@ +package cmd + +import ( + "fmt" + "log" + "os" + + "github.com/spf13/cobra" + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/signald" +) + +var protocolCmd = &cobra.Command{ + Use: "protocol", + Short: "return a machine-readable description of the signald client protocol. Details at https://signald.org/articles/socket-protocol/", + RunE: func(cmd *cobra.Command, args []string) error { + go common.Signald.Listen(nil) + + req := map[string]string{"version": "v1", "type": "protocol", "id": signald.GenerateID()} + err := common.Signald.RawRequest(req) + if err != nil { + log.Println("signald-go: error submitting request to signald") + return err + } + + responseChannel := common.Signald.GetResponseListener(req["id"]) + defer common.Signald.CloseResponseListener(req["id"]) + + rawResponse := <-responseChannel + if rawResponse.Error != nil { + return fmt.Errorf("signald error: %s", string(rawResponse.Error)) + } + + _, err = os.Stdout.Write(rawResponse.Data) + return err + }, +} + +func init() { + RootCmd.AddCommand(protocolCmd) +}