2022-10-20 19:59:46 +00:00
|
|
|
package main
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-26 22:37:03 +00:00
|
|
|
"context"
|
2024-11-09 16:39:49 +00:00
|
|
|
"errors"
|
2021-04-20 21:31:37 +00:00
|
|
|
"fmt"
|
2024-11-14 20:49:35 +00:00
|
|
|
"io"
|
2024-07-06 12:26:06 +00:00
|
|
|
"isle/daemon"
|
2024-11-14 20:49:35 +00:00
|
|
|
"isle/daemon/jsonrpc2"
|
2024-11-09 16:39:49 +00:00
|
|
|
"isle/jsonutil"
|
2024-11-19 13:12:17 +00:00
|
|
|
"isle/toolkit"
|
2021-04-20 21:31:37 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2024-06-22 15:49:56 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2021-04-20 21:31:37 +00:00
|
|
|
"github.com/spf13/pflag"
|
2024-11-09 16:39:49 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type subCmd struct {
|
2024-07-12 14:13:44 +00:00
|
|
|
name string
|
|
|
|
descr string
|
|
|
|
do func(subCmdCtx) error
|
2024-07-22 13:52:51 +00:00
|
|
|
|
|
|
|
// If set then the name will be allowed to be suffixed with this string.
|
|
|
|
plural string
|
2024-09-23 18:50:45 +00:00
|
|
|
|
|
|
|
// noNetwork, if true, means the call doesn't require a network to be
|
|
|
|
// specified on the command-line if there are more than one networks
|
|
|
|
// configured.
|
|
|
|
noNetwork bool
|
|
|
|
|
|
|
|
// Extra arguments on the command-line will be passed through to some
|
|
|
|
// underlying command.
|
|
|
|
passthroughArgs bool
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
type subCmdCtxOpts struct {
|
2024-09-23 18:50:45 +00:00
|
|
|
args []string // command-line arguments, excluding the subCmd itself.
|
|
|
|
subCmdNames []string // names of subCmds so far, including this one
|
2024-11-14 20:49:35 +00:00
|
|
|
daemonRPC daemon.RPC
|
|
|
|
stdout io.Writer
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
func (o *subCmdCtxOpts) withDefaults() *subCmdCtxOpts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(subCmdCtxOpts)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
2024-09-23 18:50:45 +00:00
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
if o.args == nil {
|
|
|
|
o.args = os.Args[1:]
|
2024-11-09 16:08:39 +00:00
|
|
|
}
|
2024-09-23 18:50:45 +00:00
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
if o.stdout == nil {
|
|
|
|
o.stdout = os.Stdout
|
2024-09-23 18:50:45 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
// subCmdCtx contains all information available to a subCmd's do method.
|
|
|
|
type subCmdCtx struct {
|
|
|
|
context.Context
|
|
|
|
logger *mlog.Logger
|
|
|
|
subCmd subCmd // the subCmd itself
|
|
|
|
opts *subCmdCtxOpts
|
|
|
|
|
|
|
|
flags *pflag.FlagSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubCmdCtx(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
|
|
|
subCmd subCmd,
|
|
|
|
opts *subCmdCtxOpts,
|
|
|
|
) subCmdCtx {
|
|
|
|
opts = opts.withDefaults()
|
2024-11-09 16:08:39 +00:00
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
return subCmdCtx{
|
2024-11-14 20:49:35 +00:00
|
|
|
Context: ctx,
|
|
|
|
logger: logger,
|
|
|
|
subCmd: subCmd,
|
|
|
|
opts: opts,
|
|
|
|
flags: pflag.NewFlagSet(subCmd.name, pflag.ExitOnError),
|
2024-09-23 18:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func usagePrefix(subCmdNames []string) string {
|
|
|
|
subCmdNamesStr := strings.Join(subCmdNames, " ")
|
|
|
|
if subCmdNamesStr != "" {
|
|
|
|
subCmdNamesStr += " "
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("\nUSAGE: %s %s", os.Args[0], subCmdNamesStr)
|
|
|
|
}
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
func (ctx subCmdCtx) getDaemonRPC() daemon.RPC {
|
|
|
|
if ctx.opts.daemonRPC == nil {
|
2024-11-19 13:12:17 +00:00
|
|
|
// TODO Close is not being called on the HTTPClient
|
|
|
|
httpClient, baseURL := toolkit.NewUnixHTTPClient(
|
|
|
|
ctx.logger.WithNamespace("http-client"),
|
|
|
|
daemon.HTTPSocketPath(),
|
|
|
|
)
|
|
|
|
|
|
|
|
baseURL.Path = daemonHTTPRPCPath
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
ctx.opts.daemonRPC = daemon.RPCFromClient(
|
2024-11-19 13:12:17 +00:00
|
|
|
jsonrpc2.NewHTTPClient(httpClient, baseURL.String()),
|
2024-11-14 20:49:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return ctx.opts.daemonRPC
|
2024-11-09 16:08:39 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
func (ctx subCmdCtx) withParsedFlags() (subCmdCtx, error) {
|
2024-11-14 20:49:35 +00:00
|
|
|
logLevel := logLevelFlag{mlog.LevelInfo}
|
|
|
|
ctx.flags.VarP(
|
|
|
|
&logLevel,
|
|
|
|
"log-level", "l",
|
|
|
|
"Maximum log level to output. Can be DEBUG, CHILD, INFO, WARN, ERROR, or FATAL.",
|
|
|
|
)
|
|
|
|
|
|
|
|
var network string
|
|
|
|
if !ctx.subCmd.noNetwork {
|
|
|
|
ctx.flags.StringVar(
|
|
|
|
&network,
|
|
|
|
"network", "",
|
|
|
|
"Which network to perform the command against, if more than one is joined. Can be an ID, name, or domain.",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
ctx.flags.VisitAll(func(f *pflag.Flag) {
|
|
|
|
if f.Shorthand == "h" {
|
|
|
|
panic(fmt.Sprintf("flag %+v has reserved shorthand `-h`", f))
|
|
|
|
}
|
|
|
|
if f.Name == "help" {
|
|
|
|
panic(fmt.Sprintf("flag %+v has reserved name `--help`", f))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
ctx.flags.Usage = func() {
|
|
|
|
var passthroughStr string
|
|
|
|
if ctx.subCmd.passthroughArgs {
|
|
|
|
passthroughStr = " [--] [args...]"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(
|
|
|
|
os.Stderr, "%s[-h|--help] [%s flags...]%s\n\n",
|
|
|
|
usagePrefix(ctx.opts.subCmdNames), ctx.subCmd.name, passthroughStr,
|
|
|
|
)
|
|
|
|
fmt.Fprintf(
|
|
|
|
os.Stderr, "%s FLAGS:\n\n", strings.ToUpper(ctx.subCmd.name),
|
|
|
|
)
|
|
|
|
fmt.Fprintln(os.Stderr, ctx.flags.FlagUsages())
|
|
|
|
|
|
|
|
os.Stderr.Sync()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.flags.Parse(ctx.opts.args); err != nil {
|
2024-09-23 18:50:45 +00:00
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
ctx.Context = daemon.WithNetwork(ctx.Context, network)
|
|
|
|
ctx.logger = ctx.logger.WithMaxLevel(logLevel.Int())
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
return ctx, nil
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error {
|
|
|
|
|
|
|
|
printUsageExit := func(subCmdName string) {
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "unknown sub-command %q\n", subCmdName)
|
|
|
|
|
|
|
|
fmt.Fprintf(
|
|
|
|
os.Stderr,
|
|
|
|
"%s<subCmd> [-h|--help] [sub-command flags...]\n",
|
2024-11-14 20:49:35 +00:00
|
|
|
usagePrefix(ctx.opts.subCmdNames),
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "\nSUB-COMMANDS:\n\n")
|
|
|
|
|
|
|
|
for _, subCmd := range subCmds {
|
2024-07-22 13:52:51 +00:00
|
|
|
name := subCmd.name
|
|
|
|
if subCmd.plural != "" {
|
|
|
|
name += "(" + subCmd.plural + ")"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, " %s\t%s\n", name, subCmd.descr)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "\n")
|
|
|
|
os.Stderr.Sync()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
args := ctx.opts.args
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
printUsageExit("")
|
|
|
|
}
|
|
|
|
|
|
|
|
subCmdsMap := map[string]subCmd{}
|
|
|
|
for _, subCmd := range subCmds {
|
|
|
|
subCmdsMap[subCmd.name] = subCmd
|
2024-07-22 13:52:51 +00:00
|
|
|
if subCmd.plural != "" {
|
|
|
|
subCmdsMap[subCmd.name+subCmd.plural] = subCmd
|
|
|
|
}
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subCmdName, args := args[0], args[1:]
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
subCmd, ok := subCmdsMap[subCmdName]
|
2021-04-20 21:31:37 +00:00
|
|
|
if !ok {
|
|
|
|
printUsageExit(subCmdName)
|
|
|
|
}
|
|
|
|
|
2024-11-14 20:49:35 +00:00
|
|
|
nextSubCmdCtxOpts := *ctx.opts
|
|
|
|
nextSubCmdCtxOpts.args = args
|
|
|
|
nextSubCmdCtxOpts.subCmdNames = append(ctx.opts.subCmdNames, subCmdName)
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
nextSubCmdCtx := newSubCmdCtx(
|
2024-11-14 20:49:35 +00:00
|
|
|
ctx.Context, ctx.logger, subCmd, &nextSubCmdCtxOpts,
|
2024-06-23 12:37:10 +00:00
|
|
|
)
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
if err := subCmd.do(nextSubCmdCtx); err != nil {
|
2021-04-20 21:31:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-09 16:39:49 +00:00
|
|
|
|
|
|
|
type outputFormat string
|
|
|
|
|
|
|
|
func (f outputFormat) MarshalText() ([]byte, error) { return []byte(f), nil }
|
|
|
|
|
|
|
|
func (f *outputFormat) UnmarshalText(b []byte) error {
|
|
|
|
*f = outputFormat(strings.ToLower(string(b)))
|
|
|
|
switch *f {
|
|
|
|
case "json", "yaml":
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("invalid output format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// doWithOutput wraps a subCmd's do function so that it will output some value
|
|
|
|
// to stdout. The value will be formatted according to a command-line argument.
|
|
|
|
func doWithOutput(fn func(subCmdCtx) (any, error)) func(subCmdCtx) error {
|
|
|
|
return func(ctx subCmdCtx) error {
|
|
|
|
type outputFormatFlag = textUnmarshalerFlag[outputFormat, *outputFormat]
|
|
|
|
|
|
|
|
outputFormat := outputFormatFlag{"yaml"}
|
|
|
|
ctx.flags.Var(
|
|
|
|
&outputFormat,
|
|
|
|
"format",
|
|
|
|
"How to format the output value. Can be 'json' or 'yaml'.",
|
|
|
|
)
|
|
|
|
|
|
|
|
res, err := fn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch outputFormat.V {
|
|
|
|
case "json":
|
2024-11-14 20:49:35 +00:00
|
|
|
return jsonutil.WriteIndented(ctx.opts.stdout, res)
|
2024-11-09 16:39:49 +00:00
|
|
|
case "yaml":
|
2024-11-14 20:49:35 +00:00
|
|
|
return yaml.NewEncoder(ctx.opts.stdout).Encode(res)
|
2024-11-09 16:39:49 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected outputFormat %q", outputFormat))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|