Skip to content

Commit 20f6149

Browse files
dmitrisjithine
authored andcommitted
convert to go modules, fix linters issues (#19)
* convert to go modules, fix some linters issues * reorder and simplify SD steps * make build fail on gofmt viiolations
1 parent d646473 commit 20f6149

10 files changed

+79
-41
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
```bash
1111
$ go get github.com/screwdriver-cd/sd-step
12-
$ cd $GOPATH/src/github.com/screwdriver-cd/sd-step
13-
$ go build -a -o sd-step
1412
$ ./sd-step --help
1513
NAME:
1614
sd-step - wrapper command of habitat for Screwdriver

doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Sd-step is a wrapper command of habitat for Screwdriver.
2+
package main

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/screwdriver-cd/sd-step
2+
3+
go 1.13
4+
5+
require (
6+
github.com/Masterminds/semver v1.5.0
7+
github.com/urfave/cli v1.22.1
8+
)

go.sum

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
3+
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
4+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
5+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
8+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
10+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
11+
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
12+
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

hab/doc.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package hab provides utility functions for working with Habitat
2+
// https://www.habitat.sh.
3+
package hab

hab/hab.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
"time"
99
)
1010

11-
// PackagesInfo is response from depotj
11+
// PackagesInfo is response from depot.
1212
type PackagesInfo struct {
1313
RangeStart int `json:"range_start"`
1414
RangeEnd int `json:"range_end"`
1515
TotalCount int `json:"total_count"`
1616
PackageList []PackageInfo `json:"data"`
1717
}
1818

19-
// PackageInfo is package info in pkgs response
19+
// PackageInfo is package info in pkgs response.
2020
type PackageInfo struct {
2121
Origin string `json:"origin"`
2222
Name string `json:"name"`
@@ -25,7 +25,7 @@ type PackageInfo struct {
2525
Channels []string `json:"channels"`
2626
}
2727

28-
// Depot for hab
28+
// Depot for hab.
2929
type Depot interface {
3030
PackageVersionsFromName(pkgName string, habChannel string) ([]string, error)
3131
}
@@ -35,7 +35,7 @@ type depot struct {
3535
client *http.Client
3636
}
3737

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

5454
if res.StatusCode == 404 {
55-
return PackagesInfo{}, errors.New("Package not found")
56-
} else if res.StatusCode/100 != 2 {
57-
return PackagesInfo{}, fmt.Errorf("Unexpected status code: %d", res.StatusCode)
55+
return PackagesInfo{}, errors.New("package not found")
56+
}
57+
if res.StatusCode/100 != 2 {
58+
return PackagesInfo{}, fmt.Errorf("unexpected status code: %d", res.StatusCode)
5859
}
5960

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

70-
// PackageVersionsFromName fetch all versions from depot
71+
// PackageVersionsFromName fetches all versions from depot.
7172
func (depo *depot) PackageVersionsFromName(pkgName string, habChannel string) ([]string, error) {
7273
var packages []PackageInfo
7374

hab/hab_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ func makeFakeHTTPClient(t *testing.T, data testData) *http.Client {
6767
return &http.Client{Transport: transport}
6868
}
6969

70+
type fakeStruct struct{}
71+
7072
func jsonError(JSON string) error {
71-
return json.Unmarshal([]byte(JSON), nil)
73+
// "dummy" object to avoid `go vet` error when passing nil
74+
dummy := &fakeStruct{}
75+
return json.Unmarshal([]byte(JSON), dummy)
7276
}
7377

7478
func TestPackagesInfoFromName(t *testing.T) {
@@ -402,7 +406,7 @@ func TestPackagesInfoFromName(t *testing.T) {
402406
expected: nil,
403407
statusCode: 404,
404408
httpError: nil,
405-
expectedError: errors.New("Package not found"),
409+
expectedError: errors.New("package not found"),
406410
},
407411
{
408412
packageName: "foo/test",
@@ -418,7 +422,7 @@ func TestPackagesInfoFromName(t *testing.T) {
418422
expected: nil,
419423
statusCode: 500,
420424
httpError: nil,
421-
expectedError: errors.New("Unexpected status code: 500"),
425+
expectedError: errors.New("unexpected status code: 500"),
422426
},
423427
{
424428
packageName: "foo/test",
@@ -434,7 +438,7 @@ func TestPackagesInfoFromName(t *testing.T) {
434438
expected: nil,
435439
statusCode: 500,
436440
httpError: nil,
437-
expectedError: errors.New("Unexpected status code: 500"),
441+
expectedError: errors.New("unexpected status code: 500"),
438442
},
439443
{
440444
packageName: "foo/test",

screwdriver.yaml

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@ jobs:
77
main:
88
requires: [~commit, ~pr]
99
steps:
10-
- get: go get -t ./...
10+
- modverify: go version; go mod verify
11+
- gofmt: |
12+
gofmtOutput=$(gofmt -l .)
13+
if [ -z "${gofmtOutput}" ]; then
14+
echo "OK"
15+
else
16+
echo "gofmt violations in the following file(s):"
17+
echo "${gofmtOutput}\n"
18+
gofmt -d .
19+
exit 1
20+
fi
1121
- vet: go vet ./...
12-
- gofmt: "find . -name '*.go' | xargs gofmt -s -w"
13-
- test: go test ./...
14-
- build: go build -a -o sd-step
22+
- build: go install -v ./...
23+
- test: go test -v ./...
1524

1625
deploy:
1726
requires: main
1827
steps:
1928
- setup-ci: git clone https://github.com/screwdriver-cd/toolbox.git ci
20-
- get: go get -t ./...
21-
- gofmt: "find . -name '*.go' | xargs gofmt -s -w"
29+
- build: go install -v ./...
2230
- tag: ./ci/git-tag.sh
2331
- release: |
2432
curl -sL https://git.io/goreleaser | bash

sd-step.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818
"github.com/urfave/cli"
1919
)
2020

21-
// VERSION gets set by the build script via the LDFLAGS
21+
// VERSION gets set by the build script via the LDFLAGS.
2222
var VERSION string
2323

24-
// habDepotURL is base url for public depot of habitat
24+
// habDepotURL is base url for public depot of habitat.
2525
const habDepotURL = "https://willem.habitat.sh/v1/depot"
2626

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

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

55-
// translatePkgName translates the pkgName if pkgVersion is specified
55+
// translatePkgName translates the pkgName if pkgVersion is specified.
5656
func translatePkgName(pkgName string, pkgVersion string) (string, error) {
5757
if pkgVersion == "" {
5858
return pkgName, nil
59-
} else if valid := versionValidator.MatchString(pkgVersion); valid == true {
59+
}
60+
if valid := versionValidator.MatchString(pkgVersion); valid {
6061
return pkgName + "/" + pkgVersion, nil
61-
} else {
62-
return "", fmt.Errorf("%v is invalid version", pkgVersion)
6362
}
63+
return "", fmt.Errorf("%v is invalid version", pkgVersion)
6464
}
6565

66-
// runCommand runs command
66+
// runCommand runs command.
6767
func runCommand(command string, output io.Writer) error {
6868
cmd := execCommand("sh", "-c", command)
6969
cmd.Stdout = output
7070
cmd.Stderr = os.Stderr
7171
return cmd.Run()
7272
}
7373

74-
// Check if package is installed
74+
// isPackageInstalled checks if the package is installed.
7575
func isPackageInstalled(pkgName string, pkgVersion string) bool {
7676
var output io.Writer
7777

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

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

159159
if len(versions) == 0 {
160-
return "", errors.New("The specified version not found")
160+
return "", errors.New("the specified version not found")
161161
}
162162

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

221221
if err != nil {
222-
failureExit(fmt.Errorf("Failed to get package version: %v", err))
222+
failureExit(fmt.Errorf("failed to get package version: %v", err))
223223
}
224224
}
225225

sd-step_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestRunCommand(t *testing.T) {
3131
if err != nil {
3232
t.Errorf("runCommand error = %q, should be nil", err)
3333
}
34-
if string(stdout.Bytes()) != expected {
35-
t.Errorf("Expected '%v', actual '%v'", expected, string(stdout.Bytes()))
34+
if s := stdout.String(); s != expected {
35+
t.Errorf("Expected '%v', actual '%v'", expected, s)
3636
}
3737

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

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

@@ -146,7 +146,7 @@ func TestGetPackageVersions(t *testing.T) {
146146
foundVersions: []string{"0.0.1", "0.1.0", "1.1.9", "1.2.1", "1.2.2", "1.3.0", "2.0.0"},
147147
expectedVersion: "",
148148
depotError: errors.New("depot error"),
149-
expectedError: errors.New("The specified version not found"),
149+
expectedError: errors.New("the specified version not found"),
150150
},
151151
{
152152
versionExpression: "~1.2.0",
@@ -160,7 +160,7 @@ func TestGetPackageVersions(t *testing.T) {
160160
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"},
161161
expectedVersion: "",
162162
depotError: nil,
163-
expectedError: errors.New("The specified version not found"),
163+
expectedError: errors.New("the specified version not found"),
164164
},
165165
}
166166

0 commit comments

Comments
 (0)