-
Notifications
You must be signed in to change notification settings - Fork 12
introducing common utils package(s) #279
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package commonutils | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func ReadFile(fileName string) ([]byte, error) { | ||
|
||
fileName = ExpandTilde(fileName) | ||
if _, err := os.Stat(fileName); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("Cannot find file %s", fileName) | ||
} | ||
data, err := ioutil.ReadFile(fileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func ReadFileAsStr(fileName string) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
content, err := ReadFile(fileName) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(content), err | ||
} | ||
|
||
func FileExists(filename string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
info, err := os.Stat(ExpandTilde(filename)) | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
return !info.IsDir() | ||
} | ||
|
||
func ExpandTilde(fileName string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
if fileName == "~" { | ||
return UserHomeDir() | ||
} else if strings.HasPrefix(fileName, "~/") { | ||
return filepath.Join(UserHomeDir(), fileName[2:]) | ||
} | ||
return fileName | ||
} | ||
|
||
func UserHomeDir() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
if runtime.GOOS == "windows" { | ||
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") | ||
if home == "" { | ||
home = os.Getenv("USERPROFILE") | ||
} | ||
return home | ||
} | ||
return os.Getenv("HOME") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package commonutils | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func DownloadFromURL(url string) ([]byte, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
c := http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
resp, err := c.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return body, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package commonutils | ||
|
||
import "net/url" | ||
|
||
func IsValidURL(strURL string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
_, err := url.ParseRequestURI(strURL) | ||
if err != nil { | ||
return false | ||
} | ||
u, err := url.Parse(strURL) | ||
if err != nil || u.Scheme == "" || u.Host == "" { | ||
return false | ||
} | ||
return true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package commonutils | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestIsValidURL(t *testing.T) { | ||
|
||
testURLS := map[string]bool{ | ||
"keptn.sh": false, | ||
"keptn..sh": false, | ||
"": false, | ||
"lakjglakbgjoejgfrlej": false, | ||
"1": false, | ||
"http://keptn.sh": true, | ||
"http://www.keptn.sh": true, | ||
"http://keptn.sh/a/b/c": true, | ||
"http://keptn.sh/a/b?c=d&e=f": true, | ||
"http://127.0.0.1/": true, | ||
} | ||
|
||
t.Parallel() | ||
|
||
for k, v := range testURLS { | ||
res := IsValidURL(k) | ||
assert.Equal(t, v, res, "Value mismatch.\nExpected: %v\nActual: %v", res, v) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[golint] reported by reviewdog 🐶
exported function ReadFile should have comment or be unexported