1+ // Package oauth provides OAuth client management commands.
12package oauth
23
34import (
45 "fmt"
6+ "strings"
57
68 "github.com/pterm/pterm"
79 "github.com/urfave/cli/v2"
810
911 "github.com/NodeOps-app/createos-cli/internal/api"
12+ "github.com/NodeOps-app/createos-cli/internal/terminal"
1013)
1114
1215func newCreateCommand () * cli.Command {
1316 return & cli.Command {
1417 Name : "create" ,
1518 Usage : "Create an OAuth client" ,
16- Description : "Starts an interactive flow to create an OAuth client.\n \n " +
17- " You'll be asked for the client name, redirect URIs, visibility, and policy URLs." ,
19+ Description : "Creates an OAuth client interactively, or non-interactively via flags.\n \n " +
20+ " Non-interactive example:\n " +
21+ " createos oauth-clients create \\ \n " +
22+ " --name \" My App\" \\ \n " +
23+ " --redirect-uri https://myapp.com/callback \\ \n " +
24+ " --app-url https://myapp.com \\ \n " +
25+ " --policy-url https://myapp.com/privacy \\ \n " +
26+ " --tos-url https://myapp.com/tos \\ \n " +
27+ " --logo-url https://myapp.com/logo.png" ,
28+ Flags : []cli.Flag {
29+ & cli.StringFlag {Name : "name" , Usage : "Client name" },
30+ & cli.StringSliceFlag {Name : "redirect-uri" , Usage : "Redirect URI (repeatable)" },
31+ & cli.BoolFlag {Name : "public" , Usage : "Create a public client (no client secret)" },
32+ & cli.StringFlag {Name : "app-url" , Usage : "Application URL" },
33+ & cli.StringFlag {Name : "policy-url" , Usage : "Privacy policy URL" },
34+ & cli.StringFlag {Name : "tos-url" , Usage : "Terms of service URL" },
35+ & cli.StringFlag {Name : "logo-url" , Usage : "Logo URL" },
36+ },
1837 Action : func (c * cli.Context ) error {
1938 client , err := getClient (c )
2039 if err != nil {
@@ -29,51 +48,95 @@ func newCreateCommand() *cli.Command {
2948 return fmt .Errorf ("you already have the maximum number of OAuth clients (4)\n \n To continue, delete one through the dashboard or reuse an existing client" )
3049 }
3150
32- name , err := promptRequiredText ("Client name" , validateClientName )
33- if err != nil {
34- return err
35- }
51+ isInteractive := terminal .IsInteractive ()
52+ hasFlags := c .IsSet ("name" ) || c .IsSet ("redirect-uri" )
3653
37- redirectURIs , err := promptRedirectURIs ()
38- if err != nil {
39- return err
40- }
54+ var name string
55+ var redirectURIs [] string
56+ var public bool
57+ var clientURI , policyURI , tosURI , logoURI string
4158
42- public , err := pterm .DefaultInteractiveConfirm .
43- WithDefaultText ("Should this be a public client? Public clients do not use a client secret" ).
44- WithDefaultValue (false ).
45- Show ()
46- if err != nil {
47- return fmt .Errorf ("could not read client type: %w" , err )
48- }
59+ if isInteractive && ! hasFlags {
60+ name , err = promptRequiredText ("Client name" , validateClientName )
61+ if err != nil {
62+ return err
63+ }
4964
50- clientURI , err := promptRequiredText ("Application URL" , validateURI )
51- if err != nil {
52- return err
53- }
54- policyURI , err := promptRequiredText ("Privacy policy URL" , validateURI )
55- if err != nil {
56- return err
57- }
58- tosURI , err := promptRequiredText ("Terms of service URL" , validateURI )
59- if err != nil {
60- return err
61- }
62- logoURI , err := promptRequiredText ("Logo URL" , validateURI )
63- if err != nil {
64- return err
65- }
65+ redirectURIs , err = promptRedirectURIs ()
66+ if err != nil {
67+ return err
68+ }
6669
67- confirm , err := pterm .DefaultInteractiveConfirm .
68- WithDefaultText ("Create this OAuth client now?" ).
69- WithDefaultValue (true ).
70- Show ()
71- if err != nil {
72- return fmt .Errorf ("could not read confirmation: %w" , err )
73- }
74- if ! confirm {
75- fmt .Println ("Cancelled. Your OAuth client was not created." )
76- return nil
70+ public , err = pterm .DefaultInteractiveConfirm .
71+ WithDefaultText ("Should this be a public client? Public clients do not use a client secret" ).
72+ WithDefaultValue (false ).
73+ Show ()
74+ if err != nil {
75+ return fmt .Errorf ("could not read client type: %w" , err )
76+ }
77+
78+ clientURI , err = promptRequiredText ("Application URL" , validateURI )
79+ if err != nil {
80+ return err
81+ }
82+ policyURI , err = promptRequiredText ("Privacy policy URL" , validateURI )
83+ if err != nil {
84+ return err
85+ }
86+ tosURI , err = promptRequiredText ("Terms of service URL" , validateURI )
87+ if err != nil {
88+ return err
89+ }
90+ logoURI , err = promptRequiredText ("Logo URL" , validateURI )
91+ if err != nil {
92+ return err
93+ }
94+
95+ confirm , err := pterm .DefaultInteractiveConfirm .
96+ WithDefaultText ("Create this OAuth client now?" ).
97+ WithDefaultValue (true ).
98+ Show ()
99+ if err != nil {
100+ return fmt .Errorf ("could not read confirmation: %w" , err )
101+ }
102+ if ! confirm {
103+ fmt .Println ("Cancelled. Your OAuth client was not created." )
104+ return nil
105+ }
106+ } else {
107+ name = strings .TrimSpace (c .String ("name" ))
108+ if err := validateClientName (name ); err != nil {
109+ return fmt .Errorf ("--name: %w" , err )
110+ }
111+
112+ redirectURIs = c .StringSlice ("redirect-uri" )
113+ if len (redirectURIs ) == 0 {
114+ return fmt .Errorf ("at least one --redirect-uri is required" )
115+ }
116+ for _ , u := range redirectURIs {
117+ if err := validateURI (u ); err != nil {
118+ return fmt .Errorf ("--redirect-uri %q: %w" , u , err )
119+ }
120+ }
121+
122+ public = c .Bool ("public" )
123+
124+ clientURI = c .String ("app-url" )
125+ if err := validateURI (clientURI ); err != nil {
126+ return fmt .Errorf ("--app-url: %w" , err )
127+ }
128+ policyURI = c .String ("policy-url" )
129+ if err := validateURI (policyURI ); err != nil {
130+ return fmt .Errorf ("--policy-url: %w" , err )
131+ }
132+ tosURI = c .String ("tos-url" )
133+ if err := validateURI (tosURI ); err != nil {
134+ return fmt .Errorf ("--tos-url: %w" , err )
135+ }
136+ logoURI = c .String ("logo-url" )
137+ if err := validateURI (logoURI ); err != nil {
138+ return fmt .Errorf ("--logo-url: %w" , err )
139+ }
77140 }
78141
79142 clientID , err := client .CreateOAuthClient (api.CreateOAuthClientInput {
@@ -94,10 +157,6 @@ func newCreateCommand() *cli.Command {
94157 detail , err := client .GetOAuthClient (clientID )
95158 if err == nil && detail != nil {
96159 printInstructions (c .String ("api-url" ), detail )
97- } else {
98- fmt .Println ()
99- pterm .Println (pterm .Gray (" Hint: To see setup instructions for this client, run:" ))
100- pterm .Println (pterm .Gray (" createos oauth clients instructions " + clientID ))
101160 }
102161
103162 return nil
0 commit comments