wastebin-mcp-go/cli.go
opencode-agent[bot] 4e0d7474f1
fix: sandbox translation now MCP-only; CLI rejects sandbox envs (#244)
* Sandbox translation now MCP-only; CLI rejects sandbox envs.

Co-authored-by: xlionjuan <[email protected]>

* docs: clarify CLI env var contract in INSTALL.md

CLI mode reads the same supported env vars as MCP mode, except the
sandbox path translation variables, which are rejected. Aligns the
CLI mode section with the sandbox translation note on the same page.

---------

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: xlionjuan <[email protected]>
2026-08-04 03:03:46 +08:00

163 lines
4.6 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"os"
"wastebin-mcp-go/internal/wastebin"
)
// printCLIHelp prints the help text showing the create subcommand usage, all
// flags, examples, and exit codes.
func printCLIHelp(w io.Writer) {
//nolint:errcheck // best-effort help text write
fmt.Fprintf(w, "wastebin-mcp-go - MCP server + CLI for Wastebin pastebin (%s)\n\n", version)
//nolint:errcheck // best-effort help text write
fmt.Fprint(w, `USAGE:
wastebin-mcp-go create [OPTIONS] Create a paste
wastebin-mcp-go Start MCP stdio server
CREATE OPTIONS:
--content TEXT Paste content
--file-path PATH Read content from file
--extension EXT Syntax highlighting extension (e.g. md, go, py)
--expires DURATION Expiration: number=seconds, or with unit (s/m/h/d/w/M/y)
--title TEXT Paste title
--burn-after-reading Delete paste after first read
--password TEXT Encrypt paste with password
--debug Enable debug logging
GLOBAL OPTIONS:
--help Show this help message
--version Show version information
EXIT CODES:
0 Success
1 CLI error / invalid arguments
2 MCP server error
For more information, see: https://github.com/xlionjuan/wastebin-mcp-go
`)
}
// errSandboxTranslationCLIUnsupported is returned when a sandbox translation
// environment variable is set in CLI mode. Sandbox path translation is an
// MCP-mode-only feature; the CLI must not be able to configure it.
var errSandboxTranslationCLIUnsupported = errors.New(
"sandbox path translation is only available in MCP mode and is not supported by the CLI; " +
"unset the variable when using 'wastebin-mcp-go create'",
)
// sandboxTranslationEnvVars are the environment variables that configure
// sandbox path translation. They are rejected in CLI mode, where sandbox
// path translation is not supported.
var sandboxTranslationEnvVars = []string{
"WASTEBIN_MCP_SANDBOX_MOUNTS",
"WASTEBIN_MCP_SANDBOX_TRANSPARENT",
}
// checkNoSandboxTranslationEnv rejects sandbox translation environment
// variables in CLI mode. Sandbox path translation is an MCP-mode-only feature;
// the CLI must not be able to define sandbox translation options, so setting
// either variable is an explicit configuration error rather than a silent no-op.
func checkNoSandboxTranslationEnv() error {
for _, env := range sandboxTranslationEnvVars {
if v := os.Getenv(env); v != "" {
return fmt.Errorf("%w: %s", errSandboxTranslationCLIUnsupported, env)
}
}
return nil
}
// runCLIMode executes the one-shot paste creation flow: reads configuration
// from the environment, creates a Wastebin client, builds CreatePasteArgs from
// the provided CLI flags, calls CreatePaste, and prints the JSON response to
// stdout.
//
// NOTE: JSON output is written directly to os.Stdout (not through a caller-
// provided io.Writer), so runCLIMode cannot be cleanly unit-tested for output
// capture. For automated schema/response verification see mcp_test.go and
// schema_test.go.
func runCLIMode(flags *CLIFlags) error {
err := checkNoSandboxTranslationEnv()
if err != nil {
return err
}
cfg, err := wastebin.ConfigFromEnv()
if err != nil {
return fmt.Errorf("configuration error: %w", err)
}
if flags.Debug {
cfg.Debug = true
}
if cfg.Debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
client, err := wastebin.NewWastebinClient(cfg)
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}
args := buildCreatePasteArgs(flags)
ctx := context.Background()
resp, err := client.CreatePaste(ctx, args)
if err != nil {
return fmt.Errorf("create paste failed: %w", err)
}
err = json.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
return fmt.Errorf("failed to encode response: %w", err)
}
return nil
}
// buildCreatePasteArgs converts CLIFlags into a CreatePasteArgs, setting only
// the fields that were explicitly provided. An explicitly-provided empty
// password is preserved so that the handler can reject it with errPasswordEmpty.
func buildCreatePasteArgs(flags *CLIFlags) *wastebin.CreatePasteArgs {
args := &wastebin.CreatePasteArgs{}
if flags.Content != "" {
args.Content = &flags.Content
}
if flags.FilePath != "" {
args.FilePath = &flags.FilePath
}
if flags.Extension != "" {
args.Extension = &flags.Extension
}
if flags.Expires != "" {
args.Expires = &flags.Expires
}
if flags.Title != "" {
args.Title = &flags.Title
}
if flags.BurnAfterReading {
args.BurnAfterReading = &flags.BurnAfterReading
}
if flags.PasswordSet {
args.Password = &flags.Password
}
return args
}