Files
base/cmd/cli.go
T
zeekayandHanzo Dev 21c8030e02 a daemon's name is the daemon's, so the CLI reads it rather than spelling it
The CLI printed a fixed name in its examples and help text while already
taking that name as a parameter, so `operator` and `rpc` now interpolate what
they were handed, the way `cluster` already did.

The config file's per-env URL block goes with it. Nothing read it: EnvURLs
computes a service address from the environment's domain suffix, which is
what every command actually calls, so the block was a second unasked answer
to where a service lives. An older config still loads — an unknown key is
ignored — and its env block is dropped on the next write. The default org is
no longer a name.

The Tasks queue is named for this repo rather than written out three times.

Fixture hostnames follow the shape the operator emits, which one of those
files was already using.

crdt/privacy_fhe.go cited a PDF that is not in the tree; the two figures it
backed are in the design doc it already points at.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-13 14:16:11 -07:00

111 lines
4.1 KiB
Go

package cmd
import (
"github.com/hanzoai/base/cmd/cli"
"github.com/spf13/cobra"
)
// Version is set at build time via ldflags.
var Version = "0.1.0"
// NewCLICommand returns the top-level Cobra command tree for the Base CLI
// client. These subcommands hit the running server's HTTP API, so they work
// against any daemon that embeds Base, by URL.
//
// Usage from a downstream daemon:
//
// rootCmd.AddCommand(cmd.NewCLICommand())
func NewCLICommand() *cobra.Command {
var flagURL string
var flagToken string
var flagBase string
var flagFormat string
var nf cli.NetworkFlags
// lazy constructors — resolved after flags are parsed
clientFn := func() *cli.Client {
return cli.NewClient(
cli.ResolveURL(flagURL),
cli.ResolveToken(flagToken),
flagBase,
)
}
formatFn := func() cli.Format {
return cli.DetectFormat(flagFormat)
}
root := &cobra.Command{
Use: "cli",
Short: "HTTP client commands against a running Base server",
Long: `CLI commands that hit the Base HTTP API.
Works against any daemon that embeds Base, by pointing --url
at the running instance.`,
}
root.PersistentFlags().StringVar(&flagURL, "url", "", "server URL (default $BASE_URL or http://127.0.0.1:8090)")
root.PersistentFlags().StringVar(&flagToken, "token", "", "auth token (default $BASE_TOKEN or ~/.config/base/token)")
root.PersistentFlags().StringVar(&flagBase, "base", "", "base org ID (sets X-Org-Id header)")
root.PersistentFlags().StringVar(&flagFormat, "format", "", "output format: table, json, yaml (default: table on tty, json otherwise)")
// Network flags: --mainnet, --testnet, --devnet, --dev
// Safe on the `cli` subtree (no collision with Base root --dev flag).
cli.AddNetworkFlags(root, &nf)
// Base CRUD commands
root.AddCommand(cli.NewCollectionCommand(clientFn, formatFn))
root.AddCommand(cli.NewRecordCommand(clientFn, formatFn))
root.AddCommand(cli.NewLoginCommand(clientFn, formatFn))
root.AddCommand(cli.NewWhoamiCommand(clientFn, formatFn))
root.AddCommand(cli.NewCronsCommand(clientFn, formatFn))
root.AddCommand(cli.NewDaemonCommand())
// Infrastructure commands (cluster, operator, config, status, self, rpc)
root.AddCommand(cli.NewClusterCommand(&nf))
root.AddCommand(cli.NewOperatorCommand(&nf))
root.AddCommand(cli.NewConfigCommand())
root.AddCommand(cli.NewStatusCommand(clientFn, formatFn, &nf))
root.AddCommand(cli.NewSelfCommand(Version))
root.AddCommand(cli.NewRPCCommand(clientFn, formatFn))
return root
}
// AddCLISubcommands registers the core Base CLI subcommands (collection,
// record, login, whoami, crons, daemon) directly onto the given parent
// command. Use this instead of NewCLICommand when you want the commands
// at root level (e.g. `base collection list` instead of `base cli collection list`).
//
// Does NOT add network flags (--mainnet, --testnet, --devnet, --dev) or
// infrastructure commands (cluster, operator, config, status, self, rpc)
// because the parent's root typically already has a --dev flag from Base.
func AddCLISubcommands(parent *cobra.Command) {
var flagURL string
var flagToken string
var flagBase string
var flagFormat string
parent.PersistentFlags().StringVar(&flagURL, "url", "", "server URL (default $BASE_URL or http://127.0.0.1:8090)")
parent.PersistentFlags().StringVar(&flagToken, "token", "", "auth token (default $BASE_TOKEN or ~/.config/base/token)")
parent.PersistentFlags().StringVar(&flagBase, "base", "", "base org ID (sets X-Org-Id header)")
parent.PersistentFlags().StringVar(&flagFormat, "format", "", "output format: table, json, yaml (default: table on tty, json otherwise)")
clientFn := func() *cli.Client {
return cli.NewClient(
cli.ResolveURL(flagURL),
cli.ResolveToken(flagToken),
flagBase,
)
}
formatFn := func() cli.Format {
return cli.DetectFormat(flagFormat)
}
parent.AddCommand(cli.NewCollectionCommand(clientFn, formatFn))
parent.AddCommand(cli.NewRecordCommand(clientFn, formatFn))
parent.AddCommand(cli.NewLoginCommand(clientFn, formatFn))
parent.AddCommand(cli.NewWhoamiCommand(clientFn, formatFn))
parent.AddCommand(cli.NewCronsCommand(clientFn, formatFn))
parent.AddCommand(cli.NewDaemonCommand())
}