|
1 | 1 | // Copyright 2025 HP Development Company, L.P. |
2 | 2 | // SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | | -package cmd |
| 4 | +// Package pubsub demonstrates adding a custom extension command to tcli |
| 5 | +// without vendoring or copying files into the tcli module itself. It |
| 6 | +// imports tcli's public pkg/cmd package and self-registers via |
| 7 | +// cmd.RegisterCommand. Consumers only need to blank-import this package |
| 8 | +// (e.g. `import _ "example.com/myclient/pubsub"`) from their main so its |
| 9 | +// init() runs. |
| 10 | +package pubsub |
5 | 11 |
|
6 | 12 | import ( |
7 | | - "log" |
| 13 | + "fmt" |
8 | 14 |
|
9 | | - "github.com/hpinc/tcli/internal/common" |
| 15 | + pubsubcommon "github.com/hpinc/tcli/examples/_pubsub/common" |
| 16 | + "github.com/hpinc/tcli/pkg/cmd" |
| 17 | + tcommon "github.com/hpinc/tcli/pkg/common" |
10 | 18 | ) |
11 | 19 |
|
12 | 20 | func init() { |
13 | | - cmds["sqs"] = &SqsCommand{} |
| 21 | + cmd.RegisterCommand("sqs", &SqsCommand{}) |
14 | 22 | } |
15 | 23 |
|
16 | 24 | type SqsCommand struct { |
17 | | - CmdBase |
| 25 | + cmd.CmdBase |
18 | 26 | Data string |
19 | 27 | Url string |
20 | 28 | Endpoint string |
21 | 29 | } |
22 | 30 |
|
23 | | -func (c *SqsCommand) Init(p *ParseResult) Command { |
| 31 | +func (c *SqsCommand) Init(p *cmd.ParseResult) cmd.Command { |
24 | 32 | k := SqsCommand{} |
25 | 33 | k.Endpoint = *p.Values["endpoint"] |
26 | 34 | k.Data = *p.Values["data"] |
27 | 35 | k.Url = *p.Values["queue_url"] |
28 | | - k.baseInit(p, k.send) |
| 36 | + k.InitBase(p, k.send) |
29 | 37 | return &k |
30 | 38 | } |
31 | 39 |
|
32 | | -// Function to publish the event to SNS |
| 40 | +// Function to publish the event to SQS |
33 | 41 | func (c *SqsCommand) send() error { |
34 | | - data := common.GetJsonString(c.Data) |
35 | | - cli, err := common.NewSQSClient(c.Endpoint) |
| 42 | + data := tcommon.GetJsonString(c.Data) |
| 43 | + cli, err := pubsubcommon.NewSQSClient(c.Endpoint) |
36 | 44 | if err != nil { |
37 | | - log.Fatalf("Publish to sqs failed: %v\n", err) |
| 45 | + return fmt.Errorf("publish to sqs failed: %w", err) |
38 | 46 | } |
39 | | - err = cli.Send(c.Url, data) |
40 | | - if err != nil { |
41 | | - log.Fatalf("Publish to sqs failed: %v\n", err) |
| 47 | + if err := cli.Send(c.Url, data); err != nil { |
| 48 | + return fmt.Errorf("publish to sqs failed: %w", err) |
42 | 49 | } |
43 | 50 | return nil |
44 | 51 | } |
0 commit comments