From 2fb10245642d15dad5bf1ec2ef8357af3310e143 Mon Sep 17 00:00:00 2001 From: rgodden Date: Tue, 9 Apr 2024 10:46:28 +0100 Subject: [PATCH] lint --- .github/workflows/golangci-lint.yml | 2 +- cmd/puku/puku.go | 40 ++++++++++------------------- generate/generate.go | 2 +- licences/licences.go | 8 ++++-- migrate/migrate.go | 3 +-- sync/sync.go | 3 ++- 6 files changed, 24 insertions(+), 34 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index e03394c..c69b774 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -18,5 +18,5 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.55.2 + version: v1.57.2 diff --git a/cmd/puku/puku.go b/cmd/puku/puku.go index 8e447a2..6e37544 100644 --- a/cmd/puku/puku.go +++ b/cmd/puku/puku.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "path/filepath" @@ -21,19 +20,6 @@ import ( "github.com/please-build/puku/work" ) -type outputFormat string - -// UnmarshalFlag implements the flags.Unmarshaler interface. -func (f *outputFormat) UnmarshalFlag(in string) error { - switch in { - case "json", "text": - *f = outputFormat(in) - default: - return fmt.Errorf("Flags error: Output format unrecognised") - } - return nil -} - var opts = struct { Usage string Verbosity clilogging.Verbosity `short:"v" long:"verbosity" description:"Verbosity of output (error, warning, notice, info, debug)" default:"info"` @@ -44,11 +30,11 @@ var opts = struct { } `positional-args:"true"` } `command:"fmt" description:"Format build files in the provided paths"` Sync struct { - Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` + Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` //nolint Write bool `short:"w" long:"write" description:"Whether to write the files back or just print them to stdout"` } `command:"sync" description:"Synchronises the go.mod to the third party build file"` Lint struct { - Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` + Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` //nolint Args struct { Paths []string `positional-arg-name:"packages" description:"The packages to process"` } `positional-args:"true"` @@ -60,7 +46,7 @@ var opts = struct { } `command:"watch" description:"Watch build files in the provided paths and update them when needed"` Migrate struct { Write bool `short:"w" long:"write" description:"Whether to write the files back or just print them to stdout"` - Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` + Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` //nolint ThirdPartyDirs []string `long:"third_party_dir" description:"Directories to find go_module rules to migrate"` UpdateGoMod bool `short:"g" long:"update_go_mod" description:"Update the go mod with the module(s) being migrated"` Args struct { @@ -69,7 +55,7 @@ var opts = struct { } `command:"migrate" description:"Migrates from go_module to go_repo"` Licenses struct { Update struct { - Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` + Format string `short:"f" long:"format" choice:"json" choice:"text" default:"text" description:"output format when outputting to stdout"` //nolint Write bool `short:"w" long:"write" description:"Whether to write the files back or just print them to stdout"` Args struct { Paths []string `positional-arg-name:"packages" description:"The packages to process"` @@ -85,35 +71,35 @@ puku is a tool used to generate and update Go targets in build files var log = logging.GetLogger() var funcs = map[string]func(conf *config.Config, plzConf *please.Config, orignalWD string) int{ - "fmt": func(conf *config.Config, plzConf *please.Config, orignalWD string) int { + "fmt": func(_ *config.Config, plzConf *please.Config, orignalWD string) int { paths := work.MustExpandPaths(orignalWD, opts.Fmt.Args.Paths) if err := generate.Update(plzConf, paths...); err != nil { log.Fatalf("%v", err) } return 0 }, - "sync": func(conf *config.Config, plzConf *please.Config, orignalWD string) int { + "sync": func(_ *config.Config, plzConf *please.Config, _ string) int { g := graph.New(plzConf.BuildFileNames()) if opts.Sync.Write { if err := sync.Sync(plzConf, g); err != nil { log.Fatalf("%v", err) } } else { - if err := sync.SyncToStdout(string(opts.Sync.Format), plzConf, g); err != nil { + if err := sync.SyncToStdout(opts.Sync.Format, plzConf, g); err != nil { log.Fatalf("%v", err) } } return 0 }, - "lint": func(conf *config.Config, plzConf *please.Config, orignalWD string) int { + "lint": func(_ *config.Config, plzConf *please.Config, orignalWD string) int { paths := work.MustExpandPaths(orignalWD, opts.Lint.Args.Paths) - if err := generate.UpdateToStdout(string(opts.Lint.Format), plzConf, paths...); err != nil { + if err := generate.UpdateToStdout(opts.Lint.Format, plzConf, paths...); err != nil { log.Fatalf("%v", err) } return 0 }, - "watch": func(conf *config.Config, plzConf *please.Config, orignalWD string) int { + "watch": func(_ *config.Config, plzConf *please.Config, orignalWD string) int { paths := work.MustExpandPaths(orignalWD, opts.Watch.Args.Paths) if err := generate.Update(plzConf, paths...); err != nil { log.Fatalf("%v", err) @@ -135,13 +121,13 @@ var funcs = map[string]func(conf *config.Config, plzConf *please.Config, orignal log.Fatalf("%v", err) } } else { - if err := migrate.MigrateToStdout(string(opts.Migrate.Format), conf, plzConf, opts.Migrate.UpdateGoMod, opts.Migrate.Args.Modules, paths); err != nil { + if err := migrate.MigrateToStdout(opts.Migrate.Format, conf, plzConf, opts.Migrate.UpdateGoMod, opts.Migrate.Args.Modules, paths); err != nil { log.Fatalf("%v", err) } } return 0 }, - "update": func(conf *config.Config, plzConf *please.Config, orignalWD string) int { + "update": func(_ *config.Config, plzConf *please.Config, orignalWD string) int { paths := work.MustExpandPaths(orignalWD, opts.Licenses.Update.Args.Paths) l := licences.New(proxy.New(proxy.DefaultURL), graph.New(plzConf.BuildFileNames())) if opts.Licenses.Update.Write { @@ -149,7 +135,7 @@ var funcs = map[string]func(conf *config.Config, plzConf *please.Config, orignal log.Fatalf("%v", err) } } else { - if err := l.UpdateToStdout(string(opts.Licenses.Update.Format), paths); err != nil { + if err := l.UpdateToStdout(opts.Licenses.Update.Format, paths); err != nil { log.Fatalf("%v", err) } } diff --git a/generate/generate.go b/generate/generate.go index 9eee536..ebcf322 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -87,7 +87,7 @@ func UpdateToStdout(format string, plzConf *please.Config, paths ...string) erro } func (u *updater) readAllModules(conf *config.Config) error { - return filepath.WalkDir(conf.GetThirdPartyDir(), func(path string, info fs.DirEntry, err error) error { + return filepath.WalkDir(conf.GetThirdPartyDir(), func(path string, info fs.DirEntry, _ error) error { for _, buildFileName := range u.plzConf.BuildFileNames() { if info.Name() == buildFileName { file, err := u.graph.LoadFile(filepath.Dir(path)) diff --git a/licences/licences.go b/licences/licences.go index 5105414..04eb6a2 100644 --- a/licences/licences.go +++ b/licences/licences.go @@ -68,12 +68,16 @@ func getLicences(modPaths []string) (map[string][]string, error) { } func (l *Licenses) Update(paths []string) error { - l.update(paths) + if err := l.update(paths); err != nil { + return err + } return l.graph.FormatFiles() } func (l *Licenses) UpdateToStdout(format string, paths []string) error { - l.update(paths) + if err := l.update(paths); err != nil { + return err + } return l.graph.FormatFilesWithWriter(os.Stdout, format) } diff --git a/migrate/migrate.go b/migrate/migrate.go index cc1d4f5..2f85a4a 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -48,10 +48,9 @@ func Migrate(conf *config.Config, plzConf *please.Config, updateGoMod bool, modu return err } return m.graph.FormatFiles() - } -func MigrateToStdout(format string, conf *config.Config, plzConf *please.Config, updateGoMod bool, modules, paths []string) error { +func MigrateToStdout(format string, conf *config.Config, plzConf *please.Config, updateGoMod bool, modules, paths []string) error { //nolint m := newMigrator(plzConf, conf) if err := m.migrate(modules, paths, updateGoMod); err != nil { return err diff --git a/sync/sync.go b/sync/sync.go index db712bc..c3d4126 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -43,7 +43,8 @@ func Sync(plzConf *please.Config, g *graph.Graph) error { return s.graph.FormatFiles() } -func SyncToStdout(format string, plzConf *please.Config, g *graph.Graph) error { +// SyncToStdout constructs the syncer and outputs the synced build file to stdout. +func SyncToStdout(format string, plzConf *please.Config, g *graph.Graph) error { //nolint s := newSyncer(plzConf, g) if err := s.sync(); err != nil { return err