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

feat: allow users to validate their doc strings via linter #2208

Merged
merged 9 commits into from
Feb 23, 2024
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
74 changes: 74 additions & 0 deletions cli/cli/commands/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
_ "embed"
"fmt"
"github.com/kurtosis-tech/kurtosis-package-indexer/server/crawler"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"io"
"os"
"os/exec"
"path"
Expand All @@ -25,6 +27,10 @@ const (
formatFlagShortKey = "f"
formatFlagDefaultValue = "false"

checkDocStringFlagKey = "check-docstring"
checkDocStringFlagShortKey = "c"
checkDocStringDefaultValue = "true"

pyBlackDockerImage = "pyfound/black:23.9.1"
dockerRunCmd = "run"
removeContainerOnExit = "--rm"
Expand All @@ -40,6 +46,8 @@ const (
presentWorkingDirectory = "."
versionArg = "version"

mainDotStarFilename = "main.star"
h4ck3rk3y marked this conversation as resolved.
Show resolved Hide resolved

linterFailedAsThingsNeedToBeReformattedExitCode = 1
linterFailedWithInternalErrorsExitCode = 123
)
Expand Down Expand Up @@ -74,7 +82,15 @@ var LintCmd = &lowlevel.LowlevelKurtosisCommand{
Type: flags.FlagType_Bool,
Default: formatFlagDefaultValue,
},
{
Key: checkDocStringFlagKey,
Usage: fmt.Sprintf("Use this flag to check whether the doc string is valid or not. This requires you to pass a single path to a '%v' or a directory containing a '%v'", mainDotStarFilename, mainDotStarFilename),
Shorthand: checkDocStringFlagShortKey,
Type: flags.FlagType_Bool,
Default: checkDocStringDefaultValue,
},
},

RunFunc: run,
}

Expand All @@ -92,6 +108,17 @@ func run(_ context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) err
dockerRunSuffix = append(dockerRunSuffix, checkFlagForBlack)
}

checkDocStringFlag, err := flags.GetBool(checkDocStringFlagKey)
if err != nil {
return stacktrace.Propagate(err, "an error occurred getting the value of the flag '%v'", checkDocStringFlagKey)
}

if checkDocStringFlag {
if err = validateDocString(fileOrDirToLintArg); err != nil {
return stacktrace.Propagate(err, "an error occurred while running the doc string validator")
}
}

logrus.Infof("This depends on '%v'; first run may take a while as we might have to download it", pyBlackDockerImage)

if _, err := exec.LookPath(dockerBinary); err != nil {
Expand Down Expand Up @@ -166,3 +193,50 @@ func getVolumeToMountAndPathToLint(pathOfFileOrDirToLint string) (string, string
return path.Dir(absolutePathForFileOrDirToLint), path.Base(absolutePathForFileOrDirToLint), nil
}
}

func validateDocString(fileOrDirToLintArg []string) error {
if len(fileOrDirToLintArg) != 1 {
return stacktrace.NewError("Doc string validation only works with one argument, either a full path to a '%v' file or a directory containing it got '%v' arguments", mainDotStarFilename, len(fileOrDirToLintArg))
}

fileOrDirToCheckForDocString := fileOrDirToLintArg[0]
lastPartOfFileToCheck := path.Base(fileOrDirToCheckForDocString)

if lastPartOfFileToCheck == mainDotStarFilename {
return parseDocString(fileOrDirToCheckForDocString)
}

fileInfo, _ := os.Stat(fileOrDirToCheckForDocString)
if !fileInfo.IsDir() {
return stacktrace.NewError("Passed argument '%v' isn't a '%v' file nor is it a directory", fileOrDirToLintArg[0], mainDotStarFilename)
}

entries, err := os.ReadDir(fileOrDirToCheckForDocString)
if err != nil {
return stacktrace.Propagate(err, "Couldn't read directory '%v' for doc string validation", fileOrDirToCheckForDocString)
}

for _, entry := range entries {
if entry.Name() == mainDotStarFilename {
return parseDocString(path.Join(fileOrDirToCheckForDocString, entry.Name()))
}
}

return stacktrace.Propagate(err, "Couldn't find a '%v' file in the passed directory '%v'", mainDotStarFilename, fileOrDirToCheckForDocString)
}

func parseDocString(mainStarFilepath string) error {
contents, err := os.ReadFile(mainStarFilepath)
if err != nil {
return stacktrace.Propagate(err, "")
}
contentsAsStr := string(contents)
currentOutput := logrus.StandardLogger().Out
defer logrus.SetOutput(currentOutput)
// we disable the output as the crawler pollutes the screen otherwise
h4ck3rk3y marked this conversation as resolved.
Show resolved Hide resolved
logrus.SetOutput(io.Discard)
if _, err := crawler.ParseMainDotStarContent(contentsAsStr); err != nil {
return stacktrace.Propagate(err, "an error occurred while parsing the doc string")
}
return nil
}
57 changes: 52 additions & 5 deletions cli/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.17.0 // indirect
google.golang.org/grpc v1.56.3
google.golang.org/grpc v1.57.1
google.golang.org/protobuf v1.31.0
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
Expand Down Expand Up @@ -70,18 +70,42 @@ require (
)

require (
connectrpc.com/connect v1.11.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/apache/arrow/go/v12 v12.0.1 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/aws/aws-sdk-go v1.44.334 // indirect
github.com/aws/aws-sdk-go-v2 v1.17.7 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.18 // indirect
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.59 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.31 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.23 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.11 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.26 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.0 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.31.0 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/bytedance/sonic v1.10.0-rc3 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cli/browser v1.3.0 // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/cli/shurcooL-graphql v0.0.4 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/containerd v1.7.2 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
Expand All @@ -93,8 +117,11 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/form3tech-oss/jwt-go v3.2.5+incompatible // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gammazero/deque v0.1.0 // indirect
github.com/gammazero/workerpool v1.1.2 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
Expand All @@ -107,42 +134,56 @@ require (
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-playground/validator/v10 v10.14.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.1.21+incompatible // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-github/v54 v54.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/asmfmt v1.3.2 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/kurtosis-tech/kurtosis-package-indexer/api/golang v0.0.0-20231220155208-4ae5a14a79d0 // indirect
github.com/kurtosis-tech/kurtosis-package-indexer/server v0.0.0-20240222174809-4f74727f5e3b // indirect
github.com/kurtosis-tech/kurtosis/connect-server v0.0.0-20230825003324-75d481e0db8c // indirect
github.com/kurtosis-tech/kurtosis/grpc-file-transfer/golang v0.0.0 // indirect
github.com/kurtosis-tech/kurtosis/utils v0.0.0-20240104153602-385833de9d76 // indirect
github.com/kurtosis-tech/starlark-lsp v0.0.0-20231103163737-8f660a80cb17 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/buildkit v0.12.4 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
Expand All @@ -151,7 +192,9 @@ require (
github.com/pascaldekloe/name v1.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
Expand All @@ -161,6 +204,7 @@ require (
github.com/shurcooL/githubv4 v0.0.0-20230704064427-599ae7bbf278 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
github.com/smacker/go-tree-sitter v0.0.0-20230226123037-c459dbde1464 // indirect
github.com/snowflakedb/gosnowflake v1.7.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
Expand All @@ -170,6 +214,7 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.lsp.dev/jsonrpc2 v0.9.0 // indirect
go.lsp.dev/pkg v0.0.0-20210323044036-f7deec69b52e // indirect
Expand All @@ -179,23 +224,25 @@ require (
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.starlark.net v0.0.0-20230224151120-c52844e64a10 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.20.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/segmentio/analytics-go.v3 v3.1.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
Loading
Loading