Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pkg/cmd/registry/artifact/crud/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"os"

"github.com/redhat-developer/app-services-cli/pkg/cmdutil"
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"

"github.com/redhat-developer/app-services-cli/pkg/color"
Expand Down Expand Up @@ -146,10 +147,18 @@ func runCreate(opts *options) error {

var specifiedFile *os.File
if opts.file != "" {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.opening.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = os.Open(opts.file)
if err != nil {
return err
if cmdutil.IsURL(opts.file) {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.loading.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = cmdutil.GetContentFromFileURL(opts.file, opts.Context)
if err != nil {
return err
}
} else {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.opening.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = os.Open(opts.file)
if err != nil {
return err
}
}
} else {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.reading.file"))
Expand Down
17 changes: 13 additions & 4 deletions pkg/cmd/registry/artifact/crud/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"

"github.com/redhat-developer/app-services-cli/pkg/cmdutil"
"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/localize"

Expand Down Expand Up @@ -118,10 +119,18 @@ func runUpdate(opts *options) error {

var specifiedFile *os.File
if opts.file != "" {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.opening.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = os.Open(opts.file)
if err != nil {
return err
if cmdutil.IsURL(opts.file) {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.loading.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = cmdutil.GetContentFromFileURL(opts.file, opts.Context)
if err != nil {
return err
}
} else {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.opening.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = os.Open(opts.file)
if err != nil {
return err
}
}
} else {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.reading.file"))
Expand Down
57 changes: 57 additions & 0 deletions pkg/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package cmdutil

import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
)

func ConvertPageValueToInt32(s string) int32 {
Expand Down Expand Up @@ -38,3 +44,54 @@ func StringSliceToListStringWithQuotes(validOptions []string) string {
}
return listF
}

// IsURL accepts a string and determines if it is a URL
func IsURL(s string) bool {
return strings.HasPrefix(s, "http:/") || strings.HasPrefix(s, "https:/")
}

// GetContentFromFileURL loads file content from the provided URL
func GetContentFromFileURL(url string, ctx context.Context) (*os.File, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ctx usually goes first in functions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like a good candidate for a unit test.


req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}

client := http.DefaultClient

resp, err := client.Do(req)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error loading file: %s", http.StatusText(resp.StatusCode))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs i18n

}

respbody := resp.Body

defer resp.Body.Close()

tmpfile, err := ioutil.TempFile("", "rhoas_file-*")
if err != nil {
return nil, fmt.Errorf("error initializing temporary file: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs i18n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a struct for all the utility methods to pass common things like localizer, context, it seems bit odd to have an additional argument for everything?

Copy link
Contributor

@craicoverflow craicoverflow Dec 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it also means we must continuously "break" APIs when new requirements are added such as i18n, which may not exist in a util but then if we need to add it we automatically break it..

A better thing to do might be to return a special error type like InitializeTemporaryFileErr and in the usage, interpret that and use i18n to create the message outside of the util..

Neither option seems ideal to me. Maybe merge it as it as its such a minor error, there's no point sacrificing usability/simplicity yet.

}

defer func() {
_ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
}()

_, err = io.Copy(tmpfile, respbody)
if err != nil {
return nil, err
}

specifiedFile, err := os.Open(tmpfile.Name())
if err != nil {
return nil, err
}

return specifiedFile, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmdutil should be strictly for utils for creating commands. These functions are technically not tied to command creation and are generally very useful anywhere, I reckon a new/separate package might be good.

3 changes: 3 additions & 0 deletions pkg/localize/locales/en/cmd/artifact.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ one = 'Using {{.DefaultArtifactGroup}} artifacts group.'
[artifact.common.message.opening.file]
one = 'Opening file: {{.FileName}}'

[artifact.common.message.loading.file]
one = 'Loading file from url: {{.FileName}}'

[artifact.common.message.file.location]
one = 'Location of the output file'

Expand Down