Skip to content

Commit aaf6ea5

Browse files
committed
refactor: streamline command handling and enhance help output
- Refactored main() to handle global help/version flags before command dispatch - Added command-specific help with --help, -h, or help subcommand support - Enhanced printCommandHelp() with detailed descriptions, examples, and available options - Added comprehensive documentation for init, login, logout, and publish commands - Used fmt.Fprintln with explicit os.Stdout for consistent output handling - Improved code organization by extracting cmd and args variables early - Fixed gofmt compliance by adding missing newline at end of file Signed-off-by: NamrathShetty <[email protected]>
1 parent 9c1a1a8 commit aaf6ea5

File tree

1 file changed

+93
-9
lines changed

1 file changed

+93
-9
lines changed

cmd/publisher/main.go

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,37 @@ func main() {
2727
os.Exit(1)
2828
}
2929

30+
cmd := os.Args[1]
31+
args := os.Args[2:]
32+
33+
// Handle global help/version
34+
if cmd == "--version" || cmd == "-v" || cmd == "version" {
35+
log.Printf("mcp-publisher %s (commit: %s, built: %s)", Version, GitCommit, BuildTime)
36+
return
37+
}
38+
if cmd == "--help" || cmd == "-h" || cmd == "help" {
39+
printUsage()
40+
return
41+
}
42+
43+
// Print command-specific help if --help or -h or help is the first argument to a subcommand
44+
if len(args) > 0 && (args[0] == "--help" || args[0] == "-h" || args[0] == "help") {
45+
printCommandHelp(cmd)
46+
return
47+
}
48+
3049
var err error
31-
switch os.Args[1] {
50+
switch cmd {
3251
case "init":
3352
err = commands.InitCommand()
3453
case "login":
35-
err = commands.LoginCommand(os.Args[2:])
54+
err = commands.LoginCommand(args)
3655
case "logout":
3756
err = commands.LogoutCommand()
3857
case "publish":
39-
err = commands.PublishCommand(os.Args[2:])
40-
case "--version", "-v", "version":
41-
log.Printf("mcp-publisher %s (commit: %s, built: %s)", Version, GitCommit, BuildTime)
42-
return
43-
case "--help", "-h", "help":
44-
printUsage()
58+
err = commands.PublishCommand(args)
4559
default:
46-
fmt.Fprintf(os.Stderr, "Unknown command: %s\n\n", os.Args[1])
60+
fmt.Fprintf(os.Stderr, "Unknown command: %s\n\n", cmd)
4761
printUsage()
4862
os.Exit(1)
4963
}
@@ -54,6 +68,76 @@ func main() {
5468
}
5569
}
5670

71+
func printCommandHelp(command string) {
72+
switch command {
73+
case "init":
74+
_, _ = fmt.Fprintln(os.Stdout, "Usage: mcp-publisher init")
75+
_, _ = fmt.Fprintln(os.Stdout)
76+
_, _ = fmt.Fprintln(os.Stdout, "Description:")
77+
_, _ = fmt.Fprintln(os.Stdout, " Create a server.json file template in the current directory.")
78+
_, _ = fmt.Fprintln(os.Stdout, " This template includes all required and optional fields for")
79+
_, _ = fmt.Fprintln(os.Stdout, " publishing your MCP server to the registry.")
80+
_, _ = fmt.Fprintln(os.Stdout)
81+
_, _ = fmt.Fprintln(os.Stdout, "Example:")
82+
_, _ = fmt.Fprintln(os.Stdout, " mcp-publisher init")
83+
_, _ = fmt.Fprintln(os.Stdout)
84+
_, _ = fmt.Fprintln(os.Stdout, "The generated server.json file should be customized with your")
85+
_, _ = fmt.Fprintln(os.Stdout, "server's specific details before publishing.")
86+
case "login":
87+
_, _ = fmt.Fprintln(os.Stdout, "Usage: mcp-publisher login <method>")
88+
_, _ = fmt.Fprintln(os.Stdout)
89+
_, _ = fmt.Fprintln(os.Stdout, "Description:")
90+
_, _ = fmt.Fprintln(os.Stdout, " Authenticate with the registry using one of the supported")
91+
_, _ = fmt.Fprintln(os.Stdout, " authentication methods. Authentication is required before")
92+
_, _ = fmt.Fprintln(os.Stdout, " you can publish servers to the registry.")
93+
_, _ = fmt.Fprintln(os.Stdout)
94+
_, _ = fmt.Fprintln(os.Stdout, "Available Methods:")
95+
_, _ = fmt.Fprintln(os.Stdout, " github-at - GitHub Personal Access Token")
96+
_, _ = fmt.Fprintln(os.Stdout, " github-oidc - GitHub OIDC (for CI/CD environments)")
97+
_, _ = fmt.Fprintln(os.Stdout, " http - HTTP-based authentication")
98+
_, _ = fmt.Fprintln(os.Stdout, " dns - DNS-based authentication")
99+
_, _ = fmt.Fprintln(os.Stdout, " none - No authentication (for testing)")
100+
_, _ = fmt.Fprintln(os.Stdout)
101+
_, _ = fmt.Fprintln(os.Stdout, "Example:")
102+
_, _ = fmt.Fprintln(os.Stdout, " mcp-publisher login github-at")
103+
_, _ = fmt.Fprintln(os.Stdout)
104+
_, _ = fmt.Fprintln(os.Stdout, "Credentials are securely stored locally for subsequent publishes.")
105+
case "logout":
106+
_, _ = fmt.Fprintln(os.Stdout, "Usage: mcp-publisher logout")
107+
_, _ = fmt.Fprintln(os.Stdout)
108+
_, _ = fmt.Fprintln(os.Stdout, "Description:")
109+
_, _ = fmt.Fprintln(os.Stdout, " Clear saved authentication credentials from the local system.")
110+
_, _ = fmt.Fprintln(os.Stdout, " This removes all stored authentication tokens and sessions.")
111+
_, _ = fmt.Fprintln(os.Stdout)
112+
_, _ = fmt.Fprintln(os.Stdout, "Example:")
113+
_, _ = fmt.Fprintln(os.Stdout, " mcp-publisher logout")
114+
_, _ = fmt.Fprintln(os.Stdout)
115+
_, _ = fmt.Fprintln(os.Stdout, "After logging out, you will need to run 'mcp-publisher login'")
116+
_, _ = fmt.Fprintln(os.Stdout, "again before publishing.")
117+
case "publish":
118+
_, _ = fmt.Fprintln(os.Stdout, "Usage: mcp-publisher publish [options]")
119+
_, _ = fmt.Fprintln(os.Stdout)
120+
_, _ = fmt.Fprintln(os.Stdout, "Description:")
121+
_, _ = fmt.Fprintln(os.Stdout, " Publish your MCP server to the registry using the server.json")
122+
_, _ = fmt.Fprintln(os.Stdout, " file in the current directory. You must be authenticated before")
123+
_, _ = fmt.Fprintln(os.Stdout, " publishing (see 'mcp-publisher login').")
124+
_, _ = fmt.Fprintln(os.Stdout)
125+
_, _ = fmt.Fprintln(os.Stdout, "Options:")
126+
_, _ = fmt.Fprintln(os.Stdout, " --file <path> - Specify a custom path to server.json")
127+
_, _ = fmt.Fprintln(os.Stdout, " --dry-run - Validate the server.json without publishing")
128+
_, _ = fmt.Fprintln(os.Stdout)
129+
_, _ = fmt.Fprintln(os.Stdout, "Example:")
130+
_, _ = fmt.Fprintln(os.Stdout, " mcp-publisher publish")
131+
_, _ = fmt.Fprintln(os.Stdout, " mcp-publisher publish --file ./my-server.json")
132+
_, _ = fmt.Fprintln(os.Stdout, " mcp-publisher publish --dry-run")
133+
_, _ = fmt.Fprintln(os.Stdout)
134+
_, _ = fmt.Fprintln(os.Stdout, "The server.json file must conform to the registry schema.")
135+
_, _ = fmt.Fprintln(os.Stdout, "Run 'mcp-publisher init' to generate a template.")
136+
default:
137+
printUsage()
138+
}
139+
}
140+
57141
func printUsage() {
58142
_, _ = fmt.Fprintln(os.Stdout, "MCP Registry Publisher Tool")
59143
_, _ = fmt.Fprintln(os.Stdout)

0 commit comments

Comments
 (0)