66 "os"
77
88 "github.com/redhat-developer/app-services-cli/pkg/icon"
9+ "github.com/redhat-developer/app-services-cli/pkg/ioutil/spinner"
910
1011 "github.com/redhat-developer/app-services-cli/pkg/localize"
1112 "github.com/redhat-developer/app-services-cli/pkg/serviceaccount/validation"
@@ -33,11 +34,10 @@ type options struct {
3334 localizer localize.Localizer
3435 Context context.Context
3536
36- fileFormat string
37- overwrite bool
38- name string
39- description string
40- filename string
37+ fileFormat string
38+ overwrite bool
39+ shortDescription string
40+ filename string
4141
4242 interactive bool
4343}
@@ -60,9 +60,9 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command {
6060 Example : opts .localizer .MustLocalize ("serviceAccount.create.cmd.example" ),
6161 Args : cobra .NoArgs ,
6262 RunE : func (cmd * cobra.Command , _ []string ) (err error ) {
63- if ! opts .IO .CanPrompt () && opts .name == "" {
63+ if ! opts .IO .CanPrompt () && opts .shortDescription == "" {
6464 return opts .localizer .MustLocalizeError ("flag.error.requiredWhenNonInteractive" , localize .NewEntry ("Flag" , "name" ))
65- } else if opts .name == "" && opts . description == "" {
65+ } else if opts .shortDescription == "" {
6666 opts .interactive = true
6767 }
6868
@@ -75,10 +75,7 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command {
7575 return opts .localizer .MustLocalizeError ("flag.error.requiredWhenNonInteractive" , localize .NewEntry ("Flag" , "file-format" ))
7676 }
7777
78- if err = validator .ValidateName (opts .name ); err != nil {
79- return err
80- }
81- if err = validator .ValidateDescription (opts .description ); err != nil {
78+ if err = validator .ValidateShortDescription (opts .shortDescription ); err != nil {
8279 return err
8380 }
8481 }
@@ -93,8 +90,7 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command {
9390 },
9491 }
9592
96- cmd .Flags ().StringVar (& opts .name , "name" , "" , opts .localizer .MustLocalize ("serviceAccount.create.flag.name.description" ))
97- cmd .Flags ().StringVar (& opts .description , "description" , "" , opts .localizer .MustLocalize ("serviceAccount.create.flag.description.description" ))
93+ cmd .Flags ().StringVar (& opts .shortDescription , "short-description" , "" , opts .localizer .MustLocalize ("serviceAccount.create.flag.shortDescription.description" ))
9894 cmd .Flags ().BoolVar (& opts .overwrite , "overwrite" , false , opts .localizer .MustLocalize ("serviceAccount.common.flag.overwrite.description" ))
9995 cmd .Flags ().StringVar (& opts .filename , "output-file" , "" , opts .localizer .MustLocalize ("serviceAccount.common.flag.fileLocation.description" ))
10096 cmd .Flags ().StringVar (& opts .fileFormat , "file-format" , "" , opts .localizer .MustLocalize ("serviceAccount.common.flag.fileFormat.description" ))
@@ -110,6 +106,10 @@ func runCreate(opts *options) error {
110106 if err != nil {
111107 return err
112108 }
109+ cfg , err := opts .Config .Load ()
110+ if err != nil {
111+ return err
112+ }
113113
114114 if opts .interactive {
115115 // run the create command interactively
@@ -129,23 +129,29 @@ func runCreate(opts *options) error {
129129 return opts .localizer .MustLocalizeError ("serviceAccount.common.error.credentialsFileAlreadyExists" , localize .NewEntry ("FilePath" , opts .filename ))
130130 }
131131
132+ spinner := spinner .New (opts .IO .ErrOut , opts .localizer )
133+ spinner .SetSuffix (opts .localizer .MustLocalize ("serviceAccount.create.log.info.creating" ))
134+ spinner .Start ()
132135 // create the service account
133- serviceAccountPayload := & kafkamgmtclient.ServiceAccountRequest {Name : opts .name , Description : & opts . description }
136+ serviceAccountPayload := kafkamgmtclient.ServiceAccountRequest {Name : opts .shortDescription }
134137
135- a := conn .API ().ServiceAccount (). CreateServiceAccount ( opts . Context )
136- a = a . ServiceAccountRequest ( * serviceAccountPayload )
137- serviceacct , _ , err := a . Execute ()
138- if err != nil {
139- return err
140- }
138+ serviceacct , httpRes , err := conn .API ().
139+ ServiceAccount ().
140+ CreateServiceAccount ( opts . Context ).
141+ ServiceAccountRequest ( serviceAccountPayload ).
142+ Execute ()
143+ spinner . Stop ()
141144
142- opts .Logger .Info (icon .SuccessPrefix (), opts .localizer .MustLocalize ("serviceAccount.create.log.info.createdSuccessfully" , localize .NewEntry ("ID" , serviceacct .GetId ()), localize .NewEntry ("Name" , serviceacct .GetName ())))
145+ if httpRes != nil {
146+ defer httpRes .Body .Close ()
147+ }
143148
144- cfg , err := opts .Config .Load ()
145149 if err != nil {
146150 return err
147151 }
148152
153+ opts .Logger .Info (icon .SuccessPrefix (), opts .localizer .MustLocalize ("serviceAccount.create.log.info.createdSuccessfully" , localize .NewEntry ("ID" , serviceacct .GetId ())))
154+
149155 creds := & credentials.Credentials {
150156 ClientID : serviceacct .GetClientId (),
151157 ClientSecret : serviceacct .GetClientSecret (),
@@ -158,7 +164,7 @@ func runCreate(opts *options) error {
158164 return fmt .Errorf ("%v: %w" , opts .localizer .MustLocalize ("serviceAccount.common.error.couldNotSaveCredentialsFile" ), err )
159165 }
160166
161- opts .Logger .Info (opts .localizer .MustLocalize ("serviceAccount.common.log.info.credentialsSaved" , localize .NewEntry ("FilePath" , opts .filename )))
167+ opts .Logger .Info (icon . SuccessPrefix (), opts .localizer .MustLocalize ("serviceAccount.common.log.info.credentialsSaved" , localize .NewEntry ("FilePath" , opts .filename )))
162168
163169 return nil
164170}
@@ -176,11 +182,11 @@ func runInteractivePrompt(opts *options) (err error) {
176182 opts .Logger .Debug (opts .localizer .MustLocalize ("common.log.debug.startingInteractivePrompt" ))
177183
178184 promptName := & survey.Input {
179- Message : opts .localizer .MustLocalize ("serviceAccount.create.input.name .message" ),
180- Help : opts .localizer .MustLocalize ("serviceAccount.create.input.name .help" ),
185+ Message : opts .localizer .MustLocalize ("serviceAccount.create.input.shortDescription .message" ),
186+ Help : opts .localizer .MustLocalize ("serviceAccount.create.input.shortDescription .help" ),
181187 }
182188
183- err = survey .AskOne (promptName , & opts .name , survey .WithValidator (survey .Required ), survey .WithValidator (validator .ValidateName ))
189+ err = survey .AskOne (promptName , & opts .shortDescription , survey .WithValidator (survey .Required ), survey .WithValidator (validator .ValidateShortDescription ))
184190 if err != nil {
185191 return err
186192 }
@@ -207,17 +213,5 @@ func runInteractivePrompt(opts *options) (err error) {
207213 return err
208214 }
209215
210- promptDescription := & survey.Multiline {
211- Message : opts .localizer .MustLocalize ("serviceAccount.create.input.description.message" ),
212- Help : opts .localizer .MustLocalize ("serviceAccount.create.flag.description.description" ),
213- }
214-
215- err = survey .AskOne (promptDescription , & opts .description , survey .WithValidator (validator .ValidateDescription ))
216- if err != nil {
217- return err
218- }
219-
220- opts .Logger .Info (opts .localizer .MustLocalize ("serviceAccount.create.log.info.creating" , localize .NewEntry ("Name" , opts .name )))
221-
222216 return nil
223217}
0 commit comments