|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/onecli/onecli-cli/internal/api" |
| 7 | + "github.com/onecli/onecli-cli/pkg/output" |
| 8 | + "github.com/onecli/onecli-cli/pkg/validate" |
| 9 | +) |
| 10 | + |
| 11 | +// AppsCmd is the `onecli apps` command group. |
| 12 | +type AppsCmd struct { |
| 13 | + List AppsListCmd `cmd:"" help:"List all app connections."` |
| 14 | + Connect AppsConnectCmd `cmd:"" help:"Connect an OAuth app (e.g. Google)."` |
| 15 | + Disconnect AppsDisconnectCmd `cmd:"" help:"Disconnect an app."` |
| 16 | +} |
| 17 | + |
| 18 | +// AppsListCmd is `onecli apps list`. |
| 19 | +type AppsListCmd struct { |
| 20 | + Fields string `optional:"" help:"Comma-separated list of fields to include in output."` |
| 21 | + Quiet string `optional:"" name:"quiet" help:"Output only the specified field, one per line."` |
| 22 | +} |
| 23 | + |
| 24 | +func (c *AppsListCmd) Run(out *output.Writer) error { |
| 25 | + client, err := newClient() |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + apps, err := client.ListApps(newContext()) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + if c.Quiet != "" { |
| 34 | + return out.WriteQuiet(apps, c.Quiet) |
| 35 | + } |
| 36 | + return out.WriteFiltered(apps, c.Fields) |
| 37 | +} |
| 38 | + |
| 39 | +// AppsConnectCmd is `onecli apps connect`. |
| 40 | +type AppsConnectCmd struct { |
| 41 | + Provider string `required:"" help:"Provider name (e.g. 'google')."` |
| 42 | + ClientID string `required:"" name:"client-id" help:"OAuth client ID."` |
| 43 | + ClientSecret string `required:"" name:"client-secret" help:"OAuth client secret."` |
| 44 | + DryRun bool `optional:"" name:"dry-run" help:"Validate the request without executing it."` |
| 45 | +} |
| 46 | + |
| 47 | +// connectResponse wraps the API response with agent-facing guidance. |
| 48 | +type connectResponse struct { |
| 49 | + ID string `json:"id"` |
| 50 | + Provider string `json:"provider"` |
| 51 | + Status string `json:"status"` |
| 52 | + CreatedAt string `json:"createdAt"` |
| 53 | +} |
| 54 | + |
| 55 | +const docsBaseURL = "https://docs.onecli.sh/guides/credential-stubs" |
| 56 | + |
| 57 | +func (c *AppsConnectCmd) Run(out *output.Writer) error { |
| 58 | + input := api.ConnectAppInput{ |
| 59 | + Provider: c.Provider, |
| 60 | + ClientID: c.ClientID, |
| 61 | + ClientSecret: c.ClientSecret, |
| 62 | + } |
| 63 | + |
| 64 | + if c.DryRun { |
| 65 | + preview := map[string]string{ |
| 66 | + "provider": input.Provider, |
| 67 | + "clientId": input.ClientID, |
| 68 | + "clientSecret": "***", |
| 69 | + } |
| 70 | + return out.WriteDryRun("Would connect app", preview) |
| 71 | + } |
| 72 | + |
| 73 | + client, err := newClient() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + app, err := client.ConnectApp(newContext(), input) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + docsURL := docsBaseURL + "/" + input.Provider + ".md" |
| 83 | + fallbackURL := docsBaseURL + "/general-app.md" |
| 84 | + out.SetHint("Your MCP server needs local credential stub files to start. See " + docsURL + " for examples (fallback: " + fallbackURL + " ). The OneCLI gateway handles real OAuth token exchange at request time.") |
| 85 | + resp := connectResponse{ |
| 86 | + ID: app.ID, |
| 87 | + Provider: app.Provider, |
| 88 | + Status: app.Status, |
| 89 | + CreatedAt: app.CreatedAt, |
| 90 | + } |
| 91 | + return out.Write(resp) |
| 92 | +} |
| 93 | + |
| 94 | +// AppsDisconnectCmd is `onecli apps disconnect`. |
| 95 | +type AppsDisconnectCmd struct { |
| 96 | + ID string `required:"" help:"ID of the app connection to disconnect."` |
| 97 | + DryRun bool `optional:"" name:"dry-run" help:"Validate the request without executing it."` |
| 98 | +} |
| 99 | + |
| 100 | +func (c *AppsDisconnectCmd) Run(out *output.Writer) error { |
| 101 | + if err := validate.ResourceID(c.ID); err != nil { |
| 102 | + return fmt.Errorf("invalid app ID: %w", err) |
| 103 | + } |
| 104 | + if c.DryRun { |
| 105 | + return out.WriteDryRun("Would disconnect app", map[string]string{"id": c.ID}) |
| 106 | + } |
| 107 | + client, err := newClient() |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + if err := client.DisconnectApp(newContext(), c.ID); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + return out.Write(map[string]string{"status": "disconnected", "id": c.ID}) |
| 115 | +} |
0 commit comments