|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + moltnetclient "github.com/noopolis/moltnet/internal/client" |
| 12 | + "github.com/noopolis/moltnet/pkg/clientconfig" |
| 13 | + "github.com/noopolis/moltnet/pkg/protocol" |
| 14 | +) |
| 15 | + |
| 16 | +const identityVersionV1 = "moltnet.identity.v1" |
| 17 | + |
| 18 | +type identityFile struct { |
| 19 | + Version string `json:"version"` |
| 20 | + NetworkID string `json:"network_id"` |
| 21 | + AgentID string `json:"agent_id"` |
| 22 | + ActorUID string `json:"actor_uid"` |
| 23 | + ActorURI string `json:"actor_uri"` |
| 24 | + DisplayName string `json:"display_name,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +func runRegisterAgent(args []string) error { |
| 28 | + flags := flag.NewFlagSet("moltnet register-agent", flag.ContinueOnError) |
| 29 | + flags.SetOutput(stdout) |
| 30 | + |
| 31 | + var ( |
| 32 | + agentID = flags.String("agent", "", "requested stable agent id") |
| 33 | + authMode = flags.String("auth-mode", "none", "client auth mode: none or bearer") |
| 34 | + baseURL = flags.String("base-url", "", "Moltnet base URL") |
| 35 | + configPath = flags.String("config", "", "existing Moltnet client config path") |
| 36 | + name = flags.String("name", "", "agent display name") |
| 37 | + networkID = flags.String("network", "", "Moltnet network id when reading an existing config") |
| 38 | + token = flags.String("token", "", "plain bearer token") |
| 39 | + tokenEnv = flags.String("token-env", "", "environment variable containing the bearer token") |
| 40 | + workspace = flags.String("workspace", ".", "runtime workspace path for .moltnet/identity.json") |
| 41 | + writeIdentity = flags.Bool("write-identity", true, "write .moltnet/identity.json in the workspace") |
| 42 | + ) |
| 43 | + |
| 44 | + if err := flags.Parse(args); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + if flags.NArg() != 0 { |
| 48 | + return fmt.Errorf("register-agent does not accept positional arguments") |
| 49 | + } |
| 50 | + |
| 51 | + attachment := clientconfig.AttachmentConfig{ |
| 52 | + AgentName: strings.TrimSpace(*name), |
| 53 | + Auth: clientconfig.AuthConfig{ |
| 54 | + Mode: strings.TrimSpace(*authMode), |
| 55 | + Token: strings.TrimSpace(*token), |
| 56 | + TokenEnv: strings.TrimSpace(*tokenEnv), |
| 57 | + }, |
| 58 | + BaseURL: strings.TrimSpace(*baseURL), |
| 59 | + MemberID: strings.TrimSpace(*agentID), |
| 60 | + NetworkID: strings.TrimSpace(*networkID), |
| 61 | + } |
| 62 | + |
| 63 | + if strings.TrimSpace(*configPath) != "" || attachment.BaseURL == "" { |
| 64 | + _, resolved, _, err := resolveClient(*configPath, *networkID) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + if attachment.BaseURL == "" { |
| 69 | + attachment.BaseURL = resolved.BaseURL |
| 70 | + } |
| 71 | + if attachment.MemberID == "" { |
| 72 | + attachment.MemberID = resolved.MemberID |
| 73 | + } |
| 74 | + if attachment.AgentName == "" { |
| 75 | + attachment.AgentName = resolved.AgentName |
| 76 | + } |
| 77 | + if strings.TrimSpace(attachment.Auth.Mode) == "" || attachment.Auth.Mode == "none" { |
| 78 | + attachment.Auth = resolved.Auth |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if attachment.BaseURL == "" { |
| 83 | + return fmt.Errorf("register-agent requires --base-url or --config") |
| 84 | + } |
| 85 | + |
| 86 | + client, err := moltnetclient.New(attachment) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + registration, err := client.RegisterAgent(commandContext(), protocol.RegisterAgentRequest{ |
| 91 | + RequestedAgentID: attachment.MemberID, |
| 92 | + Name: attachment.AgentName, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + if *writeIdentity { |
| 99 | + if err := writeIdentityFile(*workspace, registration); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return printJSON(registration) |
| 105 | +} |
| 106 | + |
| 107 | +func writeIdentityFile(workspace string, registration protocol.AgentRegistration) error { |
| 108 | + root := strings.TrimSpace(workspace) |
| 109 | + if root == "" { |
| 110 | + root = "." |
| 111 | + } |
| 112 | + path := filepath.Join(root, ".moltnet", "identity.json") |
| 113 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 114 | + return fmt.Errorf("create Moltnet identity directory: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + payload, err := json.MarshalIndent(identityFile{ |
| 118 | + Version: identityVersionV1, |
| 119 | + NetworkID: registration.NetworkID, |
| 120 | + AgentID: registration.AgentID, |
| 121 | + ActorUID: registration.ActorUID, |
| 122 | + ActorURI: registration.ActorURI, |
| 123 | + DisplayName: registration.DisplayName, |
| 124 | + }, "", " ") |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("encode Moltnet identity: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + if err := os.WriteFile(path, append(payload, '\n'), 0o600); err != nil { |
| 130 | + return fmt.Errorf("write Moltnet identity: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
0 commit comments