Skip to content

Commit

Permalink
Tidy all Go code with golines
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Jun 14, 2024
1 parent 7e561c5 commit e84f5b6
Show file tree
Hide file tree
Showing 111 changed files with 4,375 additions and 1,782 deletions.
7 changes: 6 additions & 1 deletion bsondump/bsondump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,12 @@ func TestBsondumpMaxBSONSize(t *testing.T) {
t.Run("bsondump with file at max size + 1", func(t *testing.T) {
out, err := runBsondumpWithLargeFile(t, maxSize+1)
require.Error(t, err, "got error executing bsondump with large file")
require.Regexp(t, "is larger than maximum of 16793600 bytes", out, "bsondump prints error about file size")
require.Regexp(
t,
"is larger than maximum of 16793600 bytes",
out,
"bsondump prints error about file size",
)
})
}

Expand Down
16 changes: 14 additions & 2 deletions bsondump/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ func (*OutputOptions) Name() string {

// ParseOptions translates the command line arguments into an Options used to configure BSONDump.
func ParseOptions(rawArgs []string, versionStr, gitCommit string) (Options, error) {
toolOpts := options.New("bsondump", versionStr, gitCommit, Usage, false, options.EnabledOptions{})
toolOpts := options.New(
"bsondump",
versionStr,
gitCommit,
Usage,
false,
options.EnabledOptions{},
)
outputOpts := &OutputOptions{}
toolOpts.AddOptions(outputOpts)

Expand Down Expand Up @@ -82,6 +89,11 @@ func ParseOptions(rawArgs []string, versionStr, gitCommit string) (Options, erro
case "", DebugOutputType, JSONOutputType:
return Options{toolOpts, outputOpts}, nil
default:
return Options{}, fmt.Errorf("unsupported output type '%v'. Must be either '%v' or '%v'", DebugOutputType, JSONOutputType, outputOpts.Type)
return Options{}, fmt.Errorf(
"unsupported output type '%v'. Must be either '%v' or '%v'",
DebugOutputType,
JSONOutputType,
outputOpts.Type,
)
}
}
44 changes: 34 additions & 10 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,47 @@ var taskRegistry = task.NewRegistry(task.WithAutoNamespaces(true))

func init() {
// Build
taskRegistry.Declare("build").Description("build the tools").OptionalArgs("pkgs").Do(buildscript.BuildTools)
taskRegistry.Declare("checkMinVersion").Description("check if the minimum required Go version exists").Do(buildscript.CheckMinimumGoVersion)
taskRegistry.Declare("build").
Description("build the tools").
OptionalArgs("pkgs").
Do(buildscript.BuildTools)
taskRegistry.Declare("checkMinVersion").
Description("check if the minimum required Go version exists").
Do(buildscript.CheckMinimumGoVersion)

// SSDLC
taskRegistry.Declare("sbom:write").Description("create an SBOM Lite file using the Silkbomb tool").Do(buildscript.WriteSBOMLite)
taskRegistry.Declare("sbom:write").
Description("create an SBOM Lite file using the Silkbomb tool").
Do(buildscript.WriteSBOMLite)

// Static Analysis
taskRegistry.Declare("sa:installdevtools").Description("installs dev tools").Do(buildscript.SAInstallDevTools)
taskRegistry.Declare("sa:lint").Description("runs precious linting").DependsOn("sa:installdevtools").Do(buildscript.SAPreciousLint)
taskRegistry.Declare("sa:installdevtools").
Description("installs dev tools").
Do(buildscript.SAInstallDevTools)
taskRegistry.Declare("sa:lint").
Description("runs precious linting").
DependsOn("sa:installdevtools").
Do(buildscript.SAPreciousLint)
taskRegistry.Declare("sa:modtidy").Description("runs go mod tidy").Do(buildscript.SAModTidy)
taskRegistry.Declare("sa:evgvalidate").Description("runs evergreen validate").Do(buildscript.SAEvergreenValidate)
taskRegistry.Declare("sa:evgvalidate").
Description("runs evergreen validate").
Do(buildscript.SAEvergreenValidate)

// Testing
taskRegistry.Declare("test:unit").Description("runs all unit tests").OptionalArgs("pkgs").Do(buildscript.TestUnit)
taskRegistry.Declare("test:integration").Description("runs all integration tests").OptionalArgs("pkgs", "ssl", "auth", "kerberos", "topology").Do(buildscript.TestIntegration)
taskRegistry.Declare("test:kerberos").Description("runs all kerberos tests").Do(buildscript.TestKerberos)
taskRegistry.Declare("test:awsauth").Description("runs all aws auth tests").Do(buildscript.TestAWSAuth)
taskRegistry.Declare("test:unit").
Description("runs all unit tests").
OptionalArgs("pkgs").
Do(buildscript.TestUnit)
taskRegistry.Declare("test:integration").
Description("runs all integration tests").
OptionalArgs("pkgs", "ssl", "auth", "kerberos", "topology").
Do(buildscript.TestIntegration)
taskRegistry.Declare("test:kerberos").
Description("runs all kerberos tests").
Do(buildscript.TestKerberos)
taskRegistry.Declare("test:awsauth").
Description("runs all aws auth tests").
Do(buildscript.TestAWSAuth)
}

func main() {
Expand Down
11 changes: 9 additions & 2 deletions buildscript/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ func CheckMinimumGoVersion(ctx *task.Context) error {
r := regexp.MustCompile(versionPattern)
goVersionMatches := r.FindStringSubmatch(goVersionStr)
if len(goVersionMatches) < 2 {
return fmt.Errorf("Could not find version string in the output of `go version`. Output: %s", goVersionStr)
return fmt.Errorf(
"Could not find version string in the output of `go version`. Output: %s",
goVersionStr,
)
}

// goVersion must be prefixed with v to be parsed by golang.org/x/mod/semver
goVersion := fmt.Sprintf("v%s", goVersionMatches[1])

if semver.Compare(goVersion, minimumGoVersion) < 0 {
return fmt.Errorf("Could not find minimum desired Go version. Found %s, Wanted at least %s", goVersion, minimumGoVersion)
return fmt.Errorf(
"Could not find minimum desired Go version. Found %s, Wanted at least %s",
goVersion,
minimumGoVersion,
)
}

return nil
Expand Down
42 changes: 37 additions & 5 deletions buildscript/sa.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ const (
goimportsPkg = "golang.org/x/tools/cmd/goimports@" + goimportsVersion

golangCILintVersion = "1.59.1"
golinesVersion = "0.12.2"
gosecVersion = "2.20.0"
preciousVersion = "0.7.2"
ubiVersion = "0.0.18"
prettierVersion = "3.3.1"
)

func SAInstallDevTools(ctx *task.Context) error {
if err := installUBI(ctx); err != nil {
return err
}
if err := installGoimports(ctx); err != nil {
return err
}
if err := installGolangCILint(ctx); err != nil {
return err
}
if err := installGosec(ctx); err != nil {
if err := installGolines(ctx); err != nil {
return err
}
if err := installUBI(ctx); err != nil {
if err := installGosec(ctx); err != nil {
return err
}
if err := installPrecious(ctx); err != nil {
Expand Down Expand Up @@ -130,6 +134,21 @@ func installGolangCILint(ctx *task.Context) error {
)
}

// Install golines.
func installGolines(ctx *task.Context) error {
return installBinaryTool(
ctx,
"golines",
golinesVersion,
"segmentio/golines",
fmt.Sprintf(
"https://github.com/segmentio/golines/releases/download/v%s/golines_%s_linux_amd64.tar.gz",
golinesVersion,
golinesVersion,
),
)
}

// Install gosec.
func installGosec(ctx *task.Context) error {
return installBinaryTool(
Expand Down Expand Up @@ -177,7 +196,10 @@ func goInstall(ctx *task.Context, link string) error {
)
}

func installBinaryTool(ctx *task.Context, exeName, toolVersion, githubProject, downloadURLForCI string) error {
func installBinaryTool(
ctx *task.Context,
exeName, toolVersion, githubProject, downloadURLForCI string,
) error {
devBin, err := devBinDir()
if err != nil {
return err
Expand Down Expand Up @@ -315,15 +337,25 @@ func SAModTidy(ctx *task.Context) error {
// Restore originals, ignoring errors since they need tidying anyway.
_ = os.WriteFile("go.mod", origGoMod, 0600)
_ = os.WriteFile("go.sum", origGoSum, 0600)
return errors.New("go.mod and/or go.sum needs changes: run `go mod tidy` and commit the changes")
return errors.New(
"go.mod and/or go.sum needs changes: run `go mod tidy` and commit the changes",
)
}

return nil
}

// SAEvergreenValidate runs `evergreen validate` on common.yml and ensures the file is valid.
func SAEvergreenValidate(ctx *task.Context) error {
output, err := sh.RunOutput(ctx, "evergreen", "validate", "--file", "common.yml", "-p", "mongo-tools")
output, err := sh.RunOutput(
ctx,
"evergreen",
"validate",
"--file",
"common.yml",
"-p",
"mongo-tools",
)
if err != nil {
return fmt.Errorf("error from `evergreen validate`: %s: %w", output, err)
}
Expand Down
15 changes: 12 additions & 3 deletions common/archive/demultiplexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ type Demultiplexer struct {
IsAtlasProxy bool
}

func CreateDemux(namespaceMetadatas []*CollectionMetadata, in io.Reader, isAtlasProxy bool) *Demultiplexer {
func CreateDemux(
namespaceMetadatas []*CollectionMetadata,
in io.Reader,
isAtlasProxy bool,
) *Demultiplexer {
demux := &Demultiplexer{
NamespaceStatus: make(map[string]int),
In: in,
Expand Down Expand Up @@ -206,7 +210,9 @@ func (demux *Demultiplexer) End() error {
}
demux.outs[ns].End()
}
err = newError(fmt.Sprintf("archive finished but contained files were unfinished (%v)", openNss))
err = newError(
fmt.Sprintf("archive finished but contained files were unfinished (%v)", openNss),
)
} else {
for ns, status := range demux.NamespaceStatus {
if status != NamespaceClosed {
Expand Down Expand Up @@ -414,7 +420,10 @@ type SpecialCollectionCache struct {
hash hash.Hash64
}

func NewSpecialCollectionCache(intent *intents.Intent, demux *Demultiplexer) *SpecialCollectionCache {
func NewSpecialCollectionCache(
intent *intents.Intent,
demux *Demultiplexer,
) *SpecialCollectionCache {
return &SpecialCollectionCache{
Intent: intent,
Demux: demux,
Expand Down
9 changes: 8 additions & 1 deletion common/archive/multiplexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,14 @@ func (muxIn *MuxIn) Write(buf []byte) (int, error) {
panic(fmt.Errorf("corrupt bson in MuxIn.Write (size %v/%v)", size, len(buf)))
}
if buf[size-1] != 0 {
panic(fmt.Errorf("corrupt bson in MuxIn.Write bson has no-zero terminator %v, (size %v/%v)", buf[size-1], size, len(buf)))
panic(
fmt.Errorf(
"corrupt bson in MuxIn.Write bson has no-zero terminator %v, (size %v/%v)",
buf[size-1],
size,
len(buf),
),
)
}
if bufferWrites {
if len(muxIn.buf)+len(buf) > cap(muxIn.buf) {
Expand Down
18 changes: 16 additions & 2 deletions common/archive/multiplexer_roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ func TestParallelMux(t *testing.T) {
return
}

func makeIns(testIntents []*intents.Intent, mux *Multiplexer, inChecksum map[string]hash.Hash, muxIns map[string]*MuxIn, inLengths map[string]*int, errCh chan<- error) {
func makeIns(
testIntents []*intents.Intent,
mux *Multiplexer,
inChecksum map[string]hash.Hash,
muxIns map[string]*MuxIn,
inLengths map[string]*int,
errCh chan<- error,
) {
for index, dbc := range testIntents {
ns := dbc.Namespace()
sum := crc32.NewIEEE()
Expand Down Expand Up @@ -216,7 +223,14 @@ func makeIns(testIntents []*intents.Intent, mux *Multiplexer, inChecksum map[str
}
}

func makeOuts(testIntents []*intents.Intent, demux *Demultiplexer, outChecksum map[string]hash.Hash, demuxOuts map[string]*RegularCollectionReceiver, outLengths map[string]*int, errCh chan<- error) {
func makeOuts(
testIntents []*intents.Intent,
demux *Demultiplexer,
outChecksum map[string]hash.Hash,
demuxOuts map[string]*RegularCollectionReceiver,
outLengths map[string]*int,
errCh chan<- error,
) {
for _, dbc := range testIntents {
ns := dbc.Namespace()
sum := crc32.NewIEEE()
Expand Down
12 changes: 10 additions & 2 deletions common/archive/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func (parse *Parser) readBSONOrTerminator() (isTerminator bool, err error) {
return true, nil
}
if size < minBSONSize || size > db.MaxBSONSize {
return false, newParserError(fmt.Sprintf("%v is neither a valid bson length nor a archive terminator", size))
return false, newParserError(
fmt.Sprintf("%v is neither a valid bson length nor a archive terminator", size),
)
}
// TODO Because we're reusing this same buffer for all of our IO, we are basically guaranteeing that we'll
// copy the bytes twice. At some point we should fix this. It's slightly complex, because we'll need consumer
Expand All @@ -106,7 +108,13 @@ func (parse *Parser) readBSONOrTerminator() (isTerminator bool, err error) {
return false, newParserWrappedError("read bson", err)
}
if parse.buf[size-1] != 0x00 {
return false, newParserError(fmt.Sprintf("bson (size: %v, byte: %d) doesn't end with a null byte", size, parse.buf[size-1]))
return false, newParserError(
fmt.Sprintf(
"bson (size: %v, byte: %d) doesn't end with a null byte",
size,
parse.buf[size-1],
),
)
}
parse.length = int(size)
return false, nil
Expand Down
11 changes: 9 additions & 2 deletions common/archive/prelude.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ func (prelude *Prelude) Read(in io.Reader) error {
}

// NewPrelude generates a Prelude using the contents of an intent.Manager.
func NewPrelude(manager *intents.Manager, concurrentColls int, serverVersion, toolVersion string) (*Prelude, error) {
func NewPrelude(
manager *intents.Manager,
concurrentColls int,
serverVersion, toolVersion string,
) (*Prelude, error) {
prelude := Prelude{
Header: &Header{
FormatVersion: archiveFormatVersion,
Expand Down Expand Up @@ -134,7 +138,10 @@ func (prelude *Prelude) AddMetadata(cm *CollectionMetadata) {
if !ok {
prelude.DBS = append(prelude.DBS, cm.Database)
}
prelude.NamespaceMetadatasByDB[cm.Database] = append(prelude.NamespaceMetadatasByDB[cm.Database], cm)
prelude.NamespaceMetadatasByDB[cm.Database] = append(
prelude.NamespaceMetadatasByDB[cm.Database],
cm,
)
log.Logvf(log.Info, "archive prelude %v.%v", cm.Database, cm.Collection)
}

Expand Down
10 changes: 8 additions & 2 deletions common/auth/auth_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func GetAuthVersion(sessionProvider *db.SessionProvider) (int, error) {

version, err := util.ToInt(results["authSchemaVersion"])
if err != nil {
return 0, fmt.Errorf("getParameter command returned non-numeric result: %v, error: %v", results["authSchemaVersion"], err)
return 0, fmt.Errorf(
"getParameter command returned non-numeric result: %v, error: %v",
results["authSchemaVersion"],
err,
)
}
return version, nil
}
Expand All @@ -60,7 +64,9 @@ func VerifySystemAuthVersion(sessionProvider *db.SessionProvider) error {
}

authSchemaQuery := bson.M{"_id": "authSchema"}
count, err := session.Database("admin").Collection("system.version").CountDocuments(context.TODO(), authSchemaQuery)
count, err := session.Database("admin").
Collection("system.version").
CountDocuments(context.TODO(), authSchemaQuery)
if err != nil {
return fmt.Errorf("error checking pressence of auth version: %v", err)
} else if count == 0 {
Expand Down
Loading

0 comments on commit e84f5b6

Please sign in to comment.