Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert to go modules, fix linters issues #19

Merged
merged 3 commits into from
Nov 8, 2019
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

```bash
$ go get github.com/screwdriver-cd/sd-step
$ cd $GOPATH/src/github.com/screwdriver-cd/sd-step
$ go build -a -o sd-step
$ ./sd-step --help
NAME:
sd-step - wrapper command of habitat for Screwdriver
Expand Down
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Sd-step is a wrapper command of habitat for Screwdriver.
package main
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/screwdriver-cd/sd-step

go 1.13

require (
github.com/Masterminds/semver v1.5.0
github.com/urfave/cli v1.22.1
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3 changes: 3 additions & 0 deletions hab/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package hab provides utility functions for working with Habitat
// https://www.habitat.sh.
package hab
17 changes: 9 additions & 8 deletions hab/hab.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"time"
)

// PackagesInfo is response from depotj
// PackagesInfo is response from depot.
type PackagesInfo struct {
RangeStart int `json:"range_start"`
RangeEnd int `json:"range_end"`
TotalCount int `json:"total_count"`
PackageList []PackageInfo `json:"data"`
}

// PackageInfo is package info in pkgs response
// PackageInfo is package info in pkgs response.
type PackageInfo struct {
Origin string `json:"origin"`
Name string `json:"name"`
Expand All @@ -25,7 +25,7 @@ type PackageInfo struct {
Channels []string `json:"channels"`
}

// Depot for hab
// Depot for hab.
type Depot interface {
PackageVersionsFromName(pkgName string, habChannel string) ([]string, error)
}
Expand All @@ -35,7 +35,7 @@ type depot struct {
client *http.Client
}

// New returns a new depot object
// New returns a new depot object.
func New(baseURL string) Depot {
return &depot{baseURL, &http.Client{Timeout: 20 * time.Second}}
}
Expand All @@ -52,9 +52,10 @@ func (depo *depot) packagesInfo(pkgName string, from int) (PackagesInfo, error)
defer res.Body.Close()

if res.StatusCode == 404 {
return PackagesInfo{}, errors.New("Package not found")
} else if res.StatusCode/100 != 2 {
return PackagesInfo{}, fmt.Errorf("Unexpected status code: %d", res.StatusCode)
return PackagesInfo{}, errors.New("package not found")
}
if res.StatusCode/100 != 2 {
return PackagesInfo{}, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}

var pkgsInfo PackagesInfo
Expand All @@ -67,7 +68,7 @@ func (depo *depot) packagesInfo(pkgName string, from int) (PackagesInfo, error)
return pkgsInfo, nil
}

// PackageVersionsFromName fetch all versions from depot
// PackageVersionsFromName fetches all versions from depot.
func (depo *depot) PackageVersionsFromName(pkgName string, habChannel string) ([]string, error) {
var packages []PackageInfo

Expand Down
12 changes: 8 additions & 4 deletions hab/hab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ func makeFakeHTTPClient(t *testing.T, data testData) *http.Client {
return &http.Client{Transport: transport}
}

type fakeStruct struct{}

func jsonError(JSON string) error {
return json.Unmarshal([]byte(JSON), nil)
// "dummy" object to avoid `go vet` error when passing nil
dummy := &fakeStruct{}
return json.Unmarshal([]byte(JSON), dummy)
}

func TestPackagesInfoFromName(t *testing.T) {
Expand Down Expand Up @@ -402,7 +406,7 @@ func TestPackagesInfoFromName(t *testing.T) {
expected: nil,
statusCode: 404,
httpError: nil,
expectedError: errors.New("Package not found"),
expectedError: errors.New("package not found"),
},
{
packageName: "foo/test",
Expand All @@ -418,7 +422,7 @@ func TestPackagesInfoFromName(t *testing.T) {
expected: nil,
statusCode: 500,
httpError: nil,
expectedError: errors.New("Unexpected status code: 500"),
expectedError: errors.New("unexpected status code: 500"),
},
{
packageName: "foo/test",
Expand All @@ -434,7 +438,7 @@ func TestPackagesInfoFromName(t *testing.T) {
expected: nil,
statusCode: 500,
httpError: nil,
expectedError: errors.New("Unexpected status code: 500"),
expectedError: errors.New("unexpected status code: 500"),
},
{
packageName: "foo/test",
Expand Down
20 changes: 14 additions & 6 deletions screwdriver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ jobs:
main:
requires: [~commit, ~pr]
steps:
- get: go get -t ./...
- modverify: go version; go mod verify
- gofmt: |
gofmtOutput=$(gofmt -l .)
if [ -z "${gofmtOutput}" ]; then
echo "OK"
else
echo "gofmt violations in the following file(s):"
echo "${gofmtOutput}\n"
gofmt -d .
exit 1
fi
- vet: go vet ./...
- gofmt: "find . -name '*.go' | xargs gofmt -s -w"
- test: go test ./...
- build: go build -a -o sd-step
- build: go install -v ./...
- test: go test -v ./...

deploy:
requires: main
steps:
- setup-ci: git clone https://github.com/screwdriver-cd/toolbox.git ci
- get: go get -t ./...
- gofmt: "find . -name '*.go' | xargs gofmt -s -w"
- build: go install -v ./...
- tag: ./ci/git-tag.sh
- release: |
curl -sL https://git.io/goreleaser | bash
Expand Down
26 changes: 13 additions & 13 deletions sd-step.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"github.com/urfave/cli"
)

// VERSION gets set by the build script via the LDFLAGS
// VERSION gets set by the build script via the LDFLAGS.
var VERSION string

// habDepotURL is base url for public depot of habitat
// habDepotURL is base url for public depot of habitat.
const habDepotURL = "https://willem.habitat.sh/v1/depot"

var habPath = "/opt/sd/bin/hab"
Expand All @@ -33,7 +33,7 @@ func successExit() {
os.Exit(0)
}

// failureExit exits process with 1
// failureExit exits process with 1.
func failureExit(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
Expand All @@ -52,26 +52,26 @@ func finalRecover() {
successExit()
}

// translatePkgName translates the pkgName if pkgVersion is specified
// translatePkgName translates the pkgName if pkgVersion is specified.
func translatePkgName(pkgName string, pkgVersion string) (string, error) {
if pkgVersion == "" {
return pkgName, nil
} else if valid := versionValidator.MatchString(pkgVersion); valid == true {
}
if valid := versionValidator.MatchString(pkgVersion); valid {
return pkgName + "/" + pkgVersion, nil
} else {
return "", fmt.Errorf("%v is invalid version", pkgVersion)
}
return "", fmt.Errorf("%v is invalid version", pkgVersion)
}

// runCommand runs command
// runCommand runs command.
func runCommand(command string, output io.Writer) error {
cmd := execCommand("sh", "-c", command)
cmd.Stdout = output
cmd.Stderr = os.Stderr
return cmd.Run()
}

// Check if package is installed
// isPackageInstalled checks if the package is installed.
func isPackageInstalled(pkgName string, pkgVersion string) bool {
var output io.Writer

Expand All @@ -87,7 +87,7 @@ func isPackageInstalled(pkgName string, pkgVersion string) bool {
return checkCmdResult == nil
}

// execHab installs habitat package and executes habitat command
// execHab installs habitat package and executes habitat command.
func execHab(pkgName string, pkgVersion string, habChannel string, command []string, output io.Writer) error {
pkg, verErr := translatePkgName(pkgName, pkgVersion)
if verErr != nil {
Expand Down Expand Up @@ -132,7 +132,7 @@ func getPackageVersion(depot hab.Depot, pkgName, pkgVerExp string, habChannel st
"Trying to fetch versions from installed packages...\n", err)
dirs, err := ioutil.ReadDir("/hab/pkgs/" + pkgName)
if err != nil {
return "", errors.New("The specified version not found")
return "", errors.New("the specified version not found")
}
for _, dir := range dirs {
foundVersions = append(foundVersions, dir.Name())
Expand All @@ -157,7 +157,7 @@ func getPackageVersion(depot hab.Depot, pkgName, pkgVerExp string, habChannel st
}

if len(versions) == 0 {
return "", errors.New("The specified version not found")
return "", errors.New("the specified version not found")
}

sort.Sort(sort.Reverse(semver.Collection(versions)))
Expand Down Expand Up @@ -219,7 +219,7 @@ func main() {
pkgVersion, err = getPackageVersion(depot, pkgName, pkgVerExp, habChannel)

if err != nil {
failureExit(fmt.Errorf("Failed to get package version: %v", err))
failureExit(fmt.Errorf("failed to get package version: %v", err))
}
}

Expand Down
16 changes: 8 additions & 8 deletions sd-step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestRunCommand(t *testing.T) {
if err != nil {
t.Errorf("runCommand error = %q, should be nil", err)
}
if string(stdout.Bytes()) != expected {
t.Errorf("Expected '%v', actual '%v'", expected, string(stdout.Bytes()))
if s := stdout.String(); s != expected {
t.Errorf("Expected '%v', actual '%v'", expected, s)
}

stdout = new(bytes.Buffer)
Expand All @@ -41,8 +41,8 @@ func TestRunCommand(t *testing.T) {
if err != nil {
t.Errorf("runCommand error = %v, should be nil", err)
}
if string(stdout.Bytes()) != expected {
t.Errorf("Expected '%v', actual '%v'", expected, string(stdout.Bytes()))
if s := stdout.String(); s != expected {
t.Errorf("Expected '%v', actual '%v'", expected, s)
}
}

Expand All @@ -54,8 +54,8 @@ func TestExecHab(t *testing.T) {
if err != nil {
t.Errorf("execHab error = %q, should be nil", err)
}
if string(stdout.Bytes()) != habExecResult {
t.Errorf("Expected %q, got %q", habExecResult, string(stdout.Bytes()))
if s := stdout.String(); s != habExecResult {
t.Errorf("Expected %q, got %q", habExecResult, s)
}
}

Expand Down Expand Up @@ -146,7 +146,7 @@ func TestGetPackageVersions(t *testing.T) {
foundVersions: []string{"0.0.1", "0.1.0", "1.1.9", "1.2.1", "1.2.2", "1.3.0", "2.0.0"},
expectedVersion: "",
depotError: errors.New("depot error"),
expectedError: errors.New("The specified version not found"),
expectedError: errors.New("the specified version not found"),
},
{
versionExpression: "~1.2.0",
Expand All @@ -160,7 +160,7 @@ func TestGetPackageVersions(t *testing.T) {
foundVersions: []string{"0.0.1", "0.1.0", "1.1.9", "1.2.1", "1.2.2", "1.2.3-abc", "1.3.0", "2.0.0"},
expectedVersion: "",
depotError: nil,
expectedError: errors.New("The specified version not found"),
expectedError: errors.New("the specified version not found"),
},
}

Expand Down