2026-10-07 14:56:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
2026-10-11 01:50:48 +00:00
|
|
|
"gitea.maximumdirect.net/eric/CommandKit/integrations/ckconfig"
|
2026-10-07 14:56:13 +00:00
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type appConfiguration struct {
|
|
|
|
|
Greeting string `yaml:"greeting"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type greetInput struct {
|
|
|
|
|
Count commandkit.Supplied[int]
|
|
|
|
|
Tags commandkit.Supplied[[]string]
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 01:50:48 +00:00
|
|
|
func loadAppConfiguration(_ context.Context, _ ckconfig.Selection) (*configkit.Result[appConfiguration], error) {
|
|
|
|
|
return configkit.Load[appConfiguration](configkit.Mapping("simple application settings", map[string]any{
|
|
|
|
|
"greeting": "Hello",
|
|
|
|
|
}))
|
2026-10-07 14:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newApp() (*commandkit.App, error) {
|
|
|
|
|
countDefault := 1
|
|
|
|
|
tagDefaults := []string{"example"}
|
2026-10-11 01:50:48 +00:00
|
|
|
greet := commandkit.NewCommand(commandkit.CommandSpec[greetInput]{
|
2026-10-07 14:56:13 +00:00
|
|
|
Name: "greet",
|
|
|
|
|
Summary: "Show flag presence and repeated values.",
|
|
|
|
|
Flags: []commandkit.Flag[greetInput]{
|
2026-10-11 01:50:48 +00:00
|
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "count", Usage: "Greeting count."}, &countDefault, strconv.Atoi, func(input *greetInput, value commandkit.Supplied[int]) {
|
2026-10-07 14:56:13 +00:00
|
|
|
input.Count = value
|
|
|
|
|
}),
|
2026-10-11 01:50:48 +00:00
|
|
|
commandkit.RepeatFlag(commandkit.FlagMeta{Name: "tag", Usage: "A repeatable label."}, tagDefaults, func(value string) (string, error) { return value, nil }, func(input *greetInput, value commandkit.Supplied[[]string]) {
|
2026-10-07 14:56:13 +00:00
|
|
|
input.Tags = value
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
Run: func(invocation commandkit.Invocation, input greetInput) commandkit.Outcome {
|
2026-10-11 01:50:48 +00:00
|
|
|
configuration, err := loadAppConfiguration(invocation.Context, ckconfig.Selection{})
|
2026-10-07 14:56:13 +00:00
|
|
|
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()
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-10-11 01:50:48 +00:00
|
|
|
return commandkit.New(
|
|
|
|
|
commandkit.Identity{Name: "simple-example", Version: "1.0"},
|
|
|
|
|
ckconfig.AppCommand(loadAppConfiguration),
|
|
|
|
|
greet,
|
|
|
|
|
)
|
2026-10-07 14:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|