-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ExecCmd abstraction for browserlauncher
- Loading branch information
Showing
14 changed files
with
156 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,36 @@ | ||
package auth | ||
|
||
// BrowserLauncher interface for opening browser windows. | ||
type BrowserLauncher interface { | ||
Open(url string) error | ||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/UiPath/uipathcli/utils" | ||
) | ||
|
||
// BrowserLauncher tries to open the default browser on the local system. | ||
type BrowserLauncher struct { | ||
Exec utils.ExecProcess | ||
} | ||
|
||
func (l BrowserLauncher) Open(url string) error { | ||
cmd := l.openBrowser(url) | ||
err := cmd.Start() | ||
if err != nil { | ||
return err | ||
} | ||
done := make(chan error) | ||
go func() { | ||
done <- cmd.Wait() | ||
}() | ||
|
||
select { | ||
case err := <-done: | ||
return err | ||
case <-time.After(5 * time.Second): | ||
return fmt.Errorf("Timed out waiting for browser to start") | ||
} | ||
} | ||
|
||
func NewBrowserLauncher() *BrowserLauncher { | ||
return &BrowserLauncher{utils.NewExecProcess()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build darwin | ||
|
||
package auth | ||
|
||
import ( | ||
"github.com/UiPath/uipathcli/utils" | ||
) | ||
|
||
func (l BrowserLauncher) openBrowser(url string) utils.ExecCmd { | ||
return l.Exec.Command("open", url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build linux | ||
|
||
package auth | ||
|
||
import ( | ||
"github.com/UiPath/uipathcli/utils" | ||
) | ||
|
||
func (l BrowserLauncher) openBrowser(url string) utils.ExecCmd { | ||
return l.Exec.Command("xdg-open", url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build windows | ||
|
||
package auth | ||
|
||
import ( | ||
"github.com/UiPath/uipathcli/utils" | ||
) | ||
|
||
func (l BrowserLauncher) openBrowser(url string) utils.ExecCmd { | ||
return l.Exec.Command("rundll32", "url.dll,FileProtocolHandler", url) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type ExecCustomCmd struct { | ||
Name string | ||
Args []string | ||
Exit int | ||
StdOut io.ReadCloser | ||
StdErr io.ReadCloser | ||
OnStart func(name string, args []string) | ||
} | ||
|
||
func (c ExecCustomCmd) StdoutPipe() (io.ReadCloser, error) { | ||
return c.StdOut, nil | ||
} | ||
|
||
func (c ExecCustomCmd) StderrPipe() (io.ReadCloser, error) { | ||
return c.StdErr, nil | ||
} | ||
|
||
func (c ExecCustomCmd) Start() error { | ||
c.OnStart(c.Name, c.Args) | ||
return nil | ||
} | ||
|
||
func (c ExecCustomCmd) Wait() error { | ||
return nil | ||
} | ||
|
||
func (c ExecCustomCmd) ExitCode() int { | ||
return c.Exit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
) | ||
|
||
type ExecCustomProcess struct { | ||
ExitCode int | ||
Stdout string | ||
Stderr string | ||
OnStart func(name string, args []string) | ||
} | ||
|
||
func (e ExecCustomProcess) Command(name string, args ...string) ExecCmd { | ||
return ExecCustomCmd{ | ||
Name: name, | ||
Args: args, | ||
StdOut: io.NopCloser(strings.NewReader(e.Stdout)), | ||
StdErr: io.NopCloser(strings.NewReader(e.Stderr)), | ||
Exit: e.ExitCode, | ||
OnStart: e.OnStart, | ||
} | ||
} | ||
|
||
func NewExecCustomProcess(exitCode int, stdout string, stderr string, onStart func(name string, args []string)) *ExecCustomProcess { | ||
return &ExecCustomProcess{ | ||
ExitCode: exitCode, | ||
Stdout: stdout, | ||
Stderr: stderr, | ||
OnStart: onStart, | ||
} | ||
} |