Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

introducing common utils package(s) #279

Merged
merged 4 commits into from
Apr 2, 2021
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
59 changes: 59 additions & 0 deletions pkg/commonutils/fileutils.go
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) {
Copy link

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


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) {
Copy link

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 ReadFileAsStr should have comment or be unexported

content, err := ReadFile(fileName)
if err != nil {
return "", err
}
return string(content), err
}

func FileExists(filename string) bool {
Copy link

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 FileExists should have comment or be unexported

info, err := os.Stat(ExpandTilde(filename))
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func ExpandTilde(fileName string) string {
Copy link

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 ExpandTilde should have comment or be unexported

if fileName == "~" {
return UserHomeDir()
} else if strings.HasPrefix(fileName, "~/") {
return filepath.Join(UserHomeDir(), fileName[2:])
}
return fileName
}

func UserHomeDir() string {
Copy link

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 UserHomeDir should have comment or be unexported

if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
23 changes: 23 additions & 0 deletions pkg/commonutils/httputils.go
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) {
Copy link

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 DownloadFromURL should have comment or be unexported

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
}
15 changes: 15 additions & 0 deletions pkg/commonutils/strutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package commonutils

import "net/url"

func IsValidURL(strURL string) bool {
Copy link

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 IsValidURL should have comment or be unexported

_, 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
}
29 changes: 29 additions & 0 deletions pkg/commonutils/strutils_test.go
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)
}
}
13 changes: 13 additions & 0 deletions pkg/lib/v0_2_0/shipyard.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package v0_2_0

import "gopkg.in/yaml.v2"

///// v0.2.0 Shipyard Spec ///////

// Shipyard describes a shipyard specification according to Keptn spec 0.2.0
Expand Down Expand Up @@ -49,3 +51,14 @@ type Trigger struct {
type Selector struct {
Match map[string]string `json:"match" yaml:"match"`
}

// DecodeShipyardYAML takes a shipyard string formatted as YAML and decodes it to
// Shipyard value
func DecodeShipyardYAML(shipyardYaml []byte) (*Shipyard, error) {
shipyardDecoded := &Shipyard{}

if err := yaml.Unmarshal(shipyardYaml, shipyardDecoded); err != nil {
return nil, err
}
return shipyardDecoded, nil
}