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

cmd/vfsgendev: Run generator in package directory #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion cmd/vfsgendev/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type data struct {

var generateTemplate = template.Must(template.New("").Funcs(template.FuncMap{
"quote": strconv.Quote,
}).Parse(`package main
}).Parse(`// +build ignore

package main

import (
"log"
Expand Down
21 changes: 8 additions & 13 deletions cmd/vfsgendev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

var (
Expand Down Expand Up @@ -78,31 +78,26 @@ func run(importPath, variableName, tag string) error {
return nil
}

err = goRun(buf.String(), tag)
err = goRun(buf.String(), importPath, tag)
return err
}

// goRun runs Go code src with build tags.
func goRun(src string, tags string) error {
// Create a temp folder.
tempDir, err := ioutil.TempDir("", "vfsgendev_")
func goRun(src, importPath, tags string) error {
// Create a temp file.
importPath = strings.Replace(importPath, "/", "_", -1)
tempFile := "vfsgendev_" + importPath + ".go"
err := ioutil.WriteFile(tempFile, []byte(src), 0600)
if err != nil {
return err
}
defer func() {
err := os.RemoveAll(tempDir)
err := os.Remove(tempFile)
if err != nil {
fmt.Fprintln(os.Stderr, "warning: error removing temp dir:", err)
}
}()

// Write the source code file.
tempFile := filepath.Join(tempDir, "generate.go")
err = ioutil.WriteFile(tempFile, []byte(src), 0600)
if err != nil {
return err
}

// Compile and run the program.
cmd := exec.Command("go", "run", "-tags="+tags, tempFile)
cmd.Stdout = os.Stdout
Expand Down