Skip to content

Commit

Permalink
feat: variant export binary SRC_DIR DST_FILE to build a single exec…
Browse files Browse the repository at this point in the history
…utable binary at DST_FILE
  • Loading branch information
mumoshu committed Feb 11, 2020
1 parent e805f17 commit 122288e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ smoke: build
go build -o build/simple/simple ./build/simple
build/simple/simple -h | tee smoke.log
grep "Namespace to interact with" smoke.log

rm build/simple/simple
./variant export binary examples/simple build/simple
build/simple/simple -h | tee smoke2.log
grep "Namespace to interact with" smoke2.log
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ As you've seen in the help output, `variant run deploy` runs the `deploy` job, w
$ variant run deploy
```

Once you're finished developing the command, let's build a single executable binary of the command for easy distribution:

```console
$ variant export binary ./ build/myapp
```

The exported executable binary accepts the same arguments as `variant run`:

```console
$ ./build/myapp -h

$ ./build/myapp run deploy
```

Congratulations! You're now ready to dive deep and solve your own problems with Variant.

Still curious how Variant helps developing your own command as it grows?

Head over to the following per-topic sections for more features:

- [Generating Shims](#generating-shims) to make your Variant command look native
Expand Down
26 changes: 26 additions & 0 deletions export_binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package variant

import (
"github.com/spf13/cobra"
)

func newExportBinary(r *Runner) *cobra.Command {
return &cobra.Command{
Use: "binary SRC_DIR DST_FILE",
Short: "Builds the single executable binary of the Variant command defined in SRC_DIR. Basically `variant export go SRC_DIR TMP_DIR && go build -o DST_FILE TMP_DIR`",
Example: `$ variant export binary examples/simple build/simple
$ build/simple -h
$ build/simple app deploy -n default
`,
Args: cobra.ExactArgs(2),
RunE: func(c *cobra.Command, args []string) error {
err := r.ap.ExportBinary(args[0], args[1])
if err != nil {
c.SilenceUsage = true
}
return err
},
}
}
27 changes: 27 additions & 0 deletions pkg/app/app_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ import (
"github.com/mumoshu/variant2/pkg/conf"
)

func (app *App) ExportBinary(srcDir, dstFile string) error {
tmpDir, err := ioutil.TempDir("", "variant-"+filepath.Base(srcDir))
if err != nil {
return err
}

defer os.RemoveAll(tmpDir)

if err := app.ExportGo(srcDir, tmpDir); err != nil {
return err
}

if err := os.MkdirAll(filepath.Dir(dstFile), 0755); err != nil {
return err
}

absDstFile, err := filepath.Abs(dstFile)

if err != nil {
return err
}

_, err = app.execCmd("sh", []string{"-c", fmt.Sprintf("cd %s; go mod init %s && go build -o %s %s", tmpDir, filepath.Base(srcDir), absDstFile, tmpDir)}, map[string]string{}, true)

return err
}

func (app *App) ExportGo(srcDir, dstDir string) error {
if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ func (r *Runner) createVariantRootCommand() *cobra.Command {

exportCmd.AddCommand(shimCmd)
exportCmd.AddCommand(newExportGo(r))
exportCmd.AddCommand(newExportBinary(r))
}

generateCmd := &cobra.Command{
Expand Down

0 comments on commit 122288e

Please sign in to comment.