Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anchore/clio
An easy way to bootstrap your application with batteries included.
https://github.com/anchore/clio
bubbletea cli cobra event-bus go golang hacktoberfest logger viper
Last synced: 3 days ago
JSON representation
An easy way to bootstrap your application with batteries included.
- Host: GitHub
- URL: https://github.com/anchore/clio
- Owner: anchore
- License: apache-2.0
- Created: 2023-05-15T18:11:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-07T22:37:42.000Z (5 days ago)
- Last Synced: 2024-11-07T23:30:02.944Z (5 days ago)
- Topics: bubbletea, cli, cobra, event-bus, go, golang, hacktoberfest, logger, viper
- Language: Go
- Homepage:
- Size: 200 KB
- Stars: 5
- Watchers: 10
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CLIO
An easy way to bootstrap your application with batteries included.
## Status
***Consider this project to be in alpha. The API is not stable and may change at any time.***
## What is included?
- Pairs well with [cobra](github.com/spf13/cobra) and [viper](github.com/spf13/viper) via [fangs](github.com/anchore/fangs), covering CLI arg parsing and config file + env var loading.
- Provides an event bus via [partybus](github.com/wagoodman/go-partybus), enabling visibility deep in your execution stack as to what is happening.
- Provides a logger via the [logger interface](github.com/anchore/go-logger), allowing you to swap out for any concrete logger you'd like.
- Supplies a redactor object that can be used to remove sensitive output before it's exposed (in the log or elsewhere).
- Defines a generic UI interface that adapts well to TUI frameworks such as [bubbletea](github.com/charmbracelet/bubbletea).## Example
Here's a basic example of how to use clio + cobra to get a fully functional CLI application going:
```go
package mainimport (
"io"
"os""github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/wagoodman/go-partybus"
"github.com/anchore/clio"
"github.com/anchore/fangs"
)// Define your per-command or entire application config as a struct
type MyCommandConfig struct {
TimestampServer string `mapstructure:"timestamp-server"`
// ...
}// ... add cobra flags just as you are used to doing in any other cobra application
func (c *MyCommandConfig) AddFlags(flags fangs.FlagSet) {
flags.StringVarP(
&c.TimestampServer, "timestamp-server", "",
"URL to a timestamp server to use for timestamping the signature",
)
// ...
}func MyCommand(app clio.Application) *cobra.Command {
cfg := &MyCommandConfig{
TimestampServer: "https://somewhere.out/there", // a default value
}return app.SetupCommand(&cobra.Command{
Use: "my-command",
PreRunE: app.Setup(cfg),
RunE: func(cmd *cobra.Command, args []string) error {
// perform command functions here
return nil
},
}, cfg)
}func main() {
cfg := clio.NewSetupConfig(clio.Identification{
Name: "awesome",
Version: "v1.0.0",
})app := clio.New(*cfg)
root := app.SetupRootCommand(&cobra.Command{})root.AddCommand(MyCommand(app))
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
```