diff --git a/cmd/signaldctl/cmd/raw.go b/cmd/signaldctl/cmd/raw.go new file mode 100644 index 0000000..23cb9e0 --- /dev/null +++ b/cmd/signaldctl/cmd/raw.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "log" + "os" + + "github.com/spf13/cobra" + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/signald" +) + +var rawCmd = &cobra.Command{ + Use: "raw []", + Short: "make a request to signald", + Long: "make a raw request to signald, returning the result. If request data is not provided via command line argument labeled \"json\", it must be provided via stdin", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) < 3 { + return cmd.Help() + } + + var reqdata map[string]interface{} + if err := json.Unmarshal([]byte(args[2]), &reqdata); err != nil { + return err + } + + requestID := signald.GenerateID() + reqdata["id"] = requestID + reqdata["type"] = args[1] + reqdata["version"] = args[0] + + go common.Signald.Listen(nil) + + err := common.Signald.RawRequest(reqdata) + if err != nil { + log.Println("error submitting request to signald") + return err + } + + responseChannel := common.Signald.GetResponseListener(requestID) + defer common.Signald.CloseResponseListener(requestID) + + 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(rawCmd) +}