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

Fixed array versioning and support for pre-Go-modules tools; added more tests. #27

Merged
merged 3 commits into from
Jun 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ We use *breaking* word for marking changes that are not backward compatible (rel

### Fixed

* [#25](https://github.com/bwplotka/bingo/issues/25) Fixed support of `bingo get` for arrays.
* Fixed versioning binaries with `+incompatible` version (wrong templating used).
* Fixed support `bingo list` for arrays.

### Changed

* `bingo list` output format. (table `\t`-delimited now)

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

### Fixed

* Fixed extra whitespace in variables.env.

## [v0.2.0](https://github.com/bwplotka/bingo/releases/tag/v0.2.0) - 2020.06.04
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ You can read full a story behind `bingo` [in this blog post](https://deploy-prev
## Requirements

* Go 1.14+
* Linux or MacOS.
* All tools that you wish to "pin" have to be build in Go and use [Go Modules].
* Linux or MacOS (Want Windows support? [Helps us out](https://github.com/bwplotka/bingo/issues/26), should be trivial!)
* All tools that you wish to "pin" have to be build in Go (they don't need to use Go modules at all).

## Installing

Expand Down Expand Up @@ -194,15 +194,15 @@ Let's show a few examples on popular `goimports` tool (which formats Go code inc

1. Bonus: Makefile mode! If you use `Makefile` , `bingo` generates a very simple helper with nice variables. After running any `bingo get` command,
you will notice`.bingo/Variables.mk` file. Feel free to include this in your Makefile (`include .bingo/Variables.mk` on the top of your Makefile).

From now in your Makefile you can use, e.g. `$(GOIMPORTS)` variable which reliably ensures a correct version is used and installed.

1. Bonus number 2! Using immutable names might be hard to maintain for your other scripts so `bingo` also produces environment variables you can source to you shell. It's as easy as:

```shell
source .bingo/variables.env
```

From now on you can use, e.g. `$(GOIMPORTS)` variable which holds currently pinned binary name of the goimports tool.

## Production Usage
Expand Down
52 changes: 37 additions & 15 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
"time"

"github.com/bwplotka/bingo/pkg/bingo"
"github.com/bwplotka/bingo/pkg/gomodcmd"
"github.com/bwplotka/bingo/pkg/runner"
"github.com/pkg/errors"
)

type getConfig struct {
runner *gomodcmd.Runner
runner *runner.Runner
modDir string
relModDir string
update gomodcmd.GetUpdatePolicy
update runner.GetUpdatePolicy
name string

// target name or target package path, optionally with Version(s).
Expand All @@ -41,13 +41,22 @@ func get(
if c.name != "" {
return errors.New("name cannot by specified if no target was given")
}
modFiles, err := bingoModFiles(c.modDir)

pkgs, err := bingo.ListPinnedMainPackages(logger, c.relModDir, false)
if err != nil {
return err
}
for _, m := range modFiles {
for _, p := range pkgs {
mc := c
mc.rawTarget, _ = bingo.NameFromModFile(m)
mc.rawTarget = p.Name
if len(p.Versions) > 1 {
// Compose array target. Order of versions matter.
var versions []string
for _, v := range p.Versions {
versions = append(versions, v.Version)
}
mc.rawTarget += "@" + strings.Join(versions, ",")
}
if err := get(ctx, logger, mc); err != nil {
return err
}
Expand All @@ -63,17 +72,19 @@ func get(
}

if len(modVersions) > 1 {
dup := map[string]struct{}{}
for _, v := range modVersions {
if _, ok := dup[v]; ok {
return errors.Errorf("version duplicates are not allowed, got: %v", modVersions)
}
dup[v] = struct{}{}

if v == "none" {
return errors.Errorf("none is not allowed when there are more than one specified Version, got: %v", modVersions)
}
}
}

if len(modVersions) == 0 {
modVersions = append(modVersions, "")
}

pkgPath := nameOrPackage
name := nameOrPackage
if !strings.Contains(nameOrPackage, "/") {
Expand All @@ -90,13 +101,18 @@ func get(
} else {
// Binary referenced by path, get default name from package path.
name = path.Base(pkgPath)

if c.name == "" && name == "cmd" {
return errors.Errorf("package %s would be installed with ambiguous name %s. This is a common, but slightly annoying package layout. "+
"It's advised to choose unique name with -n flag", pkgPath, name)
}
}

if c.name != "" {
name = c.name
}

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

Expand All @@ -106,7 +122,13 @@ func get(
}
binModFiles = append([]string{filepath.Join(c.modDir, name+".mod")}, binModFiles...)

if modVersions[0] == "none" {
if len(modVersions) == 0 {
// No version was specified. This means user want to install what was pinned before. By specifying empty string,
// go get with figure out the version from existing module file under this index.
for range binModFiles {
modVersions = append(modVersions, "")
}
} else if modVersions[0] == "none" {
// none means we no longer want to Version this package.
// NOTE: We don't remove binaries.
return removeAllGlob(filepath.Join(c.modDir, name+".*"))
Expand Down Expand Up @@ -168,7 +190,7 @@ func getOne(
}

runnable := c.runner.With(ctx, tmpModFile, c.modDir)
if version != "" || emptyModFile || c.update != gomodcmd.NoUpdatePolicy {
if version != "" || emptyModFile || c.update != runner.NoUpdatePolicy {
// Steps 1 & 2: Resolve and download (if needed) thanks to 'go get' on our separate .mod file.
targetWithVer := pkgPath
if version != "" {
Expand Down Expand Up @@ -269,7 +291,7 @@ func ensureModDirExists(logger *log.Logger, relModDir string) error {
// 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(relModDir, fakeRootModFileName),
filepath.Join(relModDir, bingo.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 {
Expand All @@ -292,7 +314,7 @@ func ensureModDirExists(logger *log.Logger, relModDir string) error {
)
}

func createTmpModFileFromExisting(ctx context.Context, r *gomodcmd.Runner, modFile, tmpModFile string) (emptyModFile bool, _ error) {
func createTmpModFileFromExisting(ctx context.Context, r *runner.Runner, modFile, tmpModFile string) (emptyModFile bool, _ error) {
if err := os.RemoveAll(tmpModFile); err != nil {
return false, errors.Wrap(err, "rm")
}
Expand Down