Skip to content

Commit

Permalink
Fixed further error for no-go projects. (#22)
Browse files Browse the repository at this point in the history
Signed-off-by: Bartlomiej Plotka <[email protected]>
  • Loading branch information
bwplotka authored Jun 3, 2020
1 parent 11409f1 commit 5ce7ef3
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 59 deletions.
23 changes: 12 additions & 11 deletions .bingo/Variables.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Auto generated binary variables helper managed by https://github.com/bwplotka/bingo v0.1.0. DO NOT EDIT.
# Auto generated binary variables helper managed by https://github.com/bwplotka/bingo v0.1.1. DO NOT EDIT.
# All tools are designed to be build inside $GOBIN.
GOBIN ?= $(firstword $(subst :, ,${GOPATH}))/bin
GO ?= $(shell which go)
GOPATH ?= $(shell go env GOPATH)
GOBIN ?= $(firstword $(subst :, ,${GOPATH}))/bin
GO ?= $(shell which go)

# Bellow generated variables ensure that every time a tool under each variable is invoked, the correct version
# will be used; reinstalling only if needed.
Expand All @@ -19,41 +20,41 @@ COPYRIGHT ?= $(GOBIN)/copyright-v0.9.0
$(COPYRIGHT): .bingo/copyright.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/copyright-v0.9.0"
@$(GO) build -modfile=.bingo/copyright.mod -o=$(GOBIN)/copyright-v0.9.0 "github.com/bwplotka/flagarize/scripts/copyright"
@cd .bingo && $(GO) build -modfile=copyright.mod -o=$(GOBIN)/copyright-v0.9.0 "github.com/bwplotka/flagarize/scripts/copyright"
.bingo/copyright.mod: ;

EMBEDMD ?= $(GOBIN)/embedmd-v1.0.0
$(EMBEDMD): .bingo/embedmd.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/embedmd-v1.0.0"
@$(GO) build -modfile=.bingo/embedmd.mod -o=$(GOBIN)/embedmd-v1.0.0 "github.com/campoy/embedmd"
@cd .bingo && $(GO) build -modfile=embedmd.mod -o=$(GOBIN)/embedmd-v1.0.0 "github.com/campoy/embedmd"
.bingo/embedmd.mod: ;

FAILLINT ?= $(GOBIN)/faillint-v1.5.0
$(FAILLINT): .bingo/faillint.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/faillint-v1.5.0"
@$(GO) build -modfile=.bingo/faillint.mod -o=$(GOBIN)/faillint-v1.5.0 "github.com/fatih/faillint"
@cd .bingo && $(GO) build -modfile=faillint.mod -o=$(GOBIN)/faillint-v1.5.0 "github.com/fatih/faillint"
.bingo/faillint.mod: ;

GOIMPORTS ?= $(GOBIN)/goimports-v0.0.0-20200509030707-2212a7e161a5
GOIMPORTS ?= $(GOBIN)/goimports-v0.0.0-20200519204825-e64124511800
$(GOIMPORTS): .bingo/goimports.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/goimports-v0.0.0-20200509030707-2212a7e161a5"
@$(GO) build -modfile=.bingo/goimports.mod -o=$(GOBIN)/goimports-v0.0.0-20200509030707-2212a7e161a5 "golang.org/x/tools/cmd/goimports"
@echo "(re)installing $(GOBIN)/goimports-v0.0.0-20200519204825-e64124511800"
@cd .bingo && $(GO) build -modfile=goimports.mod -o=$(GOBIN)/goimports-v0.0.0-20200519204825-e64124511800 "golang.org/x/tools/cmd/goimports"
.bingo/goimports.mod: ;

GOLANGCI_LINT ?= $(GOBIN)/golangci-lint-v1.26.0
$(GOLANGCI_LINT): .bingo/golangci-lint.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/golangci-lint-v1.26.0"
@$(GO) build -modfile=.bingo/golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.26.0 "github.com/golangci/golangci-lint/cmd/golangci-lint"
@cd .bingo && $(GO) build -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.26.0 "github.com/golangci/golangci-lint/cmd/golangci-lint"
.bingo/golangci-lint.mod: ;

MISSPELL ?= $(GOBIN)/misspell-v0.3.4
$(MISSPELL): .bingo/misspell.mod
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
@echo "(re)installing $(GOBIN)/misspell-v0.3.4"
@$(GO) build -modfile=.bingo/misspell.mod -o=$(GOBIN)/misspell-v0.3.4 "github.com/client9/misspell/cmd/misspell"
@cd .bingo && $(GO) build -modfile=misspell.mod -o=$(GOBIN)/misspell-v0.3.4 "github.com/client9/misspell/cmd/misspell"
.bingo/misspell.mod: ;

1 change: 1 addition & 0 deletions .bingo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module _ // Fake go.mod auto-created by 'bingo' for go -moddir compatibility with non-Go projects. Commit this file, together with other .mod files.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ NOTE: As semantic versioning states all 0.y.z releases can contain breaking chan

We use *breaking* word for marking changes that are not backward compatible (relates only to v0.y.z releases.)

## [v0.1.1](https://github.com/bwplotka/bingo/releases/tag/v0.1.1) - 2020.06.03

Fixed

```
`Error: get command failed: 0: getting : go get -d: go: cannot find main module, but -modfile was set.
-modfile cannot be used to set the module root directory.
```

## [v0.1.0](https://github.com/bwplotka/bingo/releases/tag/v0.1.0) - 2020.05.30

Initial release.
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,15 @@ correct version of a tool.
Thanks to that you can refer to the binary using '$(TOOL)' variable which will install correct version if missing.
-go string
Path to the go command. (default "go")
-insecure
Use -insecure flag when using 'go get'
-m Generate makefile helper with all binaries as variables.
-makefile -m
Makefile to link the the generated helper for make when -m options is specified with.Specify empty to disable including the helper. (default "Makefile")
Makefile to link the the generated helper for make when -m options is specified with. Specify empty to disable including the helper. (default "Makefile")
-moddir string
Directory where separate modules for each binary will be maintained. Feel free to commit this directory to your VCS to bond binary versions to your project code. If the directorydoes not exist bingo logs and assumes a fresh project. (default ".bingo")
Directory where separate modules for each binary will be maintained. Feel free to commit this directory to your VCS to bond binary versions to your project code. If the directory does not exist bingo logs and assumes a fresh project. (default ".bingo")
-n string
The -n flag instructs to get binary and name it with given name instead of default, so the last element of package directory Allowed characters [A-z0-9._-]. If -n is used and no package/binary is specified, bingo get will return error. If -n is used with existing binary name, rename will be done.
-u The -u flag instructs get to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available.
Expand All @@ -140,14 +139,13 @@ Thanks to that you can refer to the binary using '$(TOOL)' variable which will i
List enumerates all or one binary that are/is currently pinned in this project. It will print exact path, version and immutable output.
-moddir string
Directory where separate modules for each binary ismaintained. If does not exists, bingo list will fail. (default ".bingo")
Directory where separate modules for each binary is maintained. If does not exists, bingo list will fail. (default ".bingo")
-v Print more'
version
Prints bingo version.
```
## Examples:
Expand Down Expand Up @@ -220,4 +218,4 @@ We follow [Thanos Go coding style](https://thanos.io/contributing/coding-style-g
## Initial Author
[@bwplotka](https://bwplotka.dev) inspired by [Paul's](https://github.com/myitcv) research and with a bit of help from [Duco](https://github.com/Helcaraxan) (:
[@bwplotka](https://bwplotka.dev) inspired by [Paul's](https://github.com/myitcv) research and with a bit of help from [Duco](https://github.com/Helcaraxan) (:
24 changes: 20 additions & 4 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func get(
if c.name != "" {
return errors.New("name cannot by specified if no target was given")
}
modules, err := filepath.Glob(filepath.Join(c.modDir, "*.mod"))
modFiles, err := bingoModFiles(c.modDir)
if err != nil {
return err
}
for _, m := range modules {
for _, m := range modFiles {
mc := c
mc.rawTarget, _ = bingo.NameFromModFile(m)
if err := get(ctx, logger, mc); err != nil {
Expand Down Expand Up @@ -77,7 +77,7 @@ func get(
name := nameOrPackage
if !strings.Contains(nameOrPackage, "/") {
// Binary referenced by name, get full package name if module file exists.
pkgPath, err = binNameToPackagePath(nameOrPackage, c.modDir)
pkgPath, err = packagePathFromBinaryName(nameOrPackage, c.modDir)
if err != nil {
return err
}
Expand All @@ -95,6 +95,10 @@ func get(
name = c.name
}

if name == strings.TrimSuffix(fakeRootModFileName, ".mod") {
return errors.New("requested binary with name `go`. This is impossible, choose different name using -name flag.")
}

binModFiles, err := filepath.Glob(filepath.Join(c.modDir, name+".*.mod"))
if err != nil {
return err
Expand Down Expand Up @@ -199,7 +203,7 @@ func getOne(
return c.runner.With(ctx, outModFile, c.modDir).Build(pkgPath, fmt.Sprintf("%s-%s", name, version))
}

func binNameToPackagePath(binary string, modDir string) (string, error) {
func packagePathFromBinaryName(binary string, modDir string) (string, error) {
currModFile := filepath.Join(modDir, binary+".mod")

// Get full import path from module file which has module and encoded sub path.
Expand Down Expand Up @@ -257,6 +261,18 @@ func ensureModDirExists(logger *log.Logger, modDir string) error {
}
}

// Hack against:
// "A file named go.mod must still be present in order to determine the module root directory, but it is not accessed."
// Ref: https://golang.org/doc/go1.14#go-flags
// TODO(bwplotka): Remove it: https://github.com/bwplotka/bingo/issues/20
if err := ioutil.WriteFile(
filepath.Join(modDir, fakeRootModFileName),
[]byte("module _ // Fake go.mod auto-created by 'bingo' for go -moddir compatibility with non-Go projects. Commit this file, together with other .mod files."),
os.ModePerm,
); err != nil {
return err
}

// README.
if err := ioutil.WriteFile(
filepath.Join(modDir, "README.md"),
Expand Down
21 changes: 17 additions & 4 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func TestGet_EmptyProject(t *testing.T) {
testutil.Assert(t, g.binaryExists("goimports-v0.0.0-20200522201501-cb1345f3a375"), "binary does not exists")
testutil.Assert(t, g.binaryExists("goimports2-v0.0.0-20200519175826-7521f6f42533"), "binary does not exists")
})
t.Run("Install package with go name should fail.", func(t *testing.T) {
defer p.assertNotChanged(t, defaultModDir)

testutil.NotOk(t, g.ExectErr(p.root, goBinPath, "get", "github.com/something/go"))
})
t.Run("Remove goimports2 by name", func(t *testing.T) {
defer p.assertNotChanged(t, defaultModDir)

Expand Down Expand Up @@ -213,11 +218,19 @@ func TestGet_ProjectWithBingo_NativeGoBuild(t *testing.T) {
// Get all binaries by doing native go build.
defer p.assertNotChanged(t, defaultModDir)

_, err := execCmd(p.root, nil, "go", "build", "-modfile="+filepath.Join(defaultModDir, "goimports.mod"), "-o="+filepath.Join(g.gobin, "goimports-v0.0.0-20200522201501-cb1345f3a375"), "golang.org/x/tools/cmd/goimports")
if isGoProject {
_, err := execCmd(p.root, nil, "go", "build", "-modfile="+filepath.Join(defaultModDir, "goimports.mod"), "-o="+filepath.Join(g.gobin, "goimports-v0.0.0-20200522201501-cb1345f3a375"), "golang.org/x/tools/cmd/goimports")
testutil.Ok(t, err)
_, err = execCmd(p.root, nil, "go", "build", "-modfile="+filepath.Join(defaultModDir, "faillint.mod"), "-o="+filepath.Join(g.gobin, "faillint-v1.3.0"), "github.com/fatih/faillint")
testutil.Ok(t, err)
_, err = execCmd(p.root, nil, "go", "build", "-modfile="+filepath.Join(defaultModDir, "goimports2.mod"), "-o="+filepath.Join(g.gobin, "goimports2-v0.0.0-20200519175826-7521f6f42533"), "golang.org/x/tools/cmd/goimports")
testutil.Ok(t, err)
}
_, err := execCmd(filepath.Join(p.root, defaultModDir), nil, "go", "build", "-modfile=goimports.mod", "-o="+filepath.Join(g.gobin, "goimports-v0.0.0-20200522201501-cb1345f3a375"), "golang.org/x/tools/cmd/goimports")
testutil.Ok(t, err)
_, err = execCmd(p.root, nil, "go", "build", "-modfile="+filepath.Join(defaultModDir, "faillint.mod"), "-o="+filepath.Join(g.gobin, "faillint-v1.3.0"), "github.com/fatih/faillint")
_, err = execCmd(filepath.Join(p.root, defaultModDir), nil, "go", "build", "-modfile=faillint.mod", "-o="+filepath.Join(g.gobin, "faillint-v1.3.0"), "github.com/fatih/faillint")
testutil.Ok(t, err)
_, err = execCmd(p.root, nil, "go", "build", "-modfile="+filepath.Join(defaultModDir, "goimports2.mod"), "-o="+filepath.Join(g.gobin, "goimports2-v0.0.0-20200519175826-7521f6f42533"), "golang.org/x/tools/cmd/goimports")
_, err = execCmd(filepath.Join(p.root, defaultModDir), nil, "go", "build", "-modfile=goimports2.mod", "-o="+filepath.Join(g.gobin, "goimports2-v0.0.0-20200519175826-7521f6f42533"), "golang.org/x/tools/cmd/goimports")
testutil.Ok(t, err)

testutil.Assert(t, g.binaryExists("faillint-v1.3.0"), "binary does not exists")
Expand Down Expand Up @@ -439,7 +452,7 @@ type goEnv struct {
}

func newTmpGoEnv(t testing.TB) *goEnv {
tmpDir, err := ioutil.TempDir(".", "bingo-tmpgoenv")
tmpDir, err := ioutil.TempDir(os.TempDir(), "bingo-tmpgoenv")
testutil.Ok(t, err)

tmpDir, err = filepath.Abs(tmpDir)
Expand Down
41 changes: 28 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import (
"github.com/pkg/errors"
)

const defaultMakefileName = "Makefile"
const (
defaultMakefileName = "Makefile"
// fake go.mod that we have to maintain, until https://github.com/bwplotka/bingo/issues/20 is fixed.
fakeRootModFileName = "go.mod"
)

func exitOnUsageError(usage func(), v ...interface{}) {
fmt.Println(append([]interface{}{"Error:"}, v...)...)
Expand All @@ -39,9 +43,9 @@ func main() {

// Get flags.
getFlags := flag.NewFlagSet("bingo get", flag.ContinueOnError)
getModDir := getFlags.String("moddir", ".bingo", "Directory where separate modules for each binary will be "+
"maintained. Feel free to commit this directory to your VCS to bond binary versions to your project code. If the directory"+
"does not exist bingo logs and assumes a fresh project.")
getModDir := getFlags.String("moddir", ".bingo", "Directory where separate modules for each binary will be"+
" maintained. Feel free to commit this directory to your VCS to bond binary versions to your project code. If the directory"+
" does not exist bingo logs and assumes a fresh project.")
getName := getFlags.String("n", "", "The -n flag instructs to get binary and name it with given name instead of default,"+
" so the last element of package directory Allowed characters [A-z0-9._-]. If -n is used and no package/binary is specified,"+
" bingo get will return error. If -n is used with existing binary name, rename will be done.")
Expand All @@ -50,15 +54,15 @@ func main() {
updatePatch := getFlags.Bool("upatch", false, "The -upatch flag (not -u patch) also instructs get to update dependencies, but changes the default to select patch releases.")
insecure := getFlags.Bool("insecure", false, "Use -insecure flag when using 'go get'")
makefile := getFlags.String("makefile", defaultMakefileName, "Makefile to link the the generated helper for make when `-m` options is specified with."+
"Specify empty to disable including the helper.")
" Specify empty to disable including the helper.")
genMakefileHelper := getFlags.Bool("m", false, "Generate makefile helper with all binaries as variables.")
// Go flags is so broken, need to add shadow -v flag to make those work in both before and after `get` command.
getVerbose := getFlags.Bool("v", false, "Print more'")

// List flags.
listFlags := flag.NewFlagSet("bingo list", flag.ContinueOnError)
listModDir := listFlags.String("moddir", ".bingo", "Directory where separate modules for each binary is"+
"maintained. If does not exists, bingo list will fail.")
" maintained. If does not exists, bingo list will fail.")
// Go flags is so broken, need to add shadow -v flag to make those work in both before and after `list` command.
listVerbose := listFlags.Bool("v", false, "Print more'")

Expand Down Expand Up @@ -139,7 +143,7 @@ func main() {
return err
}

modFiles, err := filepath.Glob(filepath.Join(modDir, "*.mod"))
modFiles, err := bingoModFiles(modDir)
if err != nil {
return err
}
Expand All @@ -152,7 +156,7 @@ func main() {
for _, f := range modFiles {
has, err := bingo.ModHasMeta(f, nil)
if err != nil {
return err
return errors.Wrapf(err, "modFile: %s", f)
}
if !has {
logger.Println("found malformed module file, removing:", f)
Expand Down Expand Up @@ -192,16 +196,15 @@ func main() {
if err != nil {
return errors.Wrap(err, "abs")
}
modFiles, err := filepath.Glob(filepath.Join(modDir, "*.mod"))
modFiles, err := bingoModFiles(modDir)
if err != nil {
return err
}

var targets []string
for _, f := range modFiles {
has, err := bingo.ModHasMeta(f, nil)
if err != nil {
return err
return errors.Wrapf(err, "modFile: %s", f)
}
if !has {
continue
Expand Down Expand Up @@ -263,6 +266,20 @@ func main() {
}
}

func bingoModFiles(modDir string) (ret []string, _ error) {
modFiles, err := filepath.Glob(filepath.Join(modDir, "*.mod"))
if err != nil {
return nil, err
}
for _, f := range modFiles {
if filepath.Base(f) == fakeRootModFileName {
continue
}
ret = append(ret, f)
}
return ret, nil
}

func list(modFiles []string) error {
for _, f := range modFiles {
pkg, ver, err := bingo.ModDirectPackage(f, nil)
Expand Down Expand Up @@ -314,7 +331,6 @@ correct version of a tool.
Thanks to that you can refer to the binary using '$(TOOL)' variable which will install correct version if missing.
%s
list <flags> [<package or binary>]
Expand All @@ -326,5 +342,4 @@ List enumerates all or one binary that are/is currently pinned in this project.
version
Prints bingo version.
`
Loading

0 comments on commit 5ce7ef3

Please sign in to comment.