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 pinned build envvar/flag support + list. #83

Merged
merged 1 commit into from
May 13, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ 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.)

## (Under development)
## Unreleased

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

### Added

* bingo list now lists pinned build flags and environment variables.

### Fixed

* Fixed preserving build flags and env files via bingo .mod files.
* Fixed formatting of bingo list

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

### Added

* Added support for build flags and envs via go.mod file.
* Added support for build flags and environment variables via go.mod file.

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ go 1.16
require github.com/gohugoio/hugo v0.83.1 // CGO_ENABLED=1 -tags=extended
```

Run `bingo list` to see if build options are parsed correctly.
Run `bingo get` to install all binaries including the modified one with new build flags.

## Production Usage

To see production example see:
Expand Down
7 changes: 5 additions & 2 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ func get(ctx context.Context, logger *log.Logger, c getConfig, rawTarget string)
target.Module.Version = mf.DirectPackage().Module.Version
}
target.RelPath = mf.DirectPackage().RelPath
target.BuildFlags = mf.DirectPackage().BuildFlags
target.BuildEnvs = mf.DirectPackage().BuildEnvs

// Save for future versions without potentially existing files.
pkgPath = target.Path()
Expand Down Expand Up @@ -583,6 +581,11 @@ func getPackage(ctx context.Context, logger *log.Logger, c installPackageConfig,
}
}

// Currently user can't specify build flags and envvars from CLI, take if from optionally, manually updated mod file.
if old := tmpModFile.DirectPackage(); old != nil {
target.BuildEnvs = old.BuildEnvs
target.BuildFlags = old.BuildFlags
}
if err := tmpModFile.SetDirectRequire(target); err != nil {
return err
}
Expand Down
96 changes: 71 additions & 25 deletions get_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"testing"

"github.com/bwplotka/bingo/pkg/bingo"
"github.com/bwplotka/bingo/pkg/runner"
"github.com/bwplotka/bingo/pkg/version"
"github.com/efficientgo/tools/core/pkg/testutil"
Expand All @@ -22,18 +23,33 @@ const (
defaultGoProxy = "https://proxy.golang.org"
)

var bingoExpectedCompatibilityOutput = []row{
{name: "buildable", binName: "buildable-v0.0.0-20210109094001-375d0606849d", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable@v0.0.0-20210109094001-375d0606849d"},
{name: "buildable2", binName: "buildable2-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable2@v0.0.0-20210109093942-2e6391144e85"},
{name: "buildable_old", binName: "buildable_old-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable@v0.0.0-20210109093942-2e6391144e85"},
{name: "f2", binName: "f2-v1.0.0", pkgVersion: "github.com/fatih/faillint@v1.0.0"},
{name: "f2", binName: "f2-v1.1.0", pkgVersion: "github.com/fatih/faillint@v1.1.0"},
{name: "f2", binName: "f2-v1.2.0", pkgVersion: "github.com/fatih/faillint@v1.2.0"},
{name: "f2", binName: "f2-v1.5.0", pkgVersion: "github.com/fatih/faillint@v1.5.0"},
{name: "faillint", binName: "faillint-v1.3.0", pkgVersion: "github.com/fatih/faillint@v1.3.0"},
{name: "go-bindata", binName: "go-bindata-v3.1.1+incompatible", pkgVersion: "github.com/go-bindata/go-bindata/go-bindata@v3.1.1+incompatible"},
{name: "wr_buildable", binName: "wr_buildable-v0.0.0-20210109165512-ccbd4039b94a", pkgVersion: "github.com/bwplotka/bingo/testdata/module_with_replace/buildable@v0.0.0-20210109165512-ccbd4039b94a"},
}
var (
bingoExpectedCompatibilityOutput = []row{
{name: "buildable", binName: "buildable-v0.0.0-20210109094001-375d0606849d", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable@v0.0.0-20210109094001-375d0606849d"},
{name: "buildable2", binName: "buildable2-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable2@v0.0.0-20210109093942-2e6391144e85"},
{name: "buildable_old", binName: "buildable_old-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable@v0.0.0-20210109093942-2e6391144e85"},
{name: "f2", binName: "f2-v1.0.0", pkgVersion: "github.com/fatih/faillint@v1.0.0"},
{name: "f2", binName: "f2-v1.1.0", pkgVersion: "github.com/fatih/faillint@v1.1.0"},
{name: "f2", binName: "f2-v1.2.0", pkgVersion: "github.com/fatih/faillint@v1.2.0"},
{name: "f2", binName: "f2-v1.5.0", pkgVersion: "github.com/fatih/faillint@v1.5.0"},
{name: "faillint", binName: "faillint-v1.3.0", pkgVersion: "github.com/fatih/faillint@v1.3.0"},
{name: "go-bindata", binName: "go-bindata-v3.1.1+incompatible", pkgVersion: "github.com/go-bindata/go-bindata/go-bindata@v3.1.1+incompatible"},
{name: "wr_buildable", binName: "wr_buildable-v0.0.0-20210109165512-ccbd4039b94a", pkgVersion: "github.com/bwplotka/bingo/testdata/module_with_replace/buildable@v0.0.0-20210109165512-ccbd4039b94a"},
}

bingoExpectedCompatibilityOutput_0_4_1 = []row{
{name: "buildable", binName: "buildable-v0.0.0-20210109094001-375d0606849d", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable@v0.0.0-20210109094001-375d0606849d"},
{name: "buildable2", binName: "buildable2-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable2@v0.0.0-20210109093942-2e6391144e85"},
{name: "buildable_old", binName: "buildable_old-v0.0.0-20210109093942-2e6391144e85", pkgVersion: "github.com/bwplotka/bingo/testdata/module/buildable@v0.0.0-20210109093942-2e6391144e85"},
{name: "f2", binName: "f2-v1.0.0", pkgVersion: "github.com/fatih/faillint@v1.0.0"},
{name: "f2", binName: "f2-v1.1.0", pkgVersion: "github.com/fatih/faillint@v1.1.0"},
{name: "f2", binName: "f2-v1.2.0", pkgVersion: "github.com/fatih/faillint@v1.2.0"},
{name: "f2", binName: "f2-v1.5.0", pkgVersion: "github.com/fatih/faillint@v1.5.0"},
{name: "faillint", binName: "faillint-v1.3.0", pkgVersion: "github.com/fatih/faillint@v1.3.0", buildEnvVars: "CGO_ENABLED=1", buildFlags: "-tags=extended"},
{name: "go-bindata", binName: "go-bindata-v3.1.1+incompatible", pkgVersion: "github.com/go-bindata/go-bindata/go-bindata@v3.1.1+incompatible"},
{name: "wr_buildable", binName: "wr_buildable-v0.0.0-20210109165512-ccbd4039b94a", pkgVersion: "github.com/bwplotka/bingo/testdata/module_with_replace/buildable@v0.0.0-20210109165512-ccbd4039b94a"},
}
)

// TODO(bwplotka): Test running versions. To do so we might want to setup small binary printing Version at each commit.
// $GOBIN has to be set for this test to run properly.
Expand Down Expand Up @@ -764,6 +780,15 @@ func TestGet(t *testing.T) {

for _, dir := range dirs {
t.Run(dir, func(t *testing.T) {
compatibilityOutput := bingoExpectedCompatibilityOutput

// Special test case. failint.mod for v0.4.1 version contains line syntax, allowing go build to add those build flags (which does nothing to failint).
// Yet, check if they will get preserved.
// TODO(bwplotka): Design test module that tests better those edge cases instead of installing different 3rdparty real modules.
if dir == "testdata/testproject_with_bingo_v0_4_1" {
compatibilityOutput = bingoExpectedCompatibilityOutput_0_4_1
}

for _, isGoProject := range []bool{false, true} {
t.Run(fmt.Sprintf("isGoProject=%v", isGoProject), func(t *testing.T) {
t.Run("Via bingo get all", func(t *testing.T) {
Expand All @@ -778,7 +803,7 @@ func TestGet(t *testing.T) {
p.assertNotChanged(t, defaultModDir)

testutil.Equals(t, []string{}, g.existingBinaries(t))
expectBingoListRows(t, bingoExpectedCompatibilityOutput, g.ExecOutput(t, p.root, goBinPath, "list"))
expectBingoListRows(t, compatibilityOutput, g.ExecOutput(t, p.root, goBinPath, "list"))

defer p.assertNotChanged(t, defaultModDir)

Expand All @@ -792,7 +817,7 @@ func TestGet(t *testing.T) {
"faillint-v1.3.0", "go-bindata-v3.1.1+incompatible",
"wr_buildable-v0.0.0-20210109165512-ccbd4039b94a",
}, g.existingBinaries(t))
expectBingoListRows(t, bingoExpectedCompatibilityOutput, g.ExecOutput(t, p.root, goBinPath, "list"))
expectBingoListRows(t, compatibilityOutput, g.ExecOutput(t, p.root, goBinPath, "list"))

// Expect binaries works:
testutil.Equals(t, "module.buildable 2.1\n", g.ExecOutput(t, p.root, filepath.Join(g.gobin, "buildable-v0.0.0-20210109094001-375d0606849d")))
Expand Down Expand Up @@ -830,7 +855,7 @@ func TestGet(t *testing.T) {
fmt.Println(g.ExecOutput(t, p.root, goBinPath, "get", "f2"))
testutil.Equals(t, []string{"buildable-v0.0.0-20210109094001-375d0606849d", "f2-v1.0.0", "f2-v1.1.0", "f2-v1.2.0", "f2-v1.5.0", "faillint-v1.3.0", "wr_buildable-v0.0.0-20210109165512-ccbd4039b94a"}, g.existingBinaries(t))

expectBingoListRows(t, bingoExpectedCompatibilityOutput, g.ExecOutput(t, p.root, goBinPath, "list"))
expectBingoListRows(t, compatibilityOutput, g.ExecOutput(t, p.root, goBinPath, "list"))
})
t.Run("Via go", func(t *testing.T) {
g.Clear(t)
Expand Down Expand Up @@ -935,7 +960,7 @@ func TestGet(t *testing.T) {
fmt.Println(g.ExecOutput(t, p.root, goBinPath, "get", "wr_buildable@none"))
fmt.Println(g.ExecOutput(t, p.root, goBinPath, "get", "go-bindata@none"))

testutil.Equals(t, "Name\tBinary Name\tPackage @ Version\n----\t-----------\t-----------------", g.ExecOutput(t, p.root, goBinPath, "list"))
expectBingoListRows(t, nil, g.ExecOutput(t, p.root, goBinPath, "list"))

_, err := os.Stat(filepath.Join(p.root, ".bingo", "Variables.mk"))
testutil.NotOk(t, err)
Expand All @@ -949,14 +974,16 @@ func TestGet(t *testing.T) {
}

type row struct {
name, binName, pkgVersion string
name, binName, pkgVersion, buildEnvVars, buildFlags string
}

func expectBingoListRows(t testing.TB, expect []row, output string) {
t.Helper()

trimmed := strings.TrimLeft(output, "Name\tBinary Name\tPackage @ Version\n----\t-----------\t-----------------\n") // nolint
var got []row
var (
trimmed = strings.TrimLeft(output, bingo.PackageRenderablesPrintHeader)
got []row
)
for _, line := range strings.Split(trimmed, "\n") {
s := strings.Fields(line)
if len(s) == 0 {
Expand All @@ -969,17 +996,36 @@ func expectBingoListRows(t testing.TB, expect []row, output string) {
if len(s) > 2 {
r.pkgVersion = s[2]
}
if len(s) > 3 {
r.buildEnvVars = s[3]
}
if len(s) > 4 {
r.buildFlags = s[4]
}
got = append(got, r)
}
testutil.Equals(t, expect, got)
}

func TestExpectBingoListRows(t *testing.T) {
expectBingoListRows(t, []row{
{name: "f4", binName: "f4-v1.1.0", pkgVersion: "github.com/fatih/faillint@v1.1.0"},
{name: "faillint", binName: "faillint-v1.1.0", pkgVersion: "github.com/fatih/faillint@v1.1.0"},
{name: "faillint", binName: "faillint-v1.0.0", pkgVersion: "github.com/fatih/faillint@v1.0.0"},
{name: "go-bindata", binName: "go-bindata-v3.1.1+incompatible", pkgVersion: "github.com/go-bindata/go-bindata/go-bindata@v3.1.1+incompatible"},
{name: "goimports", binName: "goimports-v0.0.0-20200522201501-cb1345f3a375", pkgVersion: "golang.org/x/tools/cmd/goimports@v0.0.0-20200522201501-cb1345f3a375"},
}, "Name\t\t\tBinary Name\t\t\t\t\t\t\tPackage @ Version\n----\t\t\t-----------\t\t\t\t\t\t\t-----------------\nf4\t\t\tf4-v1.1.0\t\t\t\t\t\t\t\tgithub.com/fatih/faillint@v1.1.0\t\t\t\t\t\t\t\nfaillint\t\tfaillint-v1.1.0\t\t\t\t\t\tgithub.com/fatih/faillint@v1.1.0\t\t\t\t\t\t\t\nfaillint\t\tfaillint-v1.0.0\t\t\t\t\t\tgithub.com/fatih/faillint@v1.0.0\t\t\t\t\t\t\t\ngo-bindata\tgo-bindata-v3.1.1+incompatible\t\t\tgithub.com/go-bindata/go-bindata/go-bindata@v3.1.1+incompatible\t\ngoimports\t\tgoimports-v0.0.0-20200522201501-cb1345f3a375\tgolang.org/x/tools/cmd/goimports@v0.0.0-20200522201501-cb1345f3a375")
{name: "pyright", binName: "copyright-v0.0.0-20210112004814-138d5e5695fe", pkgVersion: "github.com/efficientgo/tools/copyright@v0.0.0-20210112004814-138d5e5695fe"},
{name: "embedmd", binName: "embedmd-v1.0.0", pkgVersion: "github.com/campoy/embedmd@v1.0.0", buildEnvVars: "CGO_ENABLED=1", buildFlags: "-tags=lol"},
{name: "faillint", binName: "faillint-v1.5.0", pkgVersion: "github.com/fatih/faillint@v1.5.0"},
{name: "goimports", binName: "goimports-v0.0.0-20210112230658-8b4aab62c064", pkgVersion: "golang.org/x/tools/cmd/goimports@v0.0.0-20210112230658-8b4aab62c064"},
{name: "golangci-lint", binName: "golangci-lint-v1.26.0", pkgVersion: "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.26.0"},
{name: "mdox", binName: "mdox-v0.2.1", pkgVersion: "github.com/bwplotka/mdox@v0.2.1"},
{name: "misspell", binName: "misspell-v0.3.4", pkgVersion: "github.com/client9/misspell/cmd/misspell@v0.3.4"},
{name: "proxy", binName: "proxy-v0.10.0", pkgVersion: "github.com/gomods/athens/cmd/proxy@v0.10.0"},
}, `Name Binary Name Package @ Version Build EnvVars Build Flags
---- ----------- ----------------- ------------- -----------
copyright copyright-v0.0.0-20210112004814-138d5e5695fe github.com/efficientgo/tools/copyright@v0.0.0-20210112004814-138d5e5695fe
embedmd embedmd-v1.0.0 github.com/campoy/embedmd@v1.0.0 CGO_ENABLED=1 -tags=lol
faillint faillint-v1.5.0 github.com/fatih/faillint@v1.5.0
goimports goimports-v0.0.0-20210112230658-8b4aab62c064 golang.org/x/tools/cmd/goimports@v0.0.0-20210112230658-8b4aab62c064
golangci-lint golangci-lint-v1.26.0 github.com/golangci/golangci-lint/cmd/golangci-lint@v1.26.0
mdox mdox-v0.2.1 github.com/bwplotka/mdox@v0.2.1
misspell misspell-v0.3.4 github.com/client9/misspell/cmd/misspell@v0.3.4
proxy proxy-v0.10.0 github.com/gomods/athens/cmd/proxy@v0.10.0
`)
}
25 changes: 1 addition & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"regexp"
"strings"
"syscall"
"text/tabwriter"

"github.com/bwplotka/bingo/pkg/bingo"
"github.com/bwplotka/bingo/pkg/runner"
Expand Down Expand Up @@ -197,29 +196,7 @@ func main() {
}

bingo.SortRenderables(pkgs)

w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 1, '\t', 0)
defer func() { _ = w.Flush() }()

_, _ = fmt.Fprintf(w, "Name\tBinary Name\tPackage @ Version")
_, _ = fmt.Fprintf(w, "\n----\t-----------\t-----------------")
for _, p := range pkgs {
if target != "" && p.Name != target {
continue
}
for _, v := range p.Versions {
_, _ = fmt.Fprintf(w, "\n%s\t%s-%s\t%s@%s", p.Name, p.Name, v.Version, p.PackagePath, v.Version)
}
if target != "" {
return nil
}
}

if target != "" {
return errors.Errorf("Pinned tool %s not found", target)
}
return nil
return pkgs.PrintTab(target, os.Stdout)
}
case "version":
cmdFunc = func(ctx context.Context, r *runner.Runner) error {
Expand Down
40 changes: 39 additions & 1 deletion pkg/bingo/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package bingo

import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"text/tabwriter"

"github.com/bwplotka/bingo/pkg/envars"
"github.com/bwplotka/bingo/pkg/runner"
Expand All @@ -27,6 +29,9 @@ const (
FakeRootModFileName = "go.mod"

NoReplaceCommand = "bingo:no_replace_fetch"

PackageRenderablesPrintHeader = "Name\tBinary Name\tPackage @ Version\tBuild EnvVars\tBuild Flags\n" +
"----\t-----------\t-----------------\t-------------\t-----------\n"
)

// NameFromModFile returns binary name from module file path.
Expand Down Expand Up @@ -446,8 +451,41 @@ func (p PackageRenderable) ToPackages() []Package {
return ret
}

type PackageRenderables []PackageRenderable

func (pkgs PackageRenderables) PrintTab(target string, w io.Writer) error {
tw := new(tabwriter.Writer)
tw.Init(w, 1, 8, 1, '\t', tabwriter.AlignRight)
defer func() { _ = tw.Flush() }()

_, _ = fmt.Fprint(tw, PackageRenderablesPrintHeader)
for _, p := range pkgs {
if target != "" && p.Name != target {
continue
}
for _, v := range p.Versions {
fields := []string{
p.Name,
p.Name + "-" + v.Version,
p.PackagePath + "@" + v.Version,
strings.Join(p.BuildEnvVars, " "),
strings.Join(p.BuildFlags, " "),
}
_, _ = fmt.Fprintln(tw, strings.Join(fields, "\t"))
}
if target != "" {
return nil
}
}

if target != "" {
return errors.Errorf("Pinned tool %s not found", target)
}
return nil
}

// ListPinnedMainPackages lists all bingo pinned binaries (Go main packages) in the same order as seen in the filesystem.
func ListPinnedMainPackages(logger *log.Logger, modDir string, remMalformed bool) (pkgs []PackageRenderable, _ error) {
func ListPinnedMainPackages(logger *log.Logger, modDir string, remMalformed bool) (pkgs PackageRenderables, _ error) {
modFiles, err := filepath.Glob(filepath.Join(modDir, "*.mod"))
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package version
import "github.com/Masterminds/semver"

// Version returns 'bingo' version.
const Version = "v0.4.1"
const Version = "v0.4.2"

var (
Go114 = semver.MustParse("1.14")
Expand Down
2 changes: 1 addition & 1 deletion testdata/testproject_with_bingo_v0_4_1/.bingo/faillint.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT

go 1.16

require github.com/fatih/faillint v1.3.0
require github.com/fatih/faillint v1.3.0 // CGO_ENABLED=1 -tags=extended
12 changes: 12 additions & 0 deletions testdata/testproject_with_bingo_v0_4_2/.bingo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# Ignore everything
*

# But not these files:
!.gitignore
!*.mod
!README.md
!Variables.mk
!variables.env

*tmp.mod
14 changes: 14 additions & 0 deletions testdata/testproject_with_bingo_v0_4_2/.bingo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Project Development Dependencies.

This is directory which stores Go modules with pinned buildable package that is used within this repository, managed by https://github.com/bwplotka/bingo.

* Run `bingo get` to install all tools having each own module file in this directory.
* Run `bingo get <tool>` to install <tool> that have own module file in this directory.
* For Makefile: Make sure to put `include .bingo/Variables.mk` in your Makefile, then use $(<upper case tool name>) variable where <tool> is the .bingo/<tool>.mod.
* For shell: Run `source .bingo/variables.env` to source all environment variable for each tool.
* For go: Import `.bingo/variables.go` to for variable names.
* See https://github.com/bwplotka/bingo or -h on how to add, remove or change binaries dependencies.

## Requirements

* Go 1.14+
Loading