91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
)
|
|
|
|
type appConfiguration struct {
|
|
Greeting string `yaml:"greeting"`
|
|
}
|
|
|
|
type greetInput struct {
|
|
Count commandkit.Supplied[int]
|
|
Tags commandkit.Supplied[[]string]
|
|
}
|
|
|
|
func loadAppConfiguration(context.Context, commandkit.Selection, struct{}) (*configkit.Result[appConfiguration], error) {
|
|
return configkit.Load[appConfiguration](configkit.Bytes("simple example", []byte("greeting: hello\n")))
|
|
}
|
|
|
|
func newApp() (*commandkit.App, error) {
|
|
app, err := commandkit.New(commandkit.Identity{Name: "simple-example", Version: "1.0"})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
scope, err := commandkit.NewConfigKitScope[struct{}, appConfiguration]("app", "Application settings.", loadAppConfiguration)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := commandkit.RegisterConfig(app, commandkit.ConfigSuite[struct{}]{Scopes: []commandkit.ConfigScope[struct{}]{scope}}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
countDefault := 1
|
|
tagDefaults := []string{"example"}
|
|
greet, err := commandkit.NewCommand(commandkit.CommandSpec[greetInput]{
|
|
Name: "greet",
|
|
Summary: "Show flag presence and repeated values.",
|
|
Flags: []commandkit.Flag[greetInput]{
|
|
commandkit.ValueFlag[greetInput](commandkit.FlagMeta{Name: "count", Usage: "Greeting count."}, &countDefault, strconv.Atoi, func(input *greetInput, value commandkit.Supplied[int]) {
|
|
input.Count = value
|
|
}),
|
|
commandkit.RepeatFlag[greetInput](commandkit.FlagMeta{Name: "tag", Usage: "A repeatable label."}, tagDefaults, func(value string) (string, error) { return value, nil }, func(input *greetInput, value commandkit.Supplied[[]string]) {
|
|
input.Tags = value
|
|
}),
|
|
},
|
|
Run: func(invocation commandkit.Invocation, input greetInput) commandkit.Outcome {
|
|
configuration, err := loadAppConfiguration(invocation.Context, commandkit.Selection{}, struct{}{})
|
|
if err != nil {
|
|
return commandkit.Failure(err)
|
|
}
|
|
count := fmt.Sprintf("default %d (not supplied)", input.Count.Value)
|
|
if input.Count.Set {
|
|
count = fmt.Sprintf("explicit %d", input.Count.Value)
|
|
}
|
|
tags := fmt.Sprintf("default %v (not supplied)", input.Tags.Value)
|
|
if input.Tags.Set {
|
|
tags = fmt.Sprintf("explicit %v", input.Tags.Value)
|
|
}
|
|
if _, err := fmt.Fprintf(invocation.Streams.Out, "%s; count=%s; tags=%s\n", configuration.Config.Greeting, count, tags); err != nil {
|
|
return commandkit.Failure(err)
|
|
}
|
|
return commandkit.Success()
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := app.Register(greet); err != nil {
|
|
return nil, err
|
|
}
|
|
return app, nil
|
|
}
|
|
|
|
func main() {
|
|
app, err := newApp()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
outcome := app.Execute(context.Background(), os.Args[1:], commandkit.Streams{Out: os.Stdout, Err: os.Stderr})
|
|
if outcome.Status != 0 {
|
|
os.Exit(outcome.Status)
|
|
}
|
|
}
|