commit f5f72ef62640a9a203a5d0025dd1c3d49dc3a3dd Author: Daniel Martí Date: Sun Dec 8 11:33:35 2019 +0000 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..03e3bfc --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2019, Daniel Martí. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dd9aa34 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module mvdan.cc/garble + +go 1.14 + +require github.com/rogpeppe/go-internal v1.5.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..72d9f51 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/rogpeppe/go-internal v1.5.0 h1:Usqs0/lDK/NqTkvrmKSwA/3XkZAs7ZAW/eLeQ2MVBTw= +github.com/rogpeppe/go-internal v1.5.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..f108288 --- /dev/null +++ b/main.go @@ -0,0 +1,62 @@ +// Copyright (c) 2019, Daniel Martí +// See LICENSE for licensing information + +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "path/filepath" +) + +var flagSet = flag.NewFlagSet("garble", flag.ContinueOnError) + +func init() { flagSet.Usage = usage } + +func usage() { + fmt.Fprintf(os.Stderr, ` +Usage of garble: + + go build -toolexec=garble [build flags] [packages] +`[1:]) + flagSet.PrintDefaults() + os.Exit(2) +} + +func main() { os.Exit(main1()) } + +func main1() int { + if err := flagSet.Parse(os.Args[1:]); err != nil { + return 2 + } + args := flagSet.Args() + if len(args) < 1 { + flagSet.Usage() + } + _, tool := filepath.Split(args[0]) + // TODO: trim ".exe" for windows? + transformed := args[1:] + switch tool { + case "compile": + var err error + transformed, err = transformCompile(args[1:]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + } + cmd := exec.Command(args[0], transformed...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + return 0 +} + +func transformCompile(args []string) ([]string, error) { + return args, nil +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..2582539 --- /dev/null +++ b/main_test.go @@ -0,0 +1,44 @@ +// Copyright (c) 2019, Daniel Martí +// See LICENSE for licensing information + +package main + +import ( + "flag" + "os" + "path/filepath" + "testing" + + "github.com/rogpeppe/go-internal/testscript" +) + +func TestMain(m *testing.M) { + os.Exit(testscript.RunMain(m, map[string]func() int{ + "garble": main1, + })) +} + +var update = flag.Bool("u", false, "update testscript output files") + +func TestScripts(t *testing.T) { + t.Parallel() + + testscript.Run(t, testscript.Params{ + Dir: filepath.Join("testdata", "scripts"), + Setup: func(env *testscript.Env) error { + env.Vars = append(env.Vars, "TESTBIN="+os.Args[0]) + + for _, name := range [...]string{ + "HOME", + "USERPROFILE", // $HOME for windows + "GOCACHE", + } { + if value := os.Getenv(name); value != "" { + env.Vars = append(env.Vars, name+"="+value) + } + } + return nil + }, + UpdateScripts: *update, + }) +} diff --git a/testdata/scripts/basic.txt b/testdata/scripts/basic.txt new file mode 100644 index 0000000..99fb66d --- /dev/null +++ b/testdata/scripts/basic.txt @@ -0,0 +1,33 @@ +[!exec:go] skip +[!symlink] skip + +# We want to use the current garble, not whichever happens to be globally +# installed. Use the test binary with TESTSCRIPT_COMMAND, which we know will +# correctly run garble thanks to go-internal/testscript. +mkdir .bin +symlink .bin/garble$exe -> $TESTBIN +env PATH=.bin${:}$PATH +env TESTSCRIPT_COMMAND=garble + +exec go build -toolexec=garble main.go +exists main +exec ./main +cmp stderr main.stderr +# TODO +# ! grep 'globalVar' main +# ! grep 'globalFunc' main + +-- main.go -- +package main + +var globalVar = "global value" + +func globalFunc() { println("global func body") } + +func main() { + println(globalVar) + globalFunc() +} +-- main.stderr -- +global value +global func body diff --git a/testdata/scripts/help.txt b/testdata/scripts/help.txt new file mode 100644 index 0000000..92d8928 --- /dev/null +++ b/testdata/scripts/help.txt @@ -0,0 +1,12 @@ +! garble +stderr -count=1 'Usage of' +stderr '\tgo build' +! stdout . + +! garble -h +stderr 'Usage of' +! stdout . + +! garble -badflag +stderr 'Usage of' +! stdout .