-
Notifications
You must be signed in to change notification settings - Fork 0
Changes to consume tcli as a library #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,45 +1,45 @@ | ||
| // Copyright 2025 HP Development Company, L.P. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
| package pubsub | ||
|
|
||
| import ( | ||
| "log" | ||
| "fmt" | ||
|
|
||
| "github.com/hpinc/tcli/internal/common" | ||
| pubsubcommon "github.com/hpinc/tcli/examples/_pubsub/common" | ||
| "github.com/hpinc/tcli/pkg/cmd" | ||
| ) | ||
|
|
||
| func init() { | ||
| cmds["sns"] = &SnsCommand{} | ||
| cmd.RegisterCommand("sns", &SnsCommand{}) | ||
| } | ||
|
|
||
| type SnsCommand struct { | ||
| CmdBase | ||
| cmd.CmdBase | ||
| Name string | ||
| Data string | ||
| Arn string | ||
| Endpoint string | ||
| } | ||
|
|
||
| func (c *SnsCommand) Init(p *ParseResult) Command { | ||
| func (c *SnsCommand) Init(p *cmd.ParseResult) cmd.Command { | ||
| k := SnsCommand{} | ||
| k.Endpoint = *p.Values["endpoint"] | ||
| k.Data = *p.Values["data"] | ||
| k.Arn = *p.Values["arn"] | ||
| k.Name = *p.Values["name"] | ||
| k.baseInit(p, k.send) | ||
| k.InitBase(p, k.send) | ||
| return &k | ||
| } | ||
|
|
||
| // Function to publish the event to SNS | ||
| func (c *SnsCommand) send() error { | ||
| cli, err := common.NewSNSClient(c.Endpoint) | ||
| cli, err := pubsubcommon.NewSNSClient(c.Endpoint) | ||
| if err != nil { | ||
| log.Fatalf("Publish to sns failed: %v\n", err) | ||
| return fmt.Errorf("publish to sns failed: %w", err) | ||
| } | ||
| err = cli.Publish(c.Arn, c.Data, c.Name) | ||
| if err != nil { | ||
| log.Fatalf("Publish to sns failed: %v\n", err) | ||
| if err := cli.Publish(c.Arn, c.Data, c.Name); err != nil { | ||
| return fmt.Errorf("publish to sns failed: %w", err) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,44 +1,51 @@ | ||
| // Copyright 2025 HP Development Company, L.P. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
| // Package pubsub demonstrates adding a custom extension command to tcli | ||
| // without vendoring or copying files into the tcli module itself. It | ||
| // imports tcli's public pkg/cmd package and self-registers via | ||
| // cmd.RegisterCommand. Consumers only need to blank-import this package | ||
| // (e.g. `import _ "example.com/myclient/pubsub"`) from their main so its | ||
| // init() runs. | ||
| package pubsub | ||
|
|
||
| import ( | ||
| "log" | ||
| "fmt" | ||
|
|
||
| "github.com/hpinc/tcli/internal/common" | ||
| pubsubcommon "github.com/hpinc/tcli/examples/_pubsub/common" | ||
| "github.com/hpinc/tcli/pkg/cmd" | ||
| tcommon "github.com/hpinc/tcli/pkg/common" | ||
| ) | ||
|
|
||
| func init() { | ||
| cmds["sqs"] = &SqsCommand{} | ||
| cmd.RegisterCommand("sqs", &SqsCommand{}) | ||
| } | ||
|
|
||
| type SqsCommand struct { | ||
| CmdBase | ||
| cmd.CmdBase | ||
| Data string | ||
| Url string | ||
| Endpoint string | ||
| } | ||
|
|
||
| func (c *SqsCommand) Init(p *ParseResult) Command { | ||
| func (c *SqsCommand) Init(p *cmd.ParseResult) cmd.Command { | ||
| k := SqsCommand{} | ||
| k.Endpoint = *p.Values["endpoint"] | ||
| k.Data = *p.Values["data"] | ||
| k.Url = *p.Values["queue_url"] | ||
| k.baseInit(p, k.send) | ||
| k.InitBase(p, k.send) | ||
| return &k | ||
| } | ||
|
|
||
| // Function to publish the event to SNS | ||
| // Function to publish the event to SQS | ||
| func (c *SqsCommand) send() error { | ||
| data := common.GetJsonString(c.Data) | ||
| cli, err := common.NewSQSClient(c.Endpoint) | ||
| data := tcommon.GetJsonString(c.Data) | ||
| cli, err := pubsubcommon.NewSQSClient(c.Endpoint) | ||
| if err != nil { | ||
| log.Fatalf("Publish to sqs failed: %v\n", err) | ||
| return fmt.Errorf("publish to sqs failed: %w", err) | ||
| } | ||
| err = cli.Send(c.Url, data) | ||
| if err != nil { | ||
| log.Fatalf("Publish to sqs failed: %v\n", err) | ||
| if err := cli.Send(c.Url, data); err != nil { | ||
| return fmt.Errorf("publish to sqs failed: %w", err) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/hpinc/tcli | ||
|
|
||
| go 1.26.4 | ||
| go 1.26.5 | ||
|
|
||
| require ( | ||
| github.com/itchyny/gojq v0.12.19 | ||
|
|
||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,49 @@ | ||
| // Copyright 2025 HP Development Company, L.P. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
|
|
||
| type CmdBase struct { | ||
| p *ParseResult | ||
| global *GlobalResult | ||
| runFunc fnRun | ||
| } | ||
|
|
||
| func (c *CmdBase) baseInit(p *ParseResult, f fnRun) { | ||
| c.p = p | ||
| c.global = p.Global | ||
| c.runFunc = f | ||
| } | ||
|
|
||
| // InitBase initializes a CmdBase from outside this package. Custom Command | ||
| // implementations registered via RegisterCommand should call this from | ||
| // their Init method instead of reimplementing CmdBase's plumbing: | ||
| // | ||
| // func (c *MyCommand) Init(p *cmd.ParseResult) cmd.Command { | ||
| // k := MyCommand{} | ||
| // k.InitBase(p, k.run) | ||
| // return &k | ||
| // } | ||
| func (c *CmdBase) InitBase(p *ParseResult, f func() error) { | ||
| c.baseInit(p, f) | ||
| } | ||
|
|
||
| // Params returns the parsed parameter values for this command, so external | ||
| // Command implementations can read parameters without needing direct | ||
| // access to CmdBase's unexported fields. | ||
| func (c *CmdBase) Params() *ParseResult { | ||
| return c.p | ||
| } | ||
|
|
||
| // Global returns the parsed global flag values for this command. | ||
| func (c *CmdBase) Global() *GlobalResult { | ||
| return c.global | ||
| } | ||
|
|
||
| func (c *CmdBase) GetBase() *CmdBase { | ||
| return c | ||
| } | ||
|
|
||
| func (c *CmdBase) Execute() error { | ||
| return nil | ||
| } |
This file contains hidden or 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,54 @@ | ||
| // Copyright 2025 HP Development Company, L.P. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/hpinc/tcli/pkg/common" | ||
| "github.com/hpinc/tcli/pkg/config" | ||
| ) | ||
|
|
||
| // hold commands in a module | ||
| type commands map[string]Command | ||
| type fnRun func() error | ||
|
|
||
| type Command interface { | ||
| Init(*ParseResult) Command | ||
| Execute() error | ||
| GetBase() *CmdBase | ||
| } | ||
|
|
||
| var ( | ||
| cmds = make(commands) | ||
| log = config.GetLogger() | ||
| ) | ||
|
|
||
| func GetCommand(name string) (Command, error) { | ||
| if c, ok := cmds[name]; ok { | ||
| return c, nil | ||
| } | ||
| return nil, common.ErrNoSuchCommand | ||
| } | ||
|
|
||
| // RegisterCommand registers a Command implementation under the given | ||
| // extension class name (the value returned by Method.GetExtensionClass(), | ||
| // which maps to the "class" field of an operation's extension in the | ||
| // OpenAPI spec). | ||
| // | ||
| // This lets a program that imports tcli as a library add its own command | ||
| // types (e.g. for publishing to a queue or topic) without needing to copy | ||
| // source files into this package. Call it from an init() in your own | ||
| // package, then blank-import that package from your main so the init() | ||
| // runs: | ||
| // | ||
| // package mypubsub | ||
| // | ||
| // func init() { | ||
| // cmd.RegisterCommand("sqs", &SqsCommand{}) | ||
| // } | ||
| // | ||
| // // in your main package | ||
| // import _ "example.com/myclient/mypubsub" | ||
| func RegisterCommand(name string, c Command) { | ||
| cmds[name] = c | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not able to understand the example of sns-sqs.
Please update example of petstore as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point.
I have updated the example from a new project perspective. A full working example will require the current changes to go in first as the dependencies will not properly resolve without these changes in main. I will provide steps to test locally here (this will not apply as is when this branch is merged so not making it part of README)
If you want to check out how this works locally, you will have to redirect the ref locally. Assuming you create a new go project and create
main.goas shown in example.Do the following to get that project in a testable state for pubsub extensions
edit go.mod to add the following (assumes you create example in the same parent folder as this project. otherwise, change redirect as applies)
run
go mod tidyand you should be able to rungo run main.gonow with proper config override.petstoredoes not need changes. It's usage example will continue to work without changes.Note that the usage as a lib is primarily aimed at extending
tclifunctionality via thex-extensionmechanism to support custom commands.