From 122288ee4798f7b975f8f19fdfd29c38f73bc368 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Tue, 11 Feb 2020 10:28:59 +0900 Subject: [PATCH] feat: `variant export binary SRC_DIR DST_FILE` to build a single executable binary at DST_FILE --- Makefile | 5 +++++ README.md | 18 ++++++++++++++++++ export_binary.go | 26 ++++++++++++++++++++++++++ pkg/app/app_shim.go | 27 +++++++++++++++++++++++++++ variant.go | 1 + 5 files changed, 77 insertions(+) create mode 100644 export_binary.go diff --git a/Makefile b/Makefile index 87111b3..816a61c 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 7edb86e..55e482c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/export_binary.go b/export_binary.go new file mode 100644 index 0000000..c6703e1 --- /dev/null +++ b/export_binary.go @@ -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 + }, + } +} diff --git a/pkg/app/app_shim.go b/pkg/app/app_shim.go index 2a96dc9..494f2b4 100644 --- a/pkg/app/app_shim.go +++ b/pkg/app/app_shim.go @@ -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 diff --git a/variant.go b/variant.go index 7eeac0b..f3c3255 100644 --- a/variant.go +++ b/variant.go @@ -764,6 +764,7 @@ func (r *Runner) createVariantRootCommand() *cobra.Command { exportCmd.AddCommand(shimCmd) exportCmd.AddCommand(newExportGo(r)) + exportCmd.AddCommand(newExportBinary(r)) } generateCmd := &cobra.Command{