Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
goddenrich committed Apr 9, 2024
1 parent 98b1e23 commit 2fb1024
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.55.2
version: v1.57.2

40 changes: 13 additions & 27 deletions cmd/puku/puku.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

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

Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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 {
Expand All @@ -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"`
Expand All @@ -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)
Expand All @@ -135,21 +121,21 @@ 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 {
if err := l.Update(paths); err != nil {
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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 6 additions & 2 deletions licences/licences.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2fb1024

Please sign in to comment.