Skip to content

Commit

Permalink
Merge pull request #140 from UiPath/feature/utils-split-up
Browse files Browse the repository at this point in the history
Split up utils into sub-packages
  • Loading branch information
thschmitt authored Jan 23, 2025
2 parents 72e658f + 6dc70ec commit 8086db0
Show file tree
Hide file tree
Showing 53 changed files with 385 additions and 386 deletions.
6 changes: 3 additions & 3 deletions auth/browser_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"time"

"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/process"
)

// BrowserLauncher tries to open the default browser on the local system.
type BrowserLauncher struct {
Exec utils.ExecProcess
Exec process.ExecProcess
}

func (l BrowserLauncher) Open(url string) error {
Expand All @@ -32,5 +32,5 @@ func (l BrowserLauncher) Open(url string) error {
}

func NewBrowserLauncher() *BrowserLauncher {
return &BrowserLauncher{utils.NewExecProcess()}
return &BrowserLauncher{process.NewExecProcess()}
}
6 changes: 2 additions & 4 deletions auth/browser_launcher_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

package auth

import (
"github.com/UiPath/uipathcli/utils"
)
import "github.com/UiPath/uipathcli/utils/process"

func (l BrowserLauncher) openBrowser(url string) utils.ExecCmd {
func (l BrowserLauncher) openBrowser(url string) process.ExecCmd {
return l.Exec.Command("open", url)
}
6 changes: 2 additions & 4 deletions auth/browser_launcher_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

package auth

import (
"github.com/UiPath/uipathcli/utils"
)
import "github.com/UiPath/uipathcli/utils/process"

func (l BrowserLauncher) openBrowser(url string) utils.ExecCmd {
func (l BrowserLauncher) openBrowser(url string) process.ExecCmd {
return l.Exec.Command("xdg-open", url)
}
6 changes: 2 additions & 4 deletions auth/browser_launcher_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

package auth

import (
"github.com/UiPath/uipathcli/utils"
)
import "github.com/UiPath/uipathcli/utils/process"

func (l BrowserLauncher) openBrowser(url string) utils.ExecCmd {
func (l BrowserLauncher) openBrowser(url string) process.ExecCmd {
return l.Exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
}
10 changes: 5 additions & 5 deletions auth/identity_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

"github.com/UiPath/uipathcli/cache"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/resiliency"
)

type identityClient struct {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (c identityClient) GetToken(tokenRequest tokenRequest) (*tokenResponse, err

func (c identityClient) retrieveToken(baseUri url.URL, form url.Values, insecure bool) (*tokenResponse, error) {
var response *tokenResponse
err := utils.Retry(func() error {
err := resiliency.Retry(func() error {
var err error
response, err = c.send(baseUri, form, insecure)
return err
Expand All @@ -70,15 +70,15 @@ func (c identityClient) send(baseUri url.URL, form url.Values, insecure bool) (*
client := http.Client{Transport: transport}
response, err := client.Do(request)
if err != nil {
return nil, utils.Retryable(fmt.Errorf("Error sending request: %w", err))
return nil, resiliency.Retryable(fmt.Errorf("Error sending request: %w", err))
}
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, utils.Retryable(fmt.Errorf("Error reading response: %w", err))
return nil, resiliency.Retryable(fmt.Errorf("Error reading response: %w", err))
}
if response.StatusCode >= 500 {
return nil, utils.Retryable(fmt.Errorf("Token service returned status code '%v' and body '%v'", response.StatusCode, string(bytes)))
return nil, resiliency.Retryable(fmt.Errorf("Token service returned status code '%v' and body '%v'", response.StatusCode, string(bytes)))
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Token service returned status code '%v' and body '%v'", response.StatusCode, string(bytes))
Expand Down
4 changes: 2 additions & 2 deletions auth/oauth_authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"testing"

"github.com/UiPath/uipathcli/cache"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/process"
)

func TestOAuthAuthenticatorNotEnabled(t *testing.T) {
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestMissingCodeShowsErrorMessage(t *testing.T) {
func callAuthenticator(context AuthenticatorContext) (url.URL, chan AuthenticatorResult) {
loginChan := make(chan string)
authenticator := NewOAuthAuthenticator(cache.NewFileCache(), BrowserLauncher{
Exec: utils.NewExecCustomProcess(0, "", "", func(name string, args []string) {
Exec: process.NewExecCustomProcess(0, "", "", func(name string, args []string) {
switch runtime.GOOS {
case "windows":
loginChan <- args[1]
Expand Down
4 changes: 2 additions & 2 deletions cache/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"time"

"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/directories"
)

const cacheFilePermissions = 0600
Expand Down Expand Up @@ -63,7 +63,7 @@ func (c FileCache) readValue(key string) (int64, string, error) {
}

func (c FileCache) cacheFilePath(key string) (string, error) {
cacheDirectory, err := utils.Directories{}.Cache()
cacheDirectory, err := directories.Cache()
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions commandline/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/UiPath/uipathcli/config"
"github.com/UiPath/uipathcli/executor"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/stream"
"github.com/urfave/cli/v2"
)

Expand All @@ -24,7 +24,7 @@ type Cli struct {
pluginExecutor executor.Executor
}

func (c Cli) run(args []string, input utils.Stream) error {
func (c Cli) run(args []string, input stream.Stream) error {
err := c.configProvider.Load()
if err != nil {
return err
Expand Down Expand Up @@ -78,7 +78,7 @@ func (c Cli) run(args []string, input utils.Stream) error {
const colorRed = "\033[31m"
const colorReset = "\033[0m"

func (c Cli) Run(args []string, input utils.Stream) error {
func (c Cli) Run(args []string, input stream.Stream) error {
err := c.run(args, input)
if err != nil {
message := err.Error()
Expand Down
8 changes: 4 additions & 4 deletions commandline/command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"github.com/UiPath/uipathcli/log"
"github.com/UiPath/uipathcli/output"
"github.com/UiPath/uipathcli/parser"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/stream"
)

// The CommandBuilder is creating all available operations and arguments for the CLI.
type CommandBuilder struct {
Input utils.Stream
Input stream.Stream
StdIn io.Reader
StdOut io.Writer
StdErr io.Writer
Expand All @@ -37,7 +37,7 @@ func (b CommandBuilder) sort(commands []*CommandDefinition) {
})
}

func (b CommandBuilder) fileInput(context *CommandExecContext, parameters []parser.Parameter) utils.Stream {
func (b CommandBuilder) fileInput(context *CommandExecContext, parameters []parser.Parameter) stream.Stream {
value := context.String(FlagNameFile)
if value == "" {
return nil
Expand All @@ -50,7 +50,7 @@ func (b CommandBuilder) fileInput(context *CommandExecContext, parameters []pars
return nil
}
}
return utils.NewFileStream(value)
return stream.NewFileStream(value)
}

func (b CommandBuilder) createExecutionParameters(context *CommandExecContext, config *config.Config, operation parser.Operation) (executor.ExecutionParameters, error) {
Expand Down
6 changes: 3 additions & 3 deletions commandline/type_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/UiPath/uipathcli/parser"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/stream"
)

// The typeConverter converts the string value from the command-line argument into the type
Expand Down Expand Up @@ -53,8 +53,8 @@ func (c typeConverter) convertToBoolean(value string, parameter parser.Parameter
return false, fmt.Errorf("Cannot convert '%s' value '%s' to boolean", parameter.Name, value)
}

func (c typeConverter) convertToBinary(value string, parameter parser.Parameter) (utils.Stream, error) {
return utils.NewFileStream(value), nil
func (c typeConverter) convertToBinary(value string, parameter parser.Parameter) (stream.Stream, error) {
return stream.NewFileStream(value), nil
}

func (c typeConverter) findParameter(parameter *parser.Parameter, name string) *parser.Parameter {
Expand Down
4 changes: 2 additions & 2 deletions commandline/type_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/UiPath/uipathcli/parser"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/stream"
)

func TestConvertReturnsErrorForInvalidBoolean(t *testing.T) {
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestConvertStringToFileStream(t *testing.T) {
if err != nil {
t.Errorf("Should not return error, but got: %v", err)
}
fileStream := result.(*utils.FileStream)
fileStream := result.(*stream.FileStream)
if fileStream.Name() != "test.txt" {
t.Errorf("Result should be file stream, but got: %v", result)
}
Expand Down
6 changes: 3 additions & 3 deletions executor/execution_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/UiPath/uipathcli/config"
"github.com/UiPath/uipathcli/plugin"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/stream"
)

// The ExecutionContext provides all the data needed by the executor to construct the HTTP
Expand All @@ -17,7 +17,7 @@ type ExecutionContext struct {
BaseUri url.URL
Route string
ContentType string
Input utils.Stream
Input stream.Stream
Parameters ExecutionParameters
AuthConfig config.AuthConfig
Insecure bool
Expand All @@ -33,7 +33,7 @@ func NewExecutionContext(
uri url.URL,
route string,
contentType string,
input utils.Stream,
input stream.Stream,
parameters []ExecutionParameter,
authConfig config.AuthConfig,
insecure bool,
Expand Down
47 changes: 28 additions & 19 deletions executor/http_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/UiPath/uipathcli/log"
"github.com/UiPath/uipathcli/output"
"github.com/UiPath/uipathcli/utils"
"github.com/UiPath/uipathcli/utils/converter"
"github.com/UiPath/uipathcli/utils/resiliency"
"github.com/UiPath/uipathcli/utils/stream"
"github.com/UiPath/uipathcli/utils/visualization"
)

const NotConfiguredErrorTemplate = `Run config command to set organization and tenant:
Expand All @@ -40,7 +44,7 @@ type HttpExecutor struct {
}

func (e HttpExecutor) Call(context ExecutionContext, writer output.OutputWriter, logger log.Logger) error {
return utils.Retry(func() error {
return resiliency.Retry(func() error {
return e.call(context, writer, logger)
})
}
Expand All @@ -52,11 +56,11 @@ func (e HttpExecutor) requestId() string {
}

func (e HttpExecutor) addHeaders(request *http.Request, headerParameters []ExecutionParameter) {
formatter := newParameterFormatter()
converter := converter.NewStringConverter()
request.Header.Add("x-request-id", e.requestId())
request.Header.Add("User-Agent", UserAgent)
for _, parameter := range headerParameters {
headerValue := formatter.Format(parameter)
headerValue := converter.ToString(parameter.Value)
request.Header.Add(parameter.Name, headerValue)
}
}
Expand All @@ -67,7 +71,7 @@ func (e HttpExecutor) calculateMultipartSize(parameters []ExecutionParameter) in
switch v := parameter.Value.(type) {
case string:
result = result + int64(len(v))
case utils.Stream:
case stream.Stream:
size, err := v.Size()
if err == nil {
result = result + size
Expand All @@ -89,7 +93,7 @@ func (e HttpExecutor) writeMultipartForm(writer *multipart.Writer, parameters []
if err != nil {
return fmt.Errorf("Error writing form field '%s': %w", parameter.Name, err)
}
case utils.Stream:
case stream.Stream:
w, err := writer.CreateFormFile(parameter.Name, v.Name())
if err != nil {
return fmt.Errorf("Error writing form file '%s': %w", parameter.Name, err)
Expand Down Expand Up @@ -140,12 +144,14 @@ func (e HttpExecutor) validateUri(uri string) (*url.URL, error) {
}

func (e HttpExecutor) formatUri(baseUri url.URL, route string, pathParameters []ExecutionParameter, queryParameters []ExecutionParameter) (*url.URL, error) {
formatter := newUriFormatter(baseUri, route)
uriBuilder := converter.NewUriBuilder(baseUri, route)
for _, parameter := range pathParameters {
formatter.FormatPath(parameter)
uriBuilder.FormatPath(parameter.Name, parameter.Value)
}
formatter.AddQueryString(queryParameters)
return e.validateUri(formatter.Uri())
for _, parameter := range queryParameters {
uriBuilder.AddQueryString(parameter.Name, parameter.Value)
}
return e.validateUri(uriBuilder.Build())
}

func (e HttpExecutor) executeAuthenticators(authConfig config.AuthConfig, identityUri url.URL, debug bool, insecure bool, request *http.Request) (*auth.AuthenticatorResult, error) {
Expand All @@ -164,11 +170,11 @@ func (e HttpExecutor) executeAuthenticators(authConfig config.AuthConfig, identi
return auth.AuthenticatorSuccess(ctx.Request.Header, ctx.Config), nil
}

func (e HttpExecutor) progressReader(text string, completedText string, reader io.Reader, length int64, progressBar *utils.ProgressBar) io.Reader {
func (e HttpExecutor) progressReader(text string, completedText string, reader io.Reader, length int64, progressBar *visualization.ProgressBar) io.Reader {
if length < 10*1024*1024 {
return reader
}
progressReader := utils.NewProgressReader(reader, func(progress utils.Progress) {
progressReader := visualization.NewProgressReader(reader, func(progress visualization.Progress) {
displayText := text
if progress.Completed {
displayText = completedText
Expand All @@ -193,7 +199,7 @@ func (e HttpExecutor) writeMultipartBody(bodyWriter *io.PipeWriter, parameters [
return formWriter.FormDataContentType(), multipartSize
}

func (e HttpExecutor) writeInputBody(bodyWriter *io.PipeWriter, input utils.Stream, errorChan chan error) {
func (e HttpExecutor) writeInputBody(bodyWriter *io.PipeWriter, input stream.Stream, errorChan chan error) {
go func() {
defer bodyWriter.Close()
data, err := input.Data()
Expand All @@ -213,8 +219,11 @@ func (e HttpExecutor) writeInputBody(bodyWriter *io.PipeWriter, input utils.Stre
func (e HttpExecutor) writeUrlEncodedBody(bodyWriter *io.PipeWriter, parameters []ExecutionParameter, errorChan chan error) {
go func() {
defer bodyWriter.Close()
formatter := newQueryStringFormatter()
queryString := formatter.Format(parameters)
queryStringBuilder := converter.NewQueryStringBuilder()
for _, parameter := range parameters {
queryStringBuilder.Add(parameter.Name, parameter.Value)
}
queryString := queryStringBuilder.Build()
_, err := bodyWriter.Write([]byte(queryString))
if err != nil {
errorChan <- err
Expand Down Expand Up @@ -312,7 +321,7 @@ func (e HttpExecutor) call(context ExecutionContext, writer output.OutputWriter,
}
requestError := make(chan error)
bodyReader, contentType, contentLength, size := e.writeBody(context, requestError)
uploadBar := utils.NewProgressBar(logger)
uploadBar := visualization.NewProgressBar(logger)
uploadReader := e.progressReader("uploading...", "completing ", bodyReader, size, uploadBar)
defer uploadBar.Remove()
request, err := http.NewRequest(context.Method, uri.String(), uploadReader)
Expand Down Expand Up @@ -344,19 +353,19 @@ func (e HttpExecutor) call(context ExecutionContext, writer output.OutputWriter,
}
response, err := e.send(client, request, requestError)
if err != nil {
return utils.Retryable(fmt.Errorf("Error sending request: %w", err))
return resiliency.Retryable(fmt.Errorf("Error sending request: %w", err))
}
defer response.Body.Close()
downloadBar := utils.NewProgressBar(logger)
downloadBar := visualization.NewProgressBar(logger)
downloadReader := e.progressReader("downloading...", "completing ", response.Body, response.ContentLength, downloadBar)
defer downloadBar.Remove()
body, err := io.ReadAll(downloadReader)
if err != nil {
return utils.Retryable(fmt.Errorf("Error reading response body: %w", err))
return resiliency.Retryable(fmt.Errorf("Error reading response body: %w", err))
}
e.logResponse(logger, response, body)
if response.StatusCode >= 500 {
return utils.Retryable(fmt.Errorf("Service returned status code '%v' and body '%v'", response.StatusCode, string(body)))
return resiliency.Retryable(fmt.Errorf("Service returned status code '%v' and body '%v'", response.StatusCode, string(body)))
}
err = writer.WriteResponse(*output.NewResponseInfo(response.StatusCode, response.Status, response.Proto, response.Header, bytes.NewReader(body)))
if err != nil {
Expand Down
Loading

0 comments on commit 8086db0

Please sign in to comment.