Skip to content

Commit

Permalink
Simplify parameter handling in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
thschmitt committed Dec 19, 2024
1 parent 9a50fb4 commit e2ec3cb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 78 deletions.
25 changes: 13 additions & 12 deletions plugin/digitizer/digitize_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 13 additions & 18 deletions plugin/orchestrator/download_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 19 additions & 26 deletions plugin/orchestrator/upload_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 13 additions & 9 deletions plugin/studio/package_analyze_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down
30 changes: 17 additions & 13 deletions plugin/studio/package_pack_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down

0 comments on commit e2ec3cb

Please sign in to comment.