https://github.com/treblle/treblle
A framework enabling Go developers to easily create CLI applications.
https://github.com/treblle/treblle
application c cli cli-app framework go golang golang-application
Last synced: 7 months ago
JSON representation
A framework enabling Go developers to easily create CLI applications.
- Host: GitHub
- URL: https://github.com/treblle/treblle
- Owner: Treblle
- Created: 2023-08-27T16:48:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-13T14:15:50.000Z (about 2 years ago)
- Last Synced: 2025-04-10T14:45:40.052Z (12 months ago)
- Topics: application, c, cli, cli-app, framework, go, golang, golang-application
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 6
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A framework for building CLI applications fluently in Go.
> This is currently unreleased, and is a work in progress. Breaking Changes are expected.
## Usage
To start you need to create your `main.go` and create a new application
```go
const (
AppDir = "/.your-app"
)
func main() {
app := app.New(
"CLI Name", // The Name of your CLI Application
AppDir, // The path within ~/.config to create CLI configuration
1000, // The number of parallel workers you want to be available for dispatching tasks
)
app.Boot() // This will boot the application, setting the storage path for this run.
// If you need to load your CLI config from a file you can do that using a fluent interface
app.Config.LoadFromFile(app.Storage.Path + "/config.go")
}
```
Once you have a basic application set up, you can move onto registering your commands using the fluent builder interface
```go
func main() {
... other code goes here
RegisterCommands(app) // Register all the commands in your application
app.Console.RootCmd.Execute() // Execute your main command
}
func RegisterCommands(app *app.App) {
app.Console.Register(
console.
Build("test"). // The name of the command
Description("Test command"). // the description (short) for the command
Action(func(cmd *cobra.Command, args []string) {
log.Info("Test Command Called") // what you want this command to do
}
),
)
}
```
## Using the HTTP Client
```go
func main() {
... other code goes here
request := app.Http.Base("https://api.treblle.com/").WithToken("123123")
var message Message
response, err := request.Get("/")
if err != nil {
log.Errorf("Request error: %v\n", err)
os.Exit(1)
}
log.Infof("Success: %v", response.JSON(&message))
}
```
## Using the Applications Queue
You can start your applications queue runner, which will listen on a wait group for Jobs to be passed into it
```go
func main() {
... other code goes here
runner := app.Queue
runner.Run()
}
func SomeOtherMethod() {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
job := &ExampleJob{Message: fmt.Sprintf("Hello, I am job number %d", i)}
dispatcher.JobQueue <- job
}(i)
}
wg.Wait()
}
```
## Using the Event Dispatcher
```go
func main() {
... other code goes here
// Register listeners
app.Dispatcher.Event("UserRegistered").
Listen(func(data event.EventData) {
userData, _ := data.(map[string]string)
println("Send welcome email to:", userData["email"])
}).
Listen(func(data event.EventData) {
println("Log user registration to audit trail.")
})
}
func SomeOtherMethod() {
// Dispatch event
app.Dispatcher.Event("UserRegistered").Dispatch(map[string]string{
"email": "user@example.com",
"name": "John Doe",
})
}
```
## Examples
- [Simple Example](./examples/simple.go)
## Community 💙
First and foremost: **Star and watch this repository** to stay up-to-date.
Also, follow our [Blog](https://blog.treblle.com), and on [Twitter](https://twitter.com/treblleapi).
You can chat with the team and other members on [Discord](https://treblle.com/chat) and follow our tutorials and other video material at [YouTube](https://youtube.com/@treblle).
[](https://treblle.com/chat)
[](https://youtube.com/@treblle)
[](https://twitter.com/treblleapi)
### How to contribute
Here are some ways of contributing to making Treblle better:
- **[Try out Treblle](https://docs.treblle.com/en/introduction#getting-started)**, and let us know ways to make Treblle better for you. Let us know here on [Discord](https://treblle.com/chat).
- Join our [Discord](https://treblle.com/chat) and connect with other members to share and learn from.
- Send a pull request to any of our [open source repositories](https://github.com/Treblle) on Github. Check the contribution guide on the repo you want to contribute to for more details about how to contribute. We're looking forward to your contribution!