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