Skip to content

Commit 776ab36

Browse files
Extracting OpenBrowser to it's own function
1 parent 9c4b87d commit 776ab36

2 files changed

Lines changed: 90 additions & 79 deletions

File tree

client-programs/pkg/cmd/cluster_workshop_deploy_cmd.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,23 @@ func (o *ClusterWorkshopDeployOptions) Run() error {
132132
Registry: o.Repository,
133133
Environ: o.Environ,
134134
Labels: o.Labels,
135-
OpenBrowser: o.OpenBrowser,
136135
}
137136
err = manager.DeployWorkshopResource(&deployConfig)
138137

139-
// TODO: Move open browser logic to separate function and extract logic here
140-
// if o.OpenBrowser {
141-
// err = manager.OpenBrowser(&deployConfig)
142-
// if err != nil {
143-
// return err
144-
// }
145-
// }
146-
147138
if err != nil {
148139
return err
149140
}
150141

142+
openBrowserConfig := workshops.OpenBrowserConfig{
143+
Portal: o.Portal,
144+
}
145+
if o.OpenBrowser {
146+
err = manager.OpenBrowser(&openBrowserConfig)
147+
if err != nil {
148+
return err
149+
}
150+
}
151+
151152
return nil
152153
}
153154

client-programs/pkg/educates/resources/workshops/manager.go

Lines changed: 80 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ type DeployWorkshopConfig struct {
5858
Registry string
5959
Environ []string
6060
Labels []string
61-
OpenBrowser bool
6261
}
6362

6463
type UpdateWorkshopResourceConfig struct {
@@ -84,6 +83,10 @@ type DeleteWorkshopResourceConfig struct {
8483
Portal string
8584
}
8685

86+
type OpenBrowserConfig struct {
87+
Portal string
88+
}
89+
8790
func (m *WorkshopManager) DeployWorkshopResource(o *DeployWorkshopConfig) error {
8891
trainingPortalClient := m.Client.Resource(educatesTypes.TrainingPortalResource)
8992

@@ -344,75 +347,6 @@ func (m *WorkshopManager) DeployWorkshopResource(o *DeployWorkshopConfig) error
344347

345348
fmt.Print("Workshop added to training portal.\n")
346349

347-
if o.OpenBrowser {
348-
// Need to refetch training portal because if was just created the URL
349-
// for access may not have been set yet.
350-
351-
var targetUrl string
352-
353-
fmt.Print("Checking training portal is ready.\n")
354-
355-
spinner := func(iteration int) string {
356-
spinners := `|/-\`
357-
return string(spinners[iteration%len(spinners)])
358-
}
359-
360-
for i := 1; i < 60; i++ {
361-
fmt.Printf("\r[%s] Waiting...", spinner(i))
362-
363-
time.Sleep(time.Second)
364-
365-
trainingPortal, err = trainingPortalClient.Get(context.TODO(), o.Portal, metav1.GetOptions{})
366-
367-
if err != nil {
368-
return errors.Wrapf(err, "unable to fetch training portal %q in cluster", o.Portal)
369-
}
370-
371-
var found bool
372-
373-
targetUrl, found, _ = unstructured.NestedString(trainingPortal.Object, "status", "educates", "url")
374-
375-
if found {
376-
break
377-
}
378-
}
379-
380-
rootUrl := targetUrl
381-
382-
password, _, _ := unstructured.NestedString(trainingPortal.Object, "spec", "portal", "password")
383-
384-
if password != "" {
385-
values := url.Values{}
386-
values.Add("redirect_url", "/")
387-
values.Add("password", password)
388-
389-
targetUrl = fmt.Sprintf("%s/workshops/access/?%s", targetUrl, values.Encode())
390-
}
391-
392-
for i := 1; i < 300; i++ {
393-
fmt.Printf("\r[%s] Waiting...", spinner(i))
394-
395-
time.Sleep(time.Second)
396-
397-
resp, err := http.Get(rootUrl)
398-
399-
if err != nil || resp.StatusCode == 503 {
400-
continue
401-
}
402-
403-
defer resp.Body.Close()
404-
io.ReadAll(resp.Body)
405-
406-
break
407-
}
408-
409-
fmt.Print("\r \r")
410-
411-
fmt.Printf("Opening training portal %s.\n", targetUrl)
412-
413-
return utils.OpenBrowser(targetUrl)
414-
}
415-
416350
return nil
417351
}
418352

@@ -538,6 +472,82 @@ func (m *WorkshopManager) DeleteWorkshopResource(o *DeleteWorkshopResourceConfig
538472
return nil
539473
}
540474

475+
func (m *WorkshopManager) OpenBrowser(o *OpenBrowserConfig) error {
476+
trainingPortalClient := m.Client.Resource(educatesTypes.TrainingPortalResource)
477+
478+
// Need to refetch training portal because if was just created the URL
479+
// for access may not have been set yet.
480+
481+
var targetUrl string
482+
483+
fmt.Print("Checking training portal is ready.\n")
484+
485+
spinner := func(iteration int) string {
486+
spinners := `|/-\`
487+
return string(spinners[iteration%len(spinners)])
488+
}
489+
490+
var trainingPortal *unstructured.Unstructured
491+
var found bool
492+
var err error
493+
494+
for i := 1; i < 60; i++ {
495+
fmt.Printf("\r[%s] Waiting...", spinner(i))
496+
497+
time.Sleep(time.Second)
498+
499+
trainingPortal, err = trainingPortalClient.Get(context.TODO(), o.Portal, metav1.GetOptions{})
500+
501+
if err != nil {
502+
return errors.Wrapf(err, "unable to fetch training portal %q in cluster", o.Portal)
503+
}
504+
505+
targetUrl, found, _ = unstructured.NestedString(trainingPortal.Object, "status", "educates", "url")
506+
507+
if found {
508+
break
509+
}
510+
}
511+
if !found {
512+
return errors.New("training portal not found")
513+
}
514+
515+
rootUrl := targetUrl
516+
517+
password, _, _ := unstructured.NestedString(trainingPortal.Object, "spec", "portal", "password")
518+
519+
if password != "" {
520+
values := url.Values{}
521+
values.Add("redirect_url", "/")
522+
values.Add("password", password)
523+
524+
targetUrl = fmt.Sprintf("%s/workshops/access/?%s", targetUrl, values.Encode())
525+
}
526+
527+
for i := 1; i < 300; i++ {
528+
fmt.Printf("\r[%s] Waiting...", spinner(i))
529+
530+
time.Sleep(time.Second)
531+
532+
resp, err := http.Get(rootUrl)
533+
534+
if err != nil || resp.StatusCode == 503 {
535+
continue
536+
}
537+
538+
defer resp.Body.Close()
539+
io.ReadAll(resp.Body)
540+
541+
break
542+
}
543+
544+
fmt.Print("\r \r")
545+
546+
fmt.Printf("Opening training portal %s.\n", targetUrl)
547+
548+
return utils.OpenBrowser(targetUrl)
549+
}
550+
541551
func LoadWorkshopDefinition(o *WorkshopDefinitionConfig) (*unstructured.Unstructured, error) {
542552
// Parse the workshop location so we can determine if it is a local file
543553
// or accessible using a HTTP/HTTPS URL.

0 commit comments

Comments
 (0)