-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
464 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package notify | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
"time" | ||
|
||
"github.com/resonatehq/resonate/pkg/client" | ||
v1 "github.com/resonatehq/resonate/pkg/client/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var notifyExample = ` | ||
# Create a notify | ||
resonate notify create foo --promise-id bar --timeout 1h --recv default | ||
# Create a notify with url | ||
resonate notify create foo --promise-id bar --timeout 1h --recv poll://default/6fa89b7e-4a56-40e8-ba4e-78864caa3278 | ||
# Create a notify with object | ||
resonate notify create foo --promise-id bar --timeout 1h --recv {"type": "poll", "data": {"group": "default", "id": "6fa89b7e-4a56-40e8-ba4e-78864caa3278"}} | ||
` | ||
|
||
func CreateNotifyCmd(c client.Client) *cobra.Command { | ||
var ( | ||
promiseId string | ||
timeout time.Duration | ||
recvStr string | ||
) | ||
cmd := &cobra.Command{ | ||
Use: "create <id>", | ||
Short: "Create notify", | ||
Example: notifyExample, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return errors.New("must specify an id") | ||
} | ||
|
||
id := args[0] | ||
|
||
var recv v1.Recv | ||
|
||
if json.Valid([]byte(recvStr)) { | ||
var recv0 v1.Recv0 | ||
|
||
if err := json.Unmarshal([]byte(recvStr), &recv0); err != nil { | ||
return err | ||
} | ||
if err := recv.FromRecv0(recv0); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := recv.FromRecv1(recvStr); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
body := v1.CreateNotifyJSONRequestBody{ | ||
Id: id, | ||
PromiseId: promiseId, | ||
Timeout: time.Now().Add(timeout).UnixMilli(), | ||
Recv: recv, | ||
} | ||
|
||
res, err := c.V1().CreateNotifyWithResponse(context.TODO(), nil, body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if res.StatusCode() == 201 { | ||
cmd.Printf("Created callback: %s\n", id) | ||
} else if res.StatusCode() == 200 { | ||
if res.JSON200.Promise != nil && res.JSON200.Promise.State != v1.PromiseStatePENDING { | ||
cmd.Printf("Promise %s already %s\n", promiseId, strings.ToLower(string(res.JSON200.Promise.State))) | ||
} else { | ||
cmd.Printf("Created callback: %s (deduplicated)\n", id) | ||
} | ||
} else { | ||
cmd.PrintErrln(res.Status(), string(res.Body)) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&promiseId, "promise-id", "", "promise id") | ||
cmd.Flags().DurationVar(&timeout, "timeout", 0, "task timeout") | ||
cmd.Flags().StringVar(&recvStr, "recv", "default", "task receiver") | ||
|
||
_ = cmd.MarkFlagRequired("promise-id") | ||
_ = cmd.MarkFlagRequired("timeout") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package notify | ||
|
||
import ( | ||
"github.com/resonatehq/resonate/pkg/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
var ( | ||
c = client.New() | ||
server string | ||
username string | ||
password string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "notifications", | ||
Aliases: []string{"notifications"}, | ||
Short: "Resonate notifications", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
_ = cmd.Help() | ||
}, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
if username != "" || password != "" { | ||
c.SetBasicAuth(username, password) | ||
} | ||
|
||
return c.Setup(server) | ||
}, | ||
} | ||
|
||
// Add subcommands | ||
cmd.AddCommand(CreateNotifyCmd(c)) | ||
|
||
// Flags | ||
cmd.PersistentFlags().StringVarP(&server, "server", "", "http://localhost:8001", "resonate url") | ||
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "basic auth username") | ||
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "basic auth password") | ||
|
||
return cmd | ||
} |
Oops, something went wrong.