/
trigger.go
105 lines (84 loc) · 2.49 KB
/
trigger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package commands
import (
"fmt"
"github.com/rzp-gt/razorpayx-cli/internal/ansi"
"github.com/rzp-gt/razorpayx-cli/internal/client"
"github.com/rzp-gt/razorpayx-cli/internal/fixtures"
"github.com/rzp-gt/razorpayx-cli/internal/validators"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"strings"
)
type triggerCmd struct {
cmd *cobra.Command
fs afero.Fs
apiBaseURL string
}
func newTriggerCmd() *triggerCmd {
tc := &triggerCmd{}
tc.fs = afero.NewOsFs()
msg := fmt.Sprintf("Trigger specific webhook events to be sent. Webhooks events created through \n"+
"the trigger command will also create all necessary side-effect events that are \n"+
"needed to create the triggered event as well as the corresponding API objects. \n \n"+
"%s \n"+
"%s \n", "Supported events:",
fixtures.EventList())
tc.cmd = &cobra.Command{
Use: "trigger <event>",
Args: validators.MaximumNArgs(1),
ValidArgs: fixtures.EventNames(),
Short: ansi.ColoredBoldStatus("Trigger test webhook events"),
Long: ansi.ColoredBoldStatus(msg),
Example: `RazorpayX trigger payout.created`,
RunE: tc.runTriggerCmd,
}
// Hidden configuration flags, useful for dev/debugging
tc.cmd.Flags().StringVar(&tc.apiBaseURL, "api-base", client.DefaultAPIBaseURL, "Sets the API base URL")
tc.cmd.Flags().MarkHidden("api-base") // #nosec G104
return tc
}
func (tc *triggerCmd) runTriggerCmd(cmd *cobra.Command, args []string) error {
apiKey, err := Config.Profile.GetAPIKey(false)
if err != nil {
return err
}
apiSecret, err := Config.Profile.GetAPISecret(false)
if err != nil {
return err
}
if len(args) == 0 {
cmd.Help()
return nil
}
event := args[0]
res := (strings.Contains(args[0], "checklist") || strings.Contains(args[0], "status"))
url := tc.apiBaseURL
if res {
url = "http://192.168.0.103:8000"
apiKey = "rzp_test_1DP5mmOlF5G5ag"
apiSecret = "thisissupersecret"
}
var fixture *fixtures.Fixture
if file, ok := fixtures.Events[event]; ok {
fixture, err = fixtures.BuildFromFixture(tc.fs, apiKey, apiSecret, url, file)
if err != nil {
return err
}
} else {
exists, _ := afero.Exists(tc.fs, event)
if !exists {
return fmt.Errorf(fmt.Sprintf("event %s is not supported.", event))
}
fixture, err = fixtures.BuildFromFixture(tc.fs, apiKey, apiSecret, tc.apiBaseURL, event)
if err != nil {
return err
}
}
err = fixture.Execute()
if err == nil {
fmt.Println("Trigger succeeded!")
} else {
fmt.Printf("Trigger failed: %s\n", err)
}
return err
}