Skip to content

Commit

Permalink
Embed local imports and cached remote imports into binary
Browse files Browse the repository at this point in the history
Resolves #28
  • Loading branch information
mumoshu committed Sep 27, 2020
1 parent 604396c commit 1211ff7
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ variant
bin/golangci-lint
bin/goimports
\#*#
.variant2
Empty file.
18 changes: 18 additions & 0 deletions examples/advanced/import-multi/export.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

PROJECT_ROOT=../../..
VARIANT=${PROJECT_ROOT}/variant

export VARIANT_BUILD_VER=v0.33.3
export VARIANT_BUILD_REPLACE=$(pwd)/${PROJECT_ROOT}

rm -rf ../exported
rm -rf ../compiled

(cd ${PROJECT_ROOT}; make build)
${VARIANT} export go ../import-multi ../exported
${VARIANT} export binary ../import-multi ../compiled

${VARIANT} run foo baz HELLO1

(cd ..; ./compiled foo baz HELLO2)
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/mattn/go-isatty v0.0.12
github.com/nlopes/slack v0.6.0
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/rs/xid v1.2.1
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -38,7 +39,7 @@ require (
github.com/zclconf/go-cty-yaml v1.0.1
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 // indirect
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1 h1:/K3IL0Z1quvmJ7X0A1AwNEK7CRkVK3YwfOU/QAL4WGg=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:5STLWrekHfjyYwxBRVRXNOSewLJ3PWfDJd1VyTS21fI=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -1362,6 +1364,7 @@ google.golang.org/api v0.18.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/
google.golang.org/api v0.20.0 h1:jz2KixHX7EcCPiQrySzPdnYT7DbINAypCqKZ1Z7GM40=
google.golang.org/api v0.20.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE=
google.golang.org/api v0.21.0 h1:zS+Q/CJJnVlXpXQVIz+lH0ZT2lBuT2ac7XD8Y/3w6hY=
google.golang.org/api v0.32.0 h1:Le77IccnTqEa8ryp9wIpX5W3zYm7Gf9LhOp9PHcwFts=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
112 changes: 89 additions & 23 deletions pkg/app/app_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package app
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/mumoshu/variant2/pkg/fs"

"github.com/hashicorp/hcl/v2"

"github.com/mumoshu/variant2/pkg/conf"
Expand Down Expand Up @@ -38,7 +41,49 @@ func (app *App) ExportBinary(srcDir, dstFile string) error {
_, err = app.execCmd(
Command{
Name: "sh",
Args: []string{"-c", fmt.Sprintf("cd %s; go mod init %s && go build -o %s %s", tmpDir, filepath.Base(srcDir), absDstFile, tmpDir)},
Args: []string{"-c", fmt.Sprintf("cd %s; go mod init %s && go get github.com/rakyll/statik && statik -src=%s", tmpDir, filepath.Base(srcDir), fs.VendorPrefix)},
Env: map[string]string{},
},
true,
)
if err != nil {
return err
}

variantVer := os.Getenv("VARIANT_BUILD_VER")
if variantVer != "" {
_, err = app.execCmd(
Command{
Name: "sh",
Args: []string{"-c", fmt.Sprintf("cd %s; go mod edit -require=github.com/mumoshu/variant2@%s", tmpDir, variantVer)},
Env: map[string]string{},
},
true,
)
if err != nil {
return err
}
}

variantReplace := os.Getenv("VARIANT_BUILD_REPLACE")
if variantReplace != "" {
_, err = app.execCmd(
Command{
Name: "sh",
Args: []string{"-c", fmt.Sprintf("cd %s; go mod edit -replace github.com/mumoshu/variant2@%s=%s", tmpDir, variantVer, variantReplace)},
Env: map[string]string{},
},
true,
)
if err != nil {
return err
}
}

_, err = app.execCmd(
Command{
Name: "sh",
Args: []string{"-c", fmt.Sprintf("cd %s; go build -o %s %s", tmpDir, absDstFile, tmpDir)},
Env: map[string]string{},
},
true,
Expand All @@ -52,22 +97,12 @@ func (app *App) ExportGo(srcDir, dstDir string) error {
return err
}

fs, err := findVariantFiles(srcDir)
if err != nil {
return err
}

srcs, err := loadFiles(fs...)
a, err := New(FromDir(srcDir))
if err != nil {
return err
}

files, _, err := newConfigFromSources(srcs)
if err != nil {
return err
}

merged, err := merge(files)
merged, err := merge(a.Files)
if err != nil {
return err
}
Expand All @@ -83,6 +118,7 @@ import (
"strings"
variant "github.com/mumoshu/variant2"
_ "${MODULE_NAME}/statik"
)
func main() {
Expand Down Expand Up @@ -116,6 +152,9 @@ func main() {
}
`, "`"+strings.Replace(string(merged)+"\n", "`", backquote, -1)+"`"))

replaced := strings.ReplaceAll(string(code), "${MODULE_NAME}", filepath.Base(srcDir))
code = []byte(replaced)

if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
}
Expand All @@ -126,25 +165,52 @@ func main() {
return err
}

walkErr := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
rel, err := filepath.Rel(srcDir, path)
abs := filepath.Join(dstDir, fs.VendorPrefix, rel)

if info.IsDir() {
return os.MkdirAll(abs, 0755)
}

return copyFile(path, abs)
})
if walkErr != nil {
return fmt.Errorf("copying files from %s: %w", srcDir, walkErr)
}

return nil
}

func (app *App) ExportShim(srcDir, dstDir string) error {
if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
func copyFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}

fs, err := findVariantFiles(srcDir)
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}

srcs, err := loadFiles(fs...)
if err != nil {
func (app *App) ExportShim(srcDir, dstDir string) error {
if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
}

files, _, err := newConfigFromSources(srcs)
a, err := New(FromDir(srcDir))
if err != nil {
return err
}
Expand All @@ -156,7 +222,7 @@ func (app *App) ExportShim(srcDir, dstDir string) error {
binName = "variant"
}

return exportWithShim(binName, files, dstDir)
return exportWithShim(binName, a.Files, dstDir)
}

func merge(files map[string]*hcl.File) ([]byte, error) {
Expand Down
21 changes: 13 additions & 8 deletions pkg/app/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package app

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"

fs2 "github.com/mumoshu/variant2/pkg/fs"

"github.com/hashicorp/hcl/v2/ext/typeexpr"

"github.com/hashicorp/hcl/v2"
Expand All @@ -26,11 +27,11 @@ type configurable struct {
Body hcl.Body
}

func loadFiles(filenames ...string) (map[string][]byte, error) {
func loadFiles(fs *fs2.FileSystem, filenames ...string) (map[string][]byte, error) {
srcs := map[string][]byte{}

for _, filename := range filenames {
src, err := ioutil.ReadFile(filename)
src, err := fs.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,7 +109,9 @@ type Setup func() (*Instance, error)

func FromFile(path string) Setup {
return func() (*Instance, error) {
srcs, err := loadFiles(path)
fs := &fs2.FileSystem{}

srcs, err := loadFiles(fs, path)
if err != nil {
return nil, err
}
Expand All @@ -124,12 +127,14 @@ func FromFile(path string) Setup {

func FromDir(dir string) Setup {
return func() (*Instance, error) {
fs, err := findVariantFiles(dir)
fs := &fs2.FileSystem{}

files, err := findVariantFiles(fs, dir)
if err != nil {
return nil, err
}

srcs, err := loadFiles(fs...)
srcs, err := loadFiles(fs, files...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -188,7 +193,7 @@ func NewImportFunc(importBaseDir string, f func(string) (*App, error)) func(stri
}
}

func findVariantFiles(dirPathOrURL string) ([]string, error) {
func findVariantFiles(fs *fs2.FileSystem, dirPathOrURL string) ([]string, error) {
var dir string

s := strings.Split(dirPathOrURL, "::")
Expand Down Expand Up @@ -230,7 +235,7 @@ func findVariantFiles(dirPathOrURL string) ([]string, error) {
dir = dirPathOrURL
}

files, err := conf.FindVariantFiles(dir)
files, err := conf.FindVariantFiles(fs, dir)
if err != nil {
return nil, fmt.Errorf("failed to get %s files: %v", conf.VariantFileExt, err)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/conf/load.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package conf

import (
"os"
"path/filepath"

"github.com/mumoshu/variant2/pkg/fs"
)

const (
Expand All @@ -11,19 +12,19 @@ const (

// FindVariantFiles walks the given path and returns the files ending whose ext is .variant
// Also, it returns the path if the path is just a file and a HCL file
func FindVariantFiles(path string) ([]string, error) {
func FindVariantFiles(fs *fs.FileSystem, path string) ([]string, error) {
var (
files []string
err error
)

fi, err := os.Stat(path)
fi, err := fs.Stat(path)
if err != nil {
return files, err
}

if fi.IsDir() {
found, err := filepath.Glob(filepath.Join(path, "*"+VariantFileExt+"*"))
found, err := fs.Glob(filepath.Join(path, "*"+VariantFileExt+"*"))
if err != nil {
return nil, err
}
Expand All @@ -35,7 +36,7 @@ func FindVariantFiles(path string) ([]string, error) {
continue
}

info, err := os.Stat(f)
info, err := fs.Stat(f)

if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 1211ff7

Please sign in to comment.