Skip to content

Commit

Permalink
Merge pull request #36 from dgannon991/feat/3048/add-golangci-lint-co…
Browse files Browse the repository at this point in the history
…mmand

Add command to install golangci-lint and fixed local linting errors
  • Loading branch information
sgettys committed Apr 26, 2024
2 parents 85393f1 + 771e909 commit 169492a
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 31 deletions.
6 changes: 3 additions & 3 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func Vet() {
must.RunV("go", "vet", "./...")
}

// Run staticcheck on the project
// Run golangci-lint on the project
func Lint() {
mg.Deps(tools.EnsureStaticCheck)
must.RunV("staticcheck", "./...")
mg.Deps(tools.EnsureGolangCILint)
must.RunV("golangci-lint", "run", "./...")
}

func Test() {
Expand Down
8 changes: 4 additions & 4 deletions mixins/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (m Magefile) ConfigureAgent() {

// Build the mixin
func (m Magefile) Build() {
must.RunV("go", "mod", "tidy")
mgx.Must(must.RunV("go", "mod", "tidy"))
mgx.Must(releases.BuildAll(m.Pkg, m.MixinName, m.BinDir))
}

Expand All @@ -50,7 +50,7 @@ func (m Magefile) TestUnit() {
if mg.Verbose() {
v = "-v"
}
must.Command("go", "test", v, "./pkg/...").CollapseArgs().RunV()
mgx.Must(must.Command("go", "test", v, "./pkg/...").CollapseArgs().RunV())
}

// Test runs a full suite of tests
Expand All @@ -59,7 +59,7 @@ func (m Magefile) Test() {

// Check that we can call `mixin version`
m.Build()
must.RunV(filepath.Join(m.BinDir, m.MixinName+xplat.FileExt()), "version")
mgx.Must(must.RunV(filepath.Join(m.BinDir, m.MixinName+xplat.FileExt()), "version"))
}

// Publish the mixin and its mixin feed
Expand Down Expand Up @@ -101,7 +101,7 @@ func (m Magefile) Install() {
porterHome := porter.GetPorterHome()
fmt.Printf("Installing the %s mixin into %s\n", m.MixinName, porterHome)

os.MkdirAll(filepath.Join(porterHome, "mixins", m.MixinName, "runtimes"), 0770)
mgx.Must(os.MkdirAll(filepath.Join(porterHome, "mixins", m.MixinName, "runtimes"), 0770))
mgx.Must(shx.Copy(filepath.Join(m.BinDir, m.MixinName+xplat.FileExt()), filepath.Join(porterHome, "mixins", m.MixinName)))
mgx.Must(shx.Copy(filepath.Join(m.BinDir, "runtimes", m.MixinName+"-runtime"+xplat.FileExt()), filepath.Join(porterHome, "mixins", m.MixinName, "runtimes")))
}
Expand Down
6 changes: 5 additions & 1 deletion porter/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package porter

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -30,7 +31,10 @@ func UseBinForPorterHome() {
// use bin as PORTER_HOME
wd, _ := os.Getwd()
home := filepath.Join(wd, "bin")
os.Mkdir(home, 0770)
err := os.Mkdir(home, 0770)
if err != nil && !os.IsExist(err) {
panic(fmt.Sprintf("Unable to make directory %s for PORTER_HOME", home))
}
UsePorterHome(home)
}

Expand Down
2 changes: 1 addition & 1 deletion porter/porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func EnsurePorter() {
func EnsurePorterAt(version string) {
home := GetPorterHome()
runtimesDir := filepath.Join(home, "runtimes")
os.MkdirAll(runtimesDir, 0770)
mgx.Must(os.MkdirAll(runtimesDir, 0770))

var forceDownloadRuntime bool

Expand Down
4 changes: 2 additions & 2 deletions releases/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func getLDFLAGS(pkg string) string {
func build(pkgName, cmd, outPath, goos, goarch string) error {
ldflags := getLDFLAGS(pkgName)

os.MkdirAll(filepath.Dir(outPath), 0770)
mgx.Must(os.MkdirAll(filepath.Dir(outPath), 0770))
outPath += fileExt(goos)
srcPath := "./cmd/" + cmd

Expand Down Expand Up @@ -88,5 +88,5 @@ func XBuildAll(pkg string, name string, binDir string) {

// Copy most recent build into bin/dev so that subsequent build steps can easily find it, not used for publishing
os.RemoveAll(filepath.Join(binDir, "dev"))
shx.Copy(filepath.Join(binDir, info.Version), filepath.Join(binDir, "dev"), shx.CopyRecursive)
mgx.Must(shx.Copy(filepath.Join(binDir, info.Version), filepath.Join(binDir, "dev"), shx.CopyRecursive))
}
24 changes: 12 additions & 12 deletions releases/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ exec echo "$GITHUB_TOKEN"
pwd, _ := os.Getwd()
script := filepath.Join(pwd, askpass)

must.Command("git", "config", "core.askPass", script).In(dir).RunV()
mgx.Must(must.Command("git", "config", "core.askPass", script).In(dir).RunV())
}

func publishPackage(pkgType string, name string) {
Expand All @@ -99,8 +99,8 @@ func publishPackage(pkgType string, name string) {
// Create or update GitHub release for the permalink (canary/latest) with the version's binaries
if info.ShouldPublishPermalink() {
// Move the permalink tag. The existing release automatically points to the tag.
must.RunV("git", "tag", info.Permalink, info.Version+"^{}", "-f")
must.RunV("git", "push", "-f", remote, info.Permalink)
mgx.Must(must.RunV("git", "tag", info.Permalink, info.Version+"^{}", "-f"))
mgx.Must(must.RunV("git", "push", "-f", remote, info.Permalink))

AddFilesToRelease(repo, info.Permalink, versionDir)
} else {
Expand Down Expand Up @@ -139,14 +139,14 @@ func publishPackageFeed(pkgType string, name string) {
if remote == "" {
remote = "https://github.com/getporter/packages.git"
}
must.RunV("git", "clone", "--depth=1", remote, packagesRepo)
mgx.Must(must.RunV("git", "clone", "--depth=1", remote, packagesRepo))
configureGitBotIn(packagesRepo)

mgx.Must(generatePackageFeed(pkgType))

must.Command("git", "-c", "user.name='Porter Bot'", "-c", "user.email=bot@porter.sh", "commit", "--signoff", "-am", fmt.Sprintf("Add %s@%s to %s feed", name, info.Version, pkgType)).
In(packagesRepo).RunV()
must.Command("git", "push").In(packagesRepo).RunV()
mgx.Must(must.Command("git", "-c", "user.name='Porter Bot'", "-c", "user.email=bot@porter.sh", "commit", "--signoff", "-am", fmt.Sprintf("Add %s@%s to %s feed", name, info.Version, pkgType)).
In(packagesRepo).RunV())
mgx.Must(must.Command("git", "push").In(packagesRepo).RunV())
}

// Generate an updated mixin feed and publishes it.
Expand Down Expand Up @@ -194,18 +194,18 @@ func AddFilesToRelease(repo string, tag string, dir string) {

// Create the GH release and upload the assets at the same time
// The release stays in draft until all assets are uploaded
must.Command("gh", "release", "create", "-R", repo, tag, "--generate-notes", draft).
Args(files...).CollapseArgs().RunV()
mgx.Must(must.Command("gh", "release", "create", "-R", repo, tag, "--generate-notes", draft).
Args(files...).CollapseArgs().RunV())
} else {
// We must have failed when creating the release last time, and someone kicked the build to retry
// Get the release back into the desired state (see gh release create above for what we want to look like)

// Upload the release assets and overwrite existing assets
must.Command("gh", "release", "upload", "--clobber", "-R", repo, tag).
Args(files...).RunV()
mgx.Must(must.Command("gh", "release", "upload", "--clobber", "-R", repo, tag).
Args(files...).RunV())

// The release may still be stuck in draft from a previous failed upload while creating the release, make sure draft is cleared
must.Command("gh", "release", "edit", "--draft=false", "-R", repo, tag).RunV()
mgx.Must(must.Command("gh", "release", "edit", "--draft=false", "-R", repo, tag).RunV())
}
}

Expand Down
8 changes: 6 additions & 2 deletions releases/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func TestGenerateMixinFeed(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmp))
defer os.Chdir(origDir)
defer func() {
require.NoError(t, os.Chdir(origDir))
}()

err = GenerateMixinFeed()
require.NoError(t, err)
Expand Down Expand Up @@ -131,7 +133,9 @@ func TestGeneratePluginFeed_PorterNotInstalled(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmp))
defer os.Chdir(origDir)
defer func() {
require.NoError(t, os.Chdir(origDir))
}()

err = GeneratePluginFeed()
require.Errorf(t, err, "farts", "GeneratePluginFeed should fail when porter is not in the bin")
Expand Down
12 changes: 6 additions & 6 deletions tests/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,21 @@ func CreateTestCluster() {
}
defer os.Remove("kind.config.yaml")

must.Command("kind", "create", "cluster", "--name", getKindClusterName(), "--config", "kind.config.yaml").
Env("KIND_EXPERIMENTAL_DOCKER_NETWORK=" + docker.DefaultNetworkName).Run()
mgx.Must(must.Command("kind", "create", "cluster", "--name", getKindClusterName(), "--config", "kind.config.yaml").
Env("KIND_EXPERIMENTAL_DOCKER_NETWORK=" + docker.DefaultNetworkName).Run())

// Document the local registry
kubectl("apply", "-f", "-").
mgx.Must(kubectl("apply", "-f", "-").
Stdin(strings.NewReader(templateLocalRegistry)).
Run()
kubectl("config", "use-context", "kind-porter").Run()
Run())
mgx.Must(kubectl("config", "use-context", "kind-porter").Run())
}

// Delete the KIND cluster named porter.
func DeleteTestCluster() {
mg.Deps(tools.EnsureKind)

must.RunE("kind", "delete", "cluster", "--name", getKindClusterName())
mgx.Must(must.RunE("kind", "delete", "cluster", "--name", getKindClusterName()))
}

func kubectl(args ...string) shx.PreparedCommand {
Expand Down
31 changes: 31 additions & 0 deletions tools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var (

// DefaultStaticCheckVersion is the default version of StaticCheck that is installed when it's not present
DefaultStaticCheckVersion = "2023.1.6"

// DefaultGolangCILintVersion is the default version of golangci-lint that is installed when it's not present
DefaultGolangCILintVersion = "1.57.2"
)

// Fail if the go version doesn't match the specified constraint
Expand Down Expand Up @@ -131,3 +134,31 @@ func EnsureStaticCheckAt(version string) {
err := archive.DownloadToGopathBin(opts)
mgx.Must(err)
}

// Install golangci-lint
func EnsureGolangCILint() {
EnsureGolangCILintAt(DefaultGolangCILintVersion)
}

// Install golangci-lint at the specified version
func EnsureGolangCILintAt(version string) {
if ok, _ := pkg.IsCommandAvailable("golangci-lint", "--version", version); ok {
return
}

opts := archive.DownloadArchiveOptions{
DownloadOptions: downloads.DownloadOptions{
UrlTemplate: "https://github.com/golangci/golangci-lint/releases/download/v{{.VERSION}}/golangci-lint-{{.VERSION}}-{{.GOOS}}-{{.GOARCH}}.tar.gz",
Name: "golangci-lint",
Version: version,
},
ArchiveExtensions: map[string]string{
"linux": ".tar.gz",
"darwin": ".tar.gz",
"windows": ".tar.gz",
},
TargetFileTemplate: "golangci-lint-{{.VERSION}}-{{.GOOS}}-{{.GOARCH}}/golangci-lint{{.EXT}}",
}
err := archive.DownloadToGopathBin(opts)
mgx.Must(err)
}
23 changes: 23 additions & 0 deletions tools/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ func TestEnsureStaticCheck(t *testing.T) {
assert.True(t, found, "staticcheck was not available from its location in GOPATH/bin. PATH=%s", os.Getenv("PATH"))
}

func TestEnsureGolangCILint(t *testing.T) {
tmp, err := os.MkdirTemp("", "magefiles")
require.NoError(t, err, "Error creating temp directory")
defer os.RemoveAll(tmp)

oldGoPath := os.Getenv("GOPATH")
defer os.Setenv("GOPATH", oldGoPath)
os.Setenv("GOPATH", tmp)

oldPath := os.Getenv("PATH")
defer os.Setenv("PATH", oldPath)
os.Setenv("PATH", tmp)

tools.EnsureGolangCILint()
xplat.PrependPath(gopath.GetGopathBin())

require.FileExists(t, filepath.Join(tmp, "bin", "golangci-lint"+xplat.FileExt()))

found, err := pkg.IsCommandAvailable("golangci-lint", "--version", tools.DefaultGolangCILintVersion)
require.NoError(t, err, "IsCommandAvailable failed")
assert.True(t, found, "golangci-lint was not available from its location in GOPATH/bin. PATH=%s", os.Getenv("PATH"))
}

func TestEnsureGitHubClient(t *testing.T) {
tmp, err := os.MkdirTemp("", "magefiles")
require.NoError(t, err, "Error creating temp directory")
Expand Down

0 comments on commit 169492a

Please sign in to comment.