Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions pkg/cmd/registry/artifact/crud/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,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 util.IsURL(opts.file) {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.loading.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = util.GetContentFromFileURL(opts.Context, opts.file)
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
16 changes: 12 additions & 4 deletions pkg/cmd/registry/artifact/crud/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,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 util.IsURL(opts.file) {
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.loading.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = util.GetContentFromFileURL(opts.Context, opts.file)
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
52 changes: 52 additions & 0 deletions pkg/cmd/registry/artifact/util/files.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package util
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it pre-exists this PR, but going forward we should not call packages "util", and we should update it when we see them.


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

func CreateFileFromStdin() (*os.File, error) {
Expand All @@ -26,3 +30,51 @@ func CreateFileFromStdin() (*os.File, error) {
}
return specifiedFile, nil
}

// 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(ctx context.Context, url string) (*os.File, error) {

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.

i18n?

}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

defer resp.Body.Close()

tmpfile, err := ioutil.TempFile("", "rhoas-std-input")
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.

i18n?

}

_, err = (*tmpfile).Write(data)
if err != nil {
return nil, err
}
_, err = (*tmpfile).Seek(0, io.SeekStart)
if err != nil {
return nil, err
}

return tmpfile, nil
}
89 changes: 89 additions & 0 deletions pkg/cmd/registry/artifact/util/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package util

import (
"context"
"reflect"
"testing"
)

func TestIsURL(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Should return true for url starting with https",
args: args{
path: "https://bu98.serviceregistry-stage.rhcloud.com/t/8ecff228-1ffe-4cf5-b38b-55223885ee00/apis/registry/v2",
},
want: true,
},
{
name: "Should return true for url starting with http",
args: args{
path: "http://localhost:8082/",
},
want: true,
},
{
name: "Should return false for regular file path",
args: args{
path: "./schema/artifact.json",
},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsURL(tt.args.path); !reflect.DeepEqual(got, tt.want) {
t.Errorf("IsURL(%v) = %v, want %v", tt.args.path, got, tt.want)
}
})
}
}

func TestGetContentFromFileURL(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Should load file content from valid url",
args: args{
path: "https://raw.githubusercontent.com/bolcom/avro-schema-viewer/master/docs/assets/avsc/1.0/schema.avsc",
},
wantErr: false,
},
{
name: "Should throw error if URL is not found",
args: args{
path: "https://test-123-test-404.com/test",
},
wantErr: true,
},
{
name: "Should throw error if argument is not a URL",
args: args{
path: "./schema/artifact.json",
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := GetContentFromFileURL(context.TODO(), tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("GetContentFromFileURL() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
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