@@ -126,55 +126,30 @@ func (c BrowserPoolsCmd) Create(ctx context.Context, in BrowserPoolsCreateInput)
126126 }
127127
128128 // Profile
129- if in .ProfileID != "" && in .ProfileName != "" {
130- pterm .Error .Println ("must specify at most one of --profile-id or --profile-name" )
129+ profile , err := buildProfileParam (in .ProfileID , in .ProfileName , in .ProfileSaveChanges )
130+ if err != nil {
131+ pterm .Error .Println (err .Error ())
131132 return nil
132- } else if in .ProfileID != "" || in .ProfileName != "" {
133- req .Profile = kernel.BrowserProfileParam {
134- SaveChanges : kernel .Bool (in .ProfileSaveChanges .Value ),
135- }
136- if in .ProfileID != "" {
137- req .Profile .ID = kernel .String (in .ProfileID )
138- } else if in .ProfileName != "" {
139- req .Profile .Name = kernel .String (in .ProfileName )
140- }
133+ }
134+ if profile != nil {
135+ req .Profile = * profile
141136 }
142137
143138 if in .ProxyID != "" {
144139 req .ProxyID = kernel .String (in .ProxyID )
145140 }
146141
147142 // Extensions
148- if len (in .Extensions ) > 0 {
149- for _ , ext := range in .Extensions {
150- val := strings .TrimSpace (ext )
151- if val == "" {
152- continue
153- }
154- item := kernel.BrowserExtensionParam {}
155- if cuidRegex .MatchString (val ) {
156- item .ID = kernel .String (val )
157- } else {
158- item .Name = kernel .String (val )
159- }
160- req .Extensions = append (req .Extensions , item )
161- }
162- }
143+ req .Extensions = buildExtensionsParam (in .Extensions )
163144
164145 // Viewport
165- if in .Viewport != "" {
166- width , height , refreshRate , err := parseViewport (in .Viewport )
167- if err != nil {
168- pterm .Error .Printf ("Invalid viewport format: %v\n " , err )
169- return nil
170- }
171- req .Viewport = kernel.BrowserViewportParam {
172- Width : width ,
173- Height : height ,
174- }
175- if refreshRate > 0 {
176- req .Viewport .RefreshRate = kernel .Int (refreshRate )
177- }
146+ viewport , err := buildViewportParam (in .Viewport )
147+ if err != nil {
148+ pterm .Error .Println (err .Error ())
149+ return nil
150+ }
151+ if viewport != nil {
152+ req .Viewport = * viewport
178153 }
179154
180155 params := kernel.BrowserPoolNewParams {
@@ -284,55 +259,30 @@ func (c BrowserPoolsCmd) Update(ctx context.Context, in BrowserPoolsUpdateInput)
284259 }
285260
286261 // Profile
287- if in .ProfileID != "" && in .ProfileName != "" {
288- pterm .Error .Println ("must specify at most one of --profile-id or --profile-name" )
262+ profile , err := buildProfileParam (in .ProfileID , in .ProfileName , in .ProfileSaveChanges )
263+ if err != nil {
264+ pterm .Error .Println (err .Error ())
289265 return nil
290- } else if in .ProfileID != "" || in .ProfileName != "" {
291- req .Profile = kernel.BrowserProfileParam {
292- SaveChanges : kernel .Bool (in .ProfileSaveChanges .Value ),
293- }
294- if in .ProfileID != "" {
295- req .Profile .ID = kernel .String (in .ProfileID )
296- } else if in .ProfileName != "" {
297- req .Profile .Name = kernel .String (in .ProfileName )
298- }
266+ }
267+ if profile != nil {
268+ req .Profile = * profile
299269 }
300270
301271 if in .ProxyID != "" {
302272 req .ProxyID = kernel .String (in .ProxyID )
303273 }
304274
305275 // Extensions
306- if len (in .Extensions ) > 0 {
307- for _ , ext := range in .Extensions {
308- val := strings .TrimSpace (ext )
309- if val == "" {
310- continue
311- }
312- item := kernel.BrowserExtensionParam {}
313- if cuidRegex .MatchString (val ) {
314- item .ID = kernel .String (val )
315- } else {
316- item .Name = kernel .String (val )
317- }
318- req .Extensions = append (req .Extensions , item )
319- }
320- }
276+ req .Extensions = buildExtensionsParam (in .Extensions )
321277
322278 // Viewport
323- if in .Viewport != "" {
324- width , height , refreshRate , err := parseViewport (in .Viewport )
325- if err != nil {
326- pterm .Error .Printf ("Invalid viewport format: %v\n " , err )
327- return nil
328- }
329- req .Viewport = kernel.BrowserViewportParam {
330- Width : width ,
331- Height : height ,
332- }
333- if refreshRate > 0 {
334- req .Viewport .RefreshRate = kernel .Int (refreshRate )
335- }
279+ viewport , err := buildViewportParam (in .Viewport )
280+ if err != nil {
281+ pterm .Error .Println (err .Error ())
282+ return nil
283+ }
284+ if viewport != nil {
285+ req .Viewport = * viewport
336286 }
337287
338288 params := kernel.BrowserPoolUpdateParams {
@@ -536,13 +486,13 @@ func init() {
536486 browserPoolsUpdateCmd .Flags ().String ("proxy-id" , "" , "Proxy ID" )
537487 browserPoolsUpdateCmd .Flags ().StringSlice ("extension" , []string {}, "Extension IDs or names" )
538488 browserPoolsUpdateCmd .Flags ().String ("viewport" , "" , "Viewport size (e.g. 1280x800)" )
539- browserPoolsUpdateCmd .Flags ().Bool ("discard-all-idle" , true , "Discard all idle browsers" )
489+ browserPoolsUpdateCmd .Flags ().Bool ("discard-all-idle" , false , "Discard all idle browsers" )
540490
541491 // delete flags
542492 browserPoolsDeleteCmd .Flags ().Bool ("force" , false , "Force delete even if browsers are leased" )
543493
544494 // acquire flags
545- browserPoolsAcquireCmd .Flags ().Int64 ("timeout" , 5 , "Acquire timeout in seconds" )
495+ browserPoolsAcquireCmd .Flags ().Int64 ("timeout" , 0 , "Acquire timeout in seconds" )
546496
547497 // release flags
548498 browserPoolsReleaseCmd .Flags ().String ("session-id" , "" , "Browser session ID to release" )
@@ -681,3 +631,64 @@ func runBrowserPoolsFlush(cmd *cobra.Command, args []string) error {
681631 c := BrowserPoolsCmd {client : & client .BrowserPools }
682632 return c .Flush (cmd .Context (), BrowserPoolsFlushInput {IDOrName : args [0 ]})
683633}
634+
635+ func buildProfileParam (profileID , profileName string , saveChanges BoolFlag ) (* kernel.BrowserProfileParam , error ) {
636+ if profileID != "" && profileName != "" {
637+ return nil , fmt .Errorf ("must specify at most one of --profile-id or --profile-name" )
638+ }
639+ if profileID == "" && profileName == "" {
640+ return nil , nil
641+ }
642+
643+ profile := kernel.BrowserProfileParam {
644+ SaveChanges : kernel .Bool (saveChanges .Value ),
645+ }
646+ if profileID != "" {
647+ profile .ID = kernel .String (profileID )
648+ } else if profileName != "" {
649+ profile .Name = kernel .String (profileName )
650+ }
651+ return & profile , nil
652+ }
653+
654+ func buildExtensionsParam (extensions []string ) []kernel.BrowserExtensionParam {
655+ if len (extensions ) == 0 {
656+ return nil
657+ }
658+
659+ var result []kernel.BrowserExtensionParam
660+ for _ , ext := range extensions {
661+ val := strings .TrimSpace (ext )
662+ if val == "" {
663+ continue
664+ }
665+ item := kernel.BrowserExtensionParam {}
666+ if cuidRegex .MatchString (val ) {
667+ item .ID = kernel .String (val )
668+ } else {
669+ item .Name = kernel .String (val )
670+ }
671+ result = append (result , item )
672+ }
673+ return result
674+ }
675+
676+ func buildViewportParam (viewport string ) (* kernel.BrowserViewportParam , error ) {
677+ if viewport == "" {
678+ return nil , nil
679+ }
680+
681+ width , height , refreshRate , err := parseViewport (viewport )
682+ if err != nil {
683+ return nil , fmt .Errorf ("invalid viewport format: %v" , err )
684+ }
685+
686+ vp := kernel.BrowserViewportParam {
687+ Width : width ,
688+ Height : height ,
689+ }
690+ if refreshRate > 0 {
691+ vp .RefreshRate = kernel .Int (refreshRate )
692+ }
693+ return & vp , nil
694+ }
0 commit comments