|
1 | 1 | // cmd/apm is the entry point for the APM CLI (Go rewrite). |
2 | | -// This is a scaffold -- full implementation follows in subsequent milestones. |
| 2 | +// Agent Package Manager (APM) -- Go implementation. |
3 | 3 | package main |
4 | 4 |
|
5 | | -import "fmt" |
| 5 | +import ( |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const version = "0.1.0-go" |
| 13 | + |
| 14 | +const helpText = `Agent Package Manager (APM): The package manager for AI-Native Development |
| 15 | +
|
| 16 | +Usage: |
| 17 | + apm [command] |
| 18 | +
|
| 19 | +Available Commands: |
| 20 | + audit Audit installed packages for security issues |
| 21 | + cache Manage the APM package cache |
| 22 | + compile Compile APM primitives for a project |
| 23 | + config View or set APM configuration values |
| 24 | + deps Show or manage package dependencies |
| 25 | + experimental Access experimental features |
| 26 | + init Initialize a new APM project |
| 27 | + install Install packages |
| 28 | + list List installed packages |
| 29 | + marketplace Browse the APM marketplace |
| 30 | + mcp Manage MCP server integrations |
| 31 | + outdated Show outdated packages |
| 32 | + pack Pack a project into a distributable bundle |
| 33 | + plugin Manage APM plugins |
| 34 | + policy View or enforce APM policies |
| 35 | + preview Preview changes before applying them |
| 36 | + prune Remove unused packages |
| 37 | + run Run a script or command via APM |
| 38 | + runtime Manage runtimes |
| 39 | + search Search the marketplace |
| 40 | + self-update Update APM itself |
| 41 | + targets List available targets |
| 42 | + uninstall Remove installed packages |
| 43 | + unpack Unpack a bundle |
| 44 | + update Update installed packages |
| 45 | + view View package information |
| 46 | +
|
| 47 | +Flags: |
| 48 | + --help Show this help and exit |
| 49 | + --version Show version and exit |
| 50 | +
|
| 51 | +Use "apm [command] --help" for more information about a command.` |
| 52 | + |
| 53 | +var commands = map[string]string{ |
| 54 | + "audit": "Audit installed packages for security issues", |
| 55 | + "cache": "Manage the APM package cache", |
| 56 | + "compile": "Compile APM primitives for a project", |
| 57 | + "config": "View or set APM configuration values", |
| 58 | + "deps": "Show or manage package dependencies", |
| 59 | + "experimental": "Access experimental features", |
| 60 | + "init": "Initialize a new APM project", |
| 61 | + "install": "Install packages", |
| 62 | + "list": "List installed packages", |
| 63 | + "marketplace": "Browse the APM marketplace", |
| 64 | + "mcp": "Manage MCP server integrations", |
| 65 | + "outdated": "Show outdated packages", |
| 66 | + "pack": "Pack a project into a distributable bundle", |
| 67 | + "plugin": "Manage APM plugins", |
| 68 | + "policy": "View or enforce APM policies", |
| 69 | + "preview": "Preview changes before applying them", |
| 70 | + "prune": "Remove unused packages", |
| 71 | + "run": "Run a script or command via APM", |
| 72 | + "runtime": "Manage runtimes", |
| 73 | + "search": "Search the marketplace", |
| 74 | + "self-update": "Update APM itself", |
| 75 | + "targets": "List available targets", |
| 76 | + "uninstall": "Remove installed packages", |
| 77 | + "unpack": "Unpack a bundle", |
| 78 | + "update": "Update installed packages", |
| 79 | + "view": "View package information", |
| 80 | +} |
| 81 | + |
| 82 | +// aliases maps legacy or alternate names to canonical commands. |
| 83 | +var aliases = map[string]string{ |
| 84 | + "info": "view", |
| 85 | + "self_update": "self-update", |
| 86 | +} |
| 87 | + |
| 88 | +func cmdHelp(name string) { |
| 89 | + canonical := name |
| 90 | + if a, ok := aliases[name]; ok { |
| 91 | + canonical = a |
| 92 | + } |
| 93 | + desc, ok := commands[canonical] |
| 94 | + if !ok { |
| 95 | + fmt.Fprintf(os.Stderr, "apm: unknown command %q\n", name) |
| 96 | + fmt.Fprintln(os.Stderr, `Run "apm --help" for usage.`) |
| 97 | + os.Exit(1) |
| 98 | + } |
| 99 | + fmt.Printf("Usage:\n apm %s [flags]\n\n%s\n\nFlags:\n --help Show this help and exit\n", canonical, desc) |
| 100 | +} |
| 101 | + |
| 102 | +func run(args []string) int { |
| 103 | + if len(args) == 0 { |
| 104 | + fmt.Println(helpText) |
| 105 | + return 0 |
| 106 | + } |
| 107 | + |
| 108 | + // Top-level flags |
| 109 | + fs := flag.NewFlagSet("apm", flag.ContinueOnError) |
| 110 | + showVersion := fs.Bool("version", false, "Show version and exit") |
| 111 | + showHelp := fs.Bool("help", false, "Show help and exit") |
| 112 | + |
| 113 | + // Only parse flags that appear before any subcommand. |
| 114 | + // Collect the first non-flag arg as the subcommand. |
| 115 | + var subArgs []string |
| 116 | + i := 0 |
| 117 | + for i < len(args) { |
| 118 | + a := args[i] |
| 119 | + if a == "--version" || a == "-version" { |
| 120 | + *showVersion = true |
| 121 | + i++ |
| 122 | + continue |
| 123 | + } |
| 124 | + if a == "--help" || a == "-help" || a == "-h" { |
| 125 | + *showHelp = true |
| 126 | + i++ |
| 127 | + continue |
| 128 | + } |
| 129 | + // Stop at first non-flag token. |
| 130 | + subArgs = append(subArgs, args[i:]...) |
| 131 | + break |
| 132 | + } |
| 133 | + |
| 134 | + if *showVersion { |
| 135 | + fmt.Printf("apm version %s (go)\n", version) |
| 136 | + return 0 |
| 137 | + } |
| 138 | + if *showHelp && len(subArgs) == 0 { |
| 139 | + fmt.Println(helpText) |
| 140 | + return 0 |
| 141 | + } |
| 142 | + |
| 143 | + if len(subArgs) == 0 { |
| 144 | + fmt.Println(helpText) |
| 145 | + return 0 |
| 146 | + } |
| 147 | + |
| 148 | + cmd := subArgs[0] |
| 149 | + rest := subArgs[1:] |
| 150 | + |
| 151 | + // "help <command>" dispatches to per-command help. |
| 152 | + if cmd == "help" { |
| 153 | + if len(rest) == 0 { |
| 154 | + fmt.Println(helpText) |
| 155 | + return 0 |
| 156 | + } |
| 157 | + cmdHelp(rest[0]) |
| 158 | + return 0 |
| 159 | + } |
| 160 | + |
| 161 | + // Resolve aliases. |
| 162 | + if canonical, ok := aliases[cmd]; ok { |
| 163 | + cmd = canonical |
| 164 | + } |
| 165 | + |
| 166 | + // Unknown command. |
| 167 | + if _, ok := commands[cmd]; !ok { |
| 168 | + fmt.Fprintf(os.Stderr, "apm: unknown command %q\n", cmd) |
| 169 | + fmt.Fprintln(os.Stderr, `Run "apm --help" for usage.`) |
| 170 | + return 1 |
| 171 | + } |
| 172 | + |
| 173 | + // --help on subcommand. |
| 174 | + for _, a := range rest { |
| 175 | + if a == "--help" || a == "-h" || a == "-help" { |
| 176 | + cmdHelp(cmd) |
| 177 | + return 0 |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // Subcommand stub: print informative not-yet-implemented message. |
| 182 | + fmt.Fprintf(os.Stderr, "apm %s: not yet fully implemented in the Go rewrite.\n", cmd) |
| 183 | + fmt.Fprintf(os.Stderr, "Use the Python APM CLI for production use: uv run apm %s %s\n", |
| 184 | + cmd, strings.Join(rest, " ")) |
| 185 | + return 1 |
| 186 | +} |
6 | 187 |
|
7 | 188 | func main() { |
8 | | - fmt.Println("apm: Go rewrite (work in progress)") |
| 189 | + os.Exit(run(os.Args[1:])) |
9 | 190 | } |
| 191 | + |
0 commit comments