From 39856a3060f36f4879df3112229eac47c9dd1e65 Mon Sep 17 00:00:00 2001 From: orion Date: Tue, 14 Apr 2026 21:33:09 +0000 Subject: [PATCH] feat(ego-mgr): add lookup subcommand ego-mgr lookup - Finds agent whose default-username == username - Echoes the agent-id (map key in agent-scope) - Exits 7 if not found or column missing --- ego-mgr/src/main.go | 51 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/ego-mgr/src/main.go b/ego-mgr/src/main.go index b606241..27e254b 100644 --- a/ego-mgr/src/main.go +++ b/ego-mgr/src/main.go @@ -17,13 +17,14 @@ const ( // Exit codes per spec const ( - ExitSuccess = 0 - ExitUsageError = 1 - ExitColumnNotFound = 2 - ExitColumnExists = 3 - ExitPermission = 4 - ExitLockFailed = 5 - ExitJSONError = 6 + ExitSuccess = 0 + ExitUsageError = 1 + ExitColumnNotFound = 2 + ExitColumnExists = 3 + ExitPermission = 4 + ExitLockFailed = 5 + ExitJSONError = 6 + ExitNotFound = 7 ) // EgoData is the on-disk JSON structure @@ -185,7 +186,7 @@ Examples: ego-mgr delete name`, } - rootCmd.AddCommand(addCmd(), deleteCmd(), setCmd(), getCmd(), showCmd(), listCmd()) + rootCmd.AddCommand(addCmd(), deleteCmd(), setCmd(), getCmd(), showCmd(), listCmd(), lookupCmd()) if err := rootCmd.Execute(); err != nil { os.Exit(ExitUsageError) @@ -480,3 +481,37 @@ func listColumnsCmd() *cobra.Command { } return cmd } + +func lookupCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "lookup ", + Short: "Look up an agent ID by default-username", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + username := args[0] + + data, err := readEgoData() + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(ExitJSONError) + } + + // Verify default-username column exists + if !isAgentColumn(data, "default-username") { + fmt.Fprintf(os.Stderr, "Error: column 'default-username' does not exist\n") + os.Exit(ExitColumnNotFound) + } + + for agentID, agentData := range data.AgentScope { + if agentData["default-username"] == username { + fmt.Print(agentID) + return + } + } + + fmt.Fprintf(os.Stderr, "Error: no agent found with default-username '%s'\n", username) + os.Exit(ExitNotFound) + }, + } + return cmd +} -- 2.49.1