Files
vpp-maglev/cmd/client/main.go
T
pim fb61e72e06 frontend: deep-link via ?instance=; client/frontend default to :9090; Makefile help; v1.1.0
- cmd/frontend/web: honour ?instance=<hostname> query parameter on the
  initial scope hydration so /view/?instance=lb-ams opens the dashboard
  scoped to that maglevd. The cookie is updated on consumption; an
  unknown name still falls back to the first server via App.tsx.

- cmd/client, cmd/frontend: --server now accepts bare hostnames. A new
  internal/netutil.EnsurePort canonicalises addresses by appending
  :9090 when no port is given, with bracketing for bare IPv6 literals.
  Unit test covers the IPv4/IPv6/bracketed/already-ported permutations.

- Makefile: new self-documenting `help` target as the default rule;
  every user-facing target now carries a `## ` description that the
  awk-based help auto-extracts. fixstyle-web skips with a friendly
  message when prettier isn't installed instead of failing on npx.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 10:27:16 +02:00

95 lines
2.5 KiB
Go

// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
buildinfo "git.ipng.ch/ipng/vpp-maglev/cmd"
"git.ipng.ch/ipng/vpp-maglev/internal/grpcapi"
"git.ipng.ch/ipng/vpp-maglev/internal/netutil"
)
// defaultGRPCPort is the maglevd gRPC port (mirrors the server's
// -grpc-addr default in cmd/server/main.go). Used when -server is given
// without an explicit ":<port>" so operators can type "--server chbtl2"
// instead of "--server chbtl2:9090".
const defaultGRPCPort = "9090"
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", formatError(err))
os.Exit(1)
}
}
func run() error {
defaultServer := "localhost:9090"
if v := os.Getenv("MAGLEV_SERVER"); v != "" {
defaultServer = v
}
serverAddr := flag.String("server", defaultServer, "maglev server address (env: MAGLEV_SERVER)")
color := flag.Bool("color", true, "colorize static labels in output (defaults to false in one-shot mode)")
printVersion := flag.Bool("version", false, "print version and exit")
flag.Parse()
if *printVersion {
fmt.Printf("maglevc %s (commit %s, built %s)\n",
buildinfo.Version(), buildinfo.Commit(), buildinfo.Date())
return nil
}
// Detect whether -color was explicitly set so we can pick a
// mode-aware default: color is useful in the interactive shell but
// noise (ANSI escapes) when piping one-shot output into scripts.
colorExplicit := false
flag.Visit(func(f *flag.Flag) {
if f.Name == "color" {
colorExplicit = true
}
})
addr := netutil.EnsurePort(*serverAddr, defaultGRPCPort)
conn, err := grpc.NewClient(addr,
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return fmt.Errorf("connect %s: %w", addr, err)
}
defer func() { _ = conn.Close() }()
client := grpcapi.NewMaglevClient(conn)
ctx := context.Background()
args := flag.Args()
if len(args) == 0 {
// Interactive shell: color defaults to true.
if colorExplicit {
colorEnabled = *color
} else {
colorEnabled = true
}
fmt.Printf("maglevc %s (commit %s, built %s)\n",
buildinfo.Version(), buildinfo.Commit(), buildinfo.Date())
return runShell(ctx, client)
}
// One-shot command from CLI arguments: color defaults to false so
// output is script-safe. Operators wanting color can still pass
// -color=true explicitly.
if colorExplicit {
colorEnabled = *color
} else {
colorEnabled = false
}
root := buildTree()
tokens := splitTokens(strings.Join(args, " "))
return dispatch(ctx, root, client, tokens)
}