iop/apps/edge/cmd/edge/main.go
toki b4124f0bd6 feat: CLI setup, edge/node transport refactor, and infrastructure updates
- Add CLI core setup for edge and node services
- Refactor edge transport layer (server, integration tests)
- Refactor node transport layer (parser, session, heartbeat, client)
- Add main_test.go files for edge and node commands
- Add input package for edge service
- Add go.work and go.work.sum for workspace support
- Update configs, docs, and project rules
2026-05-20 16:37:42 +09:00

141 lines
3.8 KiB
Go

package main
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"go.uber.org/fx"
"gopkg.in/yaml.v3"
"iop/apps/edge/internal/bootstrap"
"iop/packages/config"
"iop/packages/hostsetup"
"iop/packages/version"
)
var cfgFile string
func main() {
if err := rootCmd().Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func rootCmd() *cobra.Command {
root := &cobra.Command{
Use: "edge",
Short: "IOP Edge — execution group controller",
}
root.PersistentFlags().StringVarP(&cfgFile, "config", "c", "configs/edge.yaml", "config file path")
root.AddCommand(serveCmd(), consoleCmd(), versionCmd(), configCmd(), setupCmd())
return root
}
func serveCmd() *cobra.Command {
return &cobra.Command{
Use: "serve",
Short: "Start the edge server",
RunE: func(_ *cobra.Command, _ []string) error {
cfg, err := config.LoadEdge(cfgFile)
if err != nil {
return fmt.Errorf("load config: %w", err)
}
fx.New(bootstrap.Module(cfg)).Run()
return nil
},
}
}
func consoleCmd() *cobra.Command {
return &cobra.Command{
Use: "console",
Short: "Start the edge server with an interactive node console",
RunE: func(_ *cobra.Command, _ []string) error {
cfg, err := config.LoadEdge(cfgFile)
if err != nil {
return fmt.Errorf("load config: %w", err)
}
return runConsole(context.Background(), cfg, os.Stdin, os.Stdout)
},
}
}
func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print edge binary version",
RunE: func(cmd *cobra.Command, _ []string) error {
fmt.Fprintln(cmd.OutOrStdout(), version.Version)
return nil
},
}
}
func configCmd() *cobra.Command {
c := &cobra.Command{
Use: "config",
Short: "Inspect edge config",
}
c.AddCommand(configPrintCmd(), configCheckCmd())
return c
}
func configPrintCmd() *cobra.Command {
return &cobra.Command{
Use: "print",
Short: "Print effective edge config as YAML",
RunE: func(cmd *cobra.Command, _ []string) error {
cfg, err := config.LoadEdge(cfgFile)
if err != nil {
return fmt.Errorf("load config: %w", err)
}
enc := yaml.NewEncoder(cmd.OutOrStdout())
defer enc.Close()
return enc.Encode(cfg)
},
}
}
func configCheckCmd() *cobra.Command {
return &cobra.Command{
Use: "check",
Short: "Validate edge config can be loaded",
RunE: func(cmd *cobra.Command, _ []string) error {
if _, err := config.LoadEdge(cfgFile); err != nil {
return fmt.Errorf("load config: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "OK %s\n", cfgFile)
return nil
},
}
}
func setupCmd() *cobra.Command {
var opts hostsetup.SetupOptions
c := &cobra.Command{
Use: "setup",
Short: "Prepare host environment and systemd unit for iop-edge",
RunE: func(cmd *cobra.Command, _ []string) error {
if cmd.Flags().Changed("config") {
opts.ConfigPath = cfgFile
} else {
opts.ConfigPath = "/etc/iop/edge.yaml"
}
return hostsetup.Run(cmd.Context(), hostsetup.EdgeSpec(), opts, cmd.OutOrStdout())
},
}
c.Flags().StringVar(&opts.BinaryPath, "binary", "", "path to iop-edge binary (defaults to current executable)")
c.Flags().StringVar(&opts.DataDir, "data-dir", "", "data directory (defaults to spec)")
c.Flags().StringVar(&opts.UnitPath, "unit", "", "systemd unit path (defaults to spec)")
c.Flags().StringVar(&opts.User, "user", "", "service user")
c.Flags().StringVar(&opts.Group, "group", "", "service group")
c.Flags().BoolVar(&opts.Enable, "enable", false, "enable the systemd unit")
c.Flags().BoolVar(&opts.Start, "start", false, "start the systemd unit")
c.Flags().BoolVar(&opts.Restart, "restart", false, "restart the systemd unit")
c.Flags().BoolVar(&opts.DryRun, "dry-run", false, "print plan and previews without making changes")
c.Flags().BoolVar(&opts.OverwriteConfig, "overwrite-config", false, "overwrite an existing config file")
return c
}