From e2ec3cbf69706cdc184f0af4e585d52eb6ba0297 Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Thu, 19 Dec 2024 10:53:23 +0200 Subject: [PATCH] Simplify parameter handling in plugins --- plugin/digitizer/digitize_command.go | 25 ++++++------- plugin/orchestrator/download_command.go | 31 +++++++--------- plugin/orchestrator/upload_command.go | 45 ++++++++++-------------- plugin/studio/package_analyze_command.go | 22 +++++++----- plugin/studio/package_pack_command.go | 30 +++++++++------- 5 files changed, 75 insertions(+), 78 deletions(-) diff --git a/plugin/digitizer/digitize_command.go b/plugin/digitizer/digitize_command.go index 0728cc5..3255a42 100644 --- a/plugin/digitizer/digitize_command.go +++ b/plugin/digitizer/digitize_command.go @@ -129,12 +129,9 @@ func (c DigitizeCommand) createDigitizeRequest(context plugin.ExecutionContext, var err error file := context.Input if file == nil { - file, err = c.getFileParameter(context.Parameters) - if err != nil { - return nil, err - } + file = c.getFileParameter(context.Parameters) } - contentType, _ := c.getParameter("content-type", context.Parameters) + contentType := c.getParameter("content-type", context.Parameters) if contentType == "" { contentType = "application/octet-stream" } @@ -262,33 +259,37 @@ func (c DigitizeCommand) sendRequest(request *http.Request, insecure bool) (*htt } func (c DigitizeCommand) getProjectId(parameters []plugin.ExecutionParameter) string { - projectId, _ := c.getParameter("project-id", parameters) + projectId := c.getParameter("project-id", parameters) if projectId == "" { projectId = "00000000-0000-0000-0000-000000000000" } return projectId } -func (c DigitizeCommand) getParameter(name string, parameters []plugin.ExecutionParameter) (string, error) { +func (c DigitizeCommand) getParameter(name string, parameters []plugin.ExecutionParameter) string { + result := "" for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(string); ok { - return data, nil + result = data + break } } } - return "", fmt.Errorf("Could not find '%s' parameter", name) + return result } -func (c DigitizeCommand) getFileParameter(parameters []plugin.ExecutionParameter) (utils.Stream, error) { +func (c DigitizeCommand) getFileParameter(parameters []plugin.ExecutionParameter) utils.Stream { + var result utils.Stream for _, p := range parameters { if p.Name == "file" { if stream, ok := p.Value.(utils.Stream); ok { - return stream, nil + result = stream + break } } } - return nil, fmt.Errorf("Could not find 'file' parameter") + return result } func (c DigitizeCommand) logRequest(logger log.Logger, request *http.Request) { diff --git a/plugin/orchestrator/download_command.go b/plugin/orchestrator/download_command.go index dd073e4..0cd5f02 100644 --- a/plugin/orchestrator/download_command.go +++ b/plugin/orchestrator/download_command.go @@ -119,18 +119,9 @@ func (c DownloadCommand) createReadUrlRequest(context plugin.ExecutionContext) ( if context.Tenant == "" { return nil, errors.New("Tenant is not set") } - folderId, err := c.getIntParameter("folder-id", context.Parameters) - if err != nil { - return nil, err - } - bucketId, err := c.getIntParameter("key", context.Parameters) - if err != nil { - return nil, err - } - path, err := c.getStringParameter("path", context.Parameters) - if err != nil { - return nil, err - } + folderId := c.getIntParameter("folder-id", context.Parameters) + bucketId := c.getIntParameter("key", context.Parameters) + path := c.getStringParameter("path", context.Parameters) uri := c.formatUri(context.BaseUri, context.Organization, context.Tenant) + fmt.Sprintf("/odata/Buckets(%d)/UiPath.Server.Configuration.OData.GetReadUri?path=%s", bucketId, path) request, err := http.NewRequest("GET", uri, &bytes.Buffer{}) @@ -182,26 +173,30 @@ func (c DownloadCommand) sendRequest(request *http.Request, insecure bool) (*htt return client.Do(request) } -func (c DownloadCommand) getStringParameter(name string, parameters []plugin.ExecutionParameter) (string, error) { +func (c DownloadCommand) getStringParameter(name string, parameters []plugin.ExecutionParameter) string { + result := "" for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(string); ok { - return data, nil + result = data + break } } } - return "", fmt.Errorf("Could not find '%s' parameter", name) + return result } -func (c DownloadCommand) getIntParameter(name string, parameters []plugin.ExecutionParameter) (int, error) { +func (c DownloadCommand) getIntParameter(name string, parameters []plugin.ExecutionParameter) int { + result := 0 for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(int); ok { - return data, nil + result = data + break } } } - return 0, fmt.Errorf("Could not find '%s' parameter", name) + return result } func (c DownloadCommand) logRequest(logger log.Logger, request *http.Request) { diff --git a/plugin/orchestrator/upload_command.go b/plugin/orchestrator/upload_command.go index d86b3a4..3c73c99 100644 --- a/plugin/orchestrator/upload_command.go +++ b/plugin/orchestrator/upload_command.go @@ -70,11 +70,7 @@ func (c UploadCommand) upload(context plugin.ExecutionContext, logger log.Logger func (c UploadCommand) createUploadRequest(context plugin.ExecutionContext, url string, uploadBar *utils.ProgressBar, requestError chan error) (*http.Request, error) { file := context.Input if file == nil { - var err error - file, err = c.getFileParameter(context.Parameters) - if err != nil { - return nil, err - } + file = c.getFileParameter(context.Parameters) } bodyReader, bodyWriter := io.Pipe() contentType, contentLength := c.writeBody(bodyWriter, file, requestError) @@ -160,18 +156,9 @@ func (c UploadCommand) createWriteUrlRequest(context plugin.ExecutionContext) (* if context.Tenant == "" { return nil, errors.New("Tenant is not set") } - folderId, err := c.getIntParameter("folder-id", context.Parameters) - if err != nil { - return nil, err - } - bucketId, err := c.getIntParameter("key", context.Parameters) - if err != nil { - return nil, err - } - path, err := c.getStringParameter("path", context.Parameters) - if err != nil { - return nil, err - } + folderId := c.getIntParameter("folder-id", context.Parameters) + bucketId := c.getIntParameter("key", context.Parameters) + path := c.getStringParameter("path", context.Parameters) uri := c.formatUri(context.BaseUri, context.Organization, context.Tenant) + fmt.Sprintf("/odata/Buckets(%d)/UiPath.Server.Configuration.OData.GetWriteUri?path=%s", bucketId, path) request, err := http.NewRequest("GET", uri, &bytes.Buffer{}) @@ -223,37 +210,43 @@ func (c UploadCommand) sendRequest(request *http.Request, insecure bool) (*http. return client.Do(request) } -func (c UploadCommand) getStringParameter(name string, parameters []plugin.ExecutionParameter) (string, error) { +func (c UploadCommand) getStringParameter(name string, parameters []plugin.ExecutionParameter) string { + result := "" for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(string); ok { - return data, nil + result = data + break } } } - return "", fmt.Errorf("Could not find '%s' parameter", name) + return result } -func (c UploadCommand) getIntParameter(name string, parameters []plugin.ExecutionParameter) (int, error) { +func (c UploadCommand) getIntParameter(name string, parameters []plugin.ExecutionParameter) int { + result := 0 for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(int); ok { - return data, nil + result = data + break } } } - return 0, fmt.Errorf("Could not find '%s' parameter", name) + return result } -func (c UploadCommand) getFileParameter(parameters []plugin.ExecutionParameter) (utils.Stream, error) { +func (c UploadCommand) getFileParameter(parameters []plugin.ExecutionParameter) utils.Stream { + var result utils.Stream for _, p := range parameters { if p.Name == "file" { if stream, ok := p.Value.(utils.Stream); ok { - return stream, nil + result = stream + break } } } - return nil, fmt.Errorf("Could not find 'file' parameter") + return result } func (c UploadCommand) logRequest(logger log.Logger, request *http.Request) { diff --git a/plugin/studio/package_analyze_command.go b/plugin/studio/package_analyze_command.go index b520670..66dbdfa 100644 --- a/plugin/studio/package_analyze_command.go +++ b/plugin/studio/package_analyze_command.go @@ -41,8 +41,8 @@ func (c PackageAnalyzeCommand) Execute(context plugin.ExecutionContext, writer o if err != nil { return err } - treatWarningsAsErrors, _ := c.getBoolParameter("treat-warnings-as-errors", context.Parameters) - stopOnRuleViolation, _ := c.getBoolParameter("stop-on-rule-violation", context.Parameters) + treatWarningsAsErrors := c.getBoolParameter("treat-warnings-as-errors", context.Parameters) + stopOnRuleViolation := c.getBoolParameter("stop-on-rule-violation", context.Parameters) exitCode, result, err := c.execute(source, treatWarningsAsErrors, stopOnRuleViolation, context.Debug, logger) if err != nil { return err @@ -223,7 +223,7 @@ func (c PackageAnalyzeCommand) newAnalyzingProgressBar(logger log.Logger) chan s } func (c PackageAnalyzeCommand) getSource(context plugin.ExecutionContext) (string, error) { - source, _ := c.getParameter("source", context.Parameters) + source := c.getParameter("source", context.Parameters) if source == "" { return "", errors.New("source is not set") } @@ -247,26 +247,30 @@ func (c PackageAnalyzeCommand) readOutput(output io.Reader, logger log.Logger, w } } -func (c PackageAnalyzeCommand) getParameter(name string, parameters []plugin.ExecutionParameter) (string, error) { +func (c PackageAnalyzeCommand) getParameter(name string, parameters []plugin.ExecutionParameter) string { + result := "" for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(string); ok { - return data, nil + result = data + break } } } - return "", fmt.Errorf("Could not find '%s' parameter", name) + return result } -func (c PackageAnalyzeCommand) getBoolParameter(name string, parameters []plugin.ExecutionParameter) (bool, error) { +func (c PackageAnalyzeCommand) getBoolParameter(name string, parameters []plugin.ExecutionParameter) bool { + result := false for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(bool); ok { - return data, nil + result = data + break } } } - return false, fmt.Errorf("Could not find '%s' parameter", name) + return result } func NewPackageAnalyzeCommand() *PackageAnalyzeCommand { diff --git a/plugin/studio/package_pack_command.go b/plugin/studio/package_pack_command.go index 64a6b08..6bc236d 100644 --- a/plugin/studio/package_pack_command.go +++ b/plugin/studio/package_pack_command.go @@ -48,11 +48,11 @@ func (c PackagePackCommand) Execute(context plugin.ExecutionContext, writer outp if err != nil { return err } - packageVersion, _ := c.getParameter("package-version", context.Parameters) - autoVersion, _ := c.getBoolParameter("auto-version", context.Parameters) - outputType, _ := c.getParameter("output-type", context.Parameters) - splitOutput, _ := c.getBoolParameter("split-output", context.Parameters) - releaseNotes, _ := c.getParameter("release-notes", context.Parameters) + packageVersion := c.getParameter("package-version", context.Parameters) + autoVersion := c.getBoolParameter("auto-version", context.Parameters) + outputType := c.getParameter("output-type", context.Parameters) + splitOutput := c.getBoolParameter("split-output", context.Parameters) + releaseNotes := c.getParameter("release-notes", context.Parameters) params := newPackagePackParams(source, destination, packageVersion, autoVersion, outputType, splitOutput, releaseNotes) result, err := c.execute(*params, context.Debug, logger) @@ -199,7 +199,7 @@ func (c PackagePackCommand) newPackagingProgressBar(logger log.Logger) chan stru } func (c PackagePackCommand) getSource(context plugin.ExecutionContext) (string, error) { - source, _ := c.getParameter("source", context.Parameters) + source := c.getParameter("source", context.Parameters) if source == "" { return "", errors.New("source is not set") } @@ -234,7 +234,7 @@ func (c PackagePackCommand) readProjectJson(path string) (projectJson, error) { } func (c PackagePackCommand) getDestination(context plugin.ExecutionContext) (string, error) { - destination, _ := c.getParameter("destination", context.Parameters) + destination := c.getParameter("destination", context.Parameters) if destination == "" { return "", errors.New("destination is not set") } @@ -251,26 +251,30 @@ func (c PackagePackCommand) readOutput(output io.Reader, logger log.Logger, wg * } } -func (c PackagePackCommand) getParameter(name string, parameters []plugin.ExecutionParameter) (string, error) { +func (c PackagePackCommand) getParameter(name string, parameters []plugin.ExecutionParameter) string { + result := "" for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(string); ok { - return data, nil + result = data + break } } } - return "", fmt.Errorf("Could not find '%s' parameter", name) + return result } -func (c PackagePackCommand) getBoolParameter(name string, parameters []plugin.ExecutionParameter) (bool, error) { +func (c PackagePackCommand) getBoolParameter(name string, parameters []plugin.ExecutionParameter) bool { + result := false for _, p := range parameters { if p.Name == name { if data, ok := p.Value.(bool); ok { - return data, nil + result = data + break } } } - return false, fmt.Errorf("Could not find '%s' parameter", name) + return result } func NewPackagePackCommand() *PackagePackCommand {