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"
|
2021-04-20 21:31:37 +00:00
|
|
|
"fmt"
|
2024-07-06 12:26:06 +00:00
|
|
|
"isle/daemon"
|
2024-06-23 12:37:10 +00:00
|
|
|
"isle/daemon/jsonrpc2"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// subCmdCtx contains all information available to a subCmd's do method.
|
|
|
|
type subCmdCtx struct {
|
|
|
|
subCmd subCmd // the subCmd itself
|
|
|
|
args []string // command-line arguments, excluding the subCmd itself.
|
|
|
|
subCmdNames []string // names of subCmds so far, including this one
|
2022-10-26 22:37:03 +00:00
|
|
|
|
2024-06-23 12:37:10 +00:00
|
|
|
ctx context.Context
|
|
|
|
logger *mlog.Logger
|
|
|
|
daemonRCPClient jsonrpc2.Client
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type subCmd struct {
|
|
|
|
name string
|
|
|
|
descr string
|
|
|
|
checkLock bool
|
|
|
|
do func(subCmdCtx) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx subCmdCtx) usagePrefix() string {
|
|
|
|
|
|
|
|
subCmdNamesStr := strings.Join(ctx.subCmdNames, " ")
|
|
|
|
if subCmdNamesStr != "" {
|
|
|
|
subCmdNamesStr += " "
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("\nUSAGE: %s %s", os.Args[0], subCmdNamesStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx subCmdCtx) flagSet(withPassthrough bool) *pflag.FlagSet {
|
|
|
|
flags := pflag.NewFlagSet(ctx.subCmd.name, pflag.ExitOnError)
|
|
|
|
flags.Usage = func() {
|
|
|
|
|
|
|
|
var passthroughStr string
|
|
|
|
if withPassthrough {
|
|
|
|
passthroughStr = " [--] [args...]"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(
|
|
|
|
os.Stderr, "%s[-h|--help] [%s flags...]%s\n\n",
|
|
|
|
ctx.usagePrefix(), ctx.subCmd.name, passthroughStr,
|
|
|
|
)
|
|
|
|
fmt.Fprintf(os.Stderr, "%s FLAGS:\n\n", strings.ToUpper(ctx.subCmd.name))
|
|
|
|
fmt.Fprintln(os.Stderr, flags.FlagUsages())
|
|
|
|
|
|
|
|
os.Stderr.Sync()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
ctx.usagePrefix(),
|
|
|
|
)
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "\nSUB-COMMANDS:\n\n")
|
|
|
|
|
|
|
|
for _, subCmd := range subCmds {
|
|
|
|
fmt.Fprintf(os.Stderr, " %s\t%s\n", subCmd.name, subCmd.descr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "\n")
|
|
|
|
os.Stderr.Sync()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := ctx.args
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
printUsageExit("")
|
|
|
|
}
|
|
|
|
|
|
|
|
subCmdsMap := map[string]subCmd{}
|
|
|
|
for _, subCmd := range subCmds {
|
|
|
|
subCmdsMap[subCmd.name] = subCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
subCmdName, args := args[0], args[1:]
|
|
|
|
subCmd, ok := subCmdsMap[subCmdName]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
printUsageExit(subCmdName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subCmd.checkLock {
|
|
|
|
|
2022-10-26 22:45:40 +00:00
|
|
|
if err := assertLock(); err != nil {
|
2021-04-20 21:31:37 +00:00
|
|
|
return fmt.Errorf("checking lock file: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 12:37:10 +00:00
|
|
|
daemonRCPClient := jsonrpc2.NewUnixHTTPClient(
|
2024-07-06 12:26:06 +00:00
|
|
|
daemon.HTTPSocketPath(), daemonHTTPRPCPath,
|
2024-06-23 12:37:10 +00:00
|
|
|
)
|
|
|
|
|
2021-04-20 21:31:37 +00:00
|
|
|
err := subCmd.do(subCmdCtx{
|
2024-06-23 12:37:10 +00:00
|
|
|
subCmd: subCmd,
|
|
|
|
args: args,
|
|
|
|
subCmdNames: append(ctx.subCmdNames, subCmdName),
|
|
|
|
ctx: ctx.ctx,
|
|
|
|
logger: ctx.logger,
|
|
|
|
daemonRCPClient: daemonRCPClient,
|
2021-04-20 21:31:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|