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

[Issue 188] Add golangci-lint to subo #189

Merged
merged 12 commits into from
Feb 7, 2022
22 changes: 22 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
- main
pull_request:
permissions:
contents: read
pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.44.0
7 changes: 6 additions & 1 deletion .github/workflows/sanity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ jobs:

- name: Check out code into the Go module directory
uses: actions/checkout@v2


- name: Install golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.44.0

- name: Build Subo
run: |
make subo
Expand Down
23 changes: 23 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
run:
timeout: 5m
skip-dirs-use-default: true
modules-download-mode: readonly
allow-parallel-runners: true

output:
sort-results: true

linters:
disable-all: true
enable:
- deadcode
- gci
- godot
- unparam
- unused

linters-settings:
gci:
local-prefixes: github.com/suborbital
godot:
scope: all
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ include ./subo/release/release.mk

GO_INSTALL=go install -ldflags $(RELEASE_FLAGS)

subo:
subo: lint
$(GO_INSTALL)

subo/dev:
subo/dev: lint
$(GO_INSTALL) -tags=development

subo/docker:
subo/docker: lint
docker build . -t suborbital/subo:dev

subo/docker/publish:
subo/docker/publish: lint
docker buildx build . --platform linux/amd64,linux/arm64 -t suborbital/subo:dev --push

mod/replace/atmo:
go mod edit -replace github.com/suborbital/atmo=$(HOME)/Workspaces/suborbital/atmo

tidy:
go mod tidy && go mod download && go mod vendor
lint:
javorszky marked this conversation as resolved.
Show resolved Hide resolved
golangci-lint run ./...

.PHONY: subo subo/docker
.PHONY: subo subo/dev subo/docker subo/docker/publish mod/replace/atmo tidy lint
22 changes: 11 additions & 11 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/suborbital/subo/subo/util"
)

// Builder is capable of building Wasm modules from source
// Builder is capable of building Wasm modules from source.
type Builder struct {
Context *context.BuildContext

Expand All @@ -29,7 +29,7 @@ type Builder struct {
log util.FriendlyLogger
}

// BuildResult is the results of a build including the built module and logs
// BuildResult is the results of a build including the built module and logs.
type BuildResult struct {
Succeeded bool
OutputLog string
Expand All @@ -42,7 +42,7 @@ const (
ToolchainDocker = Toolchain("docker")
)

// ForDirectory creates a Builder bound to a particular directory
// ForDirectory creates a Builder bound to a particular directory.
func ForDirectory(logger util.FriendlyLogger, dir string) (*Builder, error) {
ctx, err := context.ForDirectory(dir)
if err != nil {
Expand All @@ -63,8 +63,8 @@ func (b *Builder) BuildWithToolchain(tcn Toolchain) error {

b.results = []BuildResult{}

// when building in Docker mode, just collect the langs we need to build, and then
// launch the associated builder images which will do the building
// When building in Docker mode, just collect the langs we need to build, and then
// launch the associated builder images which will do the building.
dockerLangs := map[string]bool{}

for _, r := range b.Context.Runnables {
Expand All @@ -89,8 +89,8 @@ func (b *Builder) BuildWithToolchain(tcn Toolchain) error {

err = b.doNativeBuildForRunnable(r, result)

// even if there was a failure, load the result into the builder
// since the logs of the failed build are useful
// Even if there was a failure, load the result into the builder
// since the logs of the failed build are useful.
b.results = append(b.results, *result)

if err != nil {
Expand Down Expand Up @@ -144,7 +144,7 @@ func (b *Builder) Bundle() error {
} else if b.Context.Directive.Headless {
b.log.LogInfo("updating Directive")

// bump the appVersion since we're in headless mode
// Bump the appVersion since we're in headless mode.
majorStr := strings.TrimPrefix(semver.Major(b.Context.Directive.AppVersion), "v")
major, _ := strconv.Atoi(majorStr)
new := fmt.Sprintf("v%d.0.0", major+1)
Expand Down Expand Up @@ -212,7 +212,7 @@ func (b *Builder) dockerBuildForLang(lang string) (*BuildResult, error) {
return result, nil
}

// results and resulting file are loaded into the BuildResult pointer
// results and resulting file are loaded into the BuildResult pointer.
func (b *Builder) doNativeBuildForRunnable(r context.RunnableDir, result *BuildResult) error {
cmds, err := context.NativeBuildCommands(r.Runnable.Lang)
if err != nil {
Expand All @@ -232,7 +232,7 @@ func (b *Builder) doNativeBuildForRunnable(r context.RunnableDir, result *BuildR

cmdString := strings.TrimSpace(fullCmd.String())

// Even if the command fails, still load the output into the result object
// Even if the command fails, still load the output into the result object.
outputLog, err := util.RunInDir(cmdString, r.Fullpath)

result.OutputLog += outputLog + "\n"
Expand Down Expand Up @@ -288,7 +288,7 @@ func (b *Builder) checkAndRunPreReqs(runnable context.RunnableDir, result *Build
}

// analyzeForCompilerFlags looks at the Runnable and determines if any additional compiler flags are needed
// this is initially added to support AS-JSON in AssemblyScript with its need for the --transform flag
// this is initially added to support AS-JSON in AssemblyScript with its need for the --transform flag.
func (b *Builder) analyzeForCompilerFlags(runnable context.RunnableDir) (string, error) {
if runnable.Runnable.Lang == "assemblyscript" {
packageJSONBytes, err := ioutil.ReadFile(filepath.Join(runnable.Fullpath, "package.json"))
Expand Down
23 changes: 12 additions & 11 deletions builder/context/buildcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"strings"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"

"github.com/suborbital/atmo/directive"
"github.com/suborbital/subo/subo/release"
"github.com/suborbital/subo/subo/util"
"gopkg.in/yaml.v2"
)

var dockerImageForLang = map[string]string{
Expand All @@ -23,7 +24,7 @@ var dockerImageForLang = map[string]string{
"js": "suborbital/builder-js",
}

// BuildContext describes the context under which the tool is being run
// BuildContext describes the context under which the tool is being run.
type BuildContext struct {
Cwd string
CwdIsRunnable bool
Expand All @@ -37,7 +38,7 @@ type BuildContext struct {
BuilderTag string
}

// RunnableDir represents a directory containing a Runnable
// RunnableDir represents a directory containing a Runnable.
type RunnableDir struct {
Name string
UnderscoreName string
Expand All @@ -46,13 +47,13 @@ type RunnableDir struct {
CompilerFlags string
}

// BundleRef contains information about a bundle in the current context
// BundleRef contains information about a bundle in the current context.
type BundleRef struct {
Exists bool
Fullpath string
}

// ForDirectory returns the build context for the provided working directory
// ForDirectory returns the build context for the provided working directory.
func ForDirectory(dir string) (*BuildContext, error) {
fullDir, err := filepath.Abs(dir)
if err != nil {
Expand Down Expand Up @@ -100,7 +101,7 @@ func ForDirectory(dir string) (*BuildContext, error) {
return bctx, nil
}

// RunnableExists returns true if the context contains a runnable with name <name>
// RunnableExists returns true if the context contains a runnable with name <name>.
func (b *BuildContext) RunnableExists(name string) bool {
for _, r := range b.Runnables {
if r.Name == name {
Expand All @@ -111,7 +112,7 @@ func (b *BuildContext) RunnableExists(name string) bool {
return false
}

// ShouldBuildLang returns true if the provided language is safe-listed for building
// ShouldBuildLang returns true if the provided language is safe-listed for building.
func (b *BuildContext) ShouldBuildLang(lang string) bool {
if len(b.Langs) == 0 {
return true
Expand Down Expand Up @@ -146,13 +147,13 @@ func (b *BuildContext) Modules() ([]os.File, error) {
func getRunnableDirs(cwd string) ([]RunnableDir, bool, error) {
runnables := []RunnableDir{}

// go through all of the dirs in the current dir
// Go through all of the dirs in the current dir.
topLvlFiles, err := ioutil.ReadDir(cwd)
if err != nil {
return nil, false, errors.Wrap(err, "failed to list directory")
}

// check to see if we're running from within a Runnable directory
// Check to see if we're running from within a Runnable directory
// and return true if so.
runnableDir, err := getRunnableFromFiles(cwd, topLvlFiles)
if err != nil {
Expand All @@ -169,7 +170,7 @@ func getRunnableDirs(cwd string) ([]RunnableDir, bool, error) {

dirPath := filepath.Join(cwd, tf.Name())

// determine if a .runnable file exists in that dir
// Determine if a .runnable file exists in that dir.
innerFiles, err := ioutil.ReadDir(dirPath)
if err != nil {
util.LogWarn(fmt.Sprintf("couldn't read files in %v", dirPath))
Expand All @@ -189,7 +190,7 @@ func getRunnableDirs(cwd string) ([]RunnableDir, bool, error) {
return runnables, false, nil
}

// containsRunnableYaml finds any .runnable file in a list of files
// ContainsRunnableYaml finds any .runnable file in a list of files.
func ContainsRunnableYaml(files []os.FileInfo) (string, bool) {
for _, f := range files {
if strings.HasPrefix(f.Name(), ".runnable.") {
Expand Down
13 changes: 7 additions & 6 deletions builder/context/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"path/filepath"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"

"github.com/suborbital/atmo/directive"
"github.com/suborbital/subo/subo/util"
"gopkg.in/yaml.v2"
)

// WriteDirectiveFile writes a Directive to disk
// WriteDirectiveFile writes a Directive to disk.
func WriteDirectiveFile(cwd string, directive *directive.Directive) error {
filePath := filepath.Join(cwd, "Directive.yaml")

Expand All @@ -28,7 +29,7 @@ func WriteDirectiveFile(cwd string, directive *directive.Directive) error {
return nil
}

// readDirectiveFile finds a Directive from disk but does not validate it
// readDirectiveFile finds a Directive from disk but does not validate it.
func readDirectiveFile(cwd string) (*directive.Directive, error) {
filePath := filepath.Join(cwd, "Directive.yaml")

Expand All @@ -54,7 +55,7 @@ func readDirectiveFile(cwd string) (*directive.Directive, error) {
return directive, nil
}

// readQueriesFile finds a queries.yaml from disk
// readQueriesFile finds a queries.yaml from disk.
func readQueriesFile(cwd string) ([]directive.DBQuery, error) {
filePath := filepath.Join(cwd, "Queries.yaml")

Expand All @@ -76,7 +77,7 @@ func readQueriesFile(cwd string) ([]directive.DBQuery, error) {
}

// AugmentAndValidateDirectiveFns ensures that all functions referenced in a handler exist
// in the project and then adds the function list to the provided directive
// in the project and then adds the function list to the provided directive.
func AugmentAndValidateDirectiveFns(dxe *directive.Directive, fns []RunnableDir) error {
fnMap := map[string]bool{}
for _, fn := range fns {
Expand All @@ -101,7 +102,7 @@ func AugmentAndValidateDirectiveFns(dxe *directive.Directive, fns []RunnableDir)
return nil
}

// getHandlerFnList gets a full list of all functions used in the directive's handlers
// getHandlerFnList gets a full list of all functions used in the directive's handlers.
func getHandlerFnList(dxe *directive.Directive) []string {
fnMap := map[string]bool{}

Expand Down
2 changes: 1 addition & 1 deletion builder/context/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime"
)

// NativeBuildCommands returns the native build commands needed to build a Runnable of a particular language
// NativeBuildCommands returns the native build commands needed to build a Runnable of a particular language.
func NativeBuildCommands(lang string) ([]string, error) {
os := runtime.GOOS

Expand Down
2 changes: 1 addition & 1 deletion builder/context/prereq.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Prereq struct {
Command string
}

// PreRequisiteCommands is a map of OS : language : preReq
// PreRequisiteCommands is a map of OS : language : preReq.
var PreRequisiteCommands = map[string]map[string][]Prereq{
"darwin": {
"rust": {},
Expand Down
3 changes: 2 additions & 1 deletion builder/context/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/suborbital/atmo/bundle"
)

// CollectStaticFiles collects all of the files in the `static/` directory relative to cwd
// and generates a map of their relative paths
// and generates a map of their relative paths.
func CollectStaticFiles(cwd string) (map[string]os.File, error) {
staticDir := filepath.Join(cwd, "static")

Expand Down
3 changes: 2 additions & 1 deletion builder/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/suborbital/subo/subo/util"
)

Expand All @@ -26,7 +27,7 @@ func TemplateFullPath(repo, branch string) (string, error) {
return filepath.Join(root, fmt.Sprintf("%s-%s", repoName, strings.ReplaceAll(branch, "/", "-")), "templates"), nil
}

// TemplateRootDir gets the template directory for subo and ensures it exists
// TemplateRootDir gets the template directory for subo and ensures it exists.
func TemplateRootDir() (string, error) {
config, err := os.UserConfigDir()
if err != nil {
Expand Down
Loading