Skip to content

Commit

Permalink
Generate plugin with cli/go run
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Gajewski committed Dec 6, 2016
1 parent be07a4f commit 698e30c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 41 deletions.
79 changes: 45 additions & 34 deletions cli/cli.go
Expand Up @@ -59,6 +59,42 @@ type Config struct {
AsInterface bool
}

// String formats config as command line arguments
func (c Config) String() string {
var commandLine []string

commandLine = append(commandLine, fmt.Sprintf(
"-plugin-path %s -plugin-package %s -output-name %s -output-path %s -output-package %s",
c.PluginPath,
c.PluginPackage,
c.OutputName,
c.OutputPath,
c.OutputPackage,
))

if c.CheckSha256 {
commandLine = append(commandLine, "-sha256")
}

if c.DereferenceVariables {
commandLine = append(commandLine, "-dereference-vars")
}

if c.ForcePluginRebuild {
commandLine = append(commandLine, "-rebuild")
}

if c.AsInterface {
commandLine = append(commandLine, "-interface")
}

if c.HideVariables {
commandLine = append(commandLine, "-hide-vars")
}

return strings.Join(commandLine, " ")
}

// Cli is responsible for generating plugin wrapper, can be initialized with New()
type Cli struct {
config Config
Expand Down Expand Up @@ -210,46 +246,21 @@ func (c *Cli) buildPluginFromSources(pluginPath string, pluginPackage string) er
return err
}

command := []string{"build", "-x", "-v", "-buildmode=plugin", "-o", pluginPath, pluginPackage}
command := []string{"build", "-o", pluginPath, "-buildmode=plugin", pluginPackage}

c.logger.Printf("Running: go %s", strings.Join(command, " "))

return exec.Command("go", command...).Run()
}
cmd := exec.Command("go", command...)

func (c *Cli) buildCommandArgs() string {
var commandLine []string

commandLine = append(commandLine, fmt.Sprintf(
"go-bind-plugin -plugin-path %s -plugin-package %s -output-name %s -output-path %s -output-package %s",
c.config.PluginPath,
c.config.PluginPackage,
c.config.OutputName,
c.config.OutputPath,
c.config.OutputPackage,
))

if c.config.CheckSha256 {
commandLine = append(commandLine, "-sha256")
}

if c.config.DereferenceVariables {
commandLine = append(commandLine, "-dereference-vars")
}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Env = os.Environ()

if c.config.ForcePluginRebuild {
commandLine = append(commandLine, "-rebuild")
}

if c.config.AsInterface {
commandLine = append(commandLine, "-interface")
}

if c.config.HideVariables {
commandLine = append(commandLine, "-hide-vars")
}
return cmd.Run()
}

return strings.Join(commandLine, " ")
func (c *Cli) buildCommandArgs() string {
return fmt.Sprintf("go-bind-plugin %s", c.config.String())
}

func validateConfig(config *Config) error {
Expand Down
38 changes: 31 additions & 7 deletions cli/cli_test.go
Expand Up @@ -59,13 +59,8 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {

t.Logf("[Test %d] Generator config: %+v", i, config)

client, err := cli.New(config, log.New(os.Stdout, "", 0))
if err != nil {
t.Fatalf("[Test %d] Expected err to be nil, actual: %s", i, err)
}

if generateErr := client.GenerateFile(); generateErr != nil {
t.Fatalf("[Test %d] Expected err to be nil, actual: %s", i, generateErr)
if err := generatePluginWithCli(config, t); err != nil {
t.Fatalf("[Test %d] Expected error to be nil, actual: %s", i, err)
}

runFile := fmt.Sprintf("./internal/test_fixtures/generated/%s/plugin.go", testCase.Plugin)
Expand All @@ -83,6 +78,35 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {
}
}

// Switch to generatePluginWithCli when https://github.com/golang/go/issues/17928 is solved
func generatePluginWithCli(config cli.Config, t *testing.T) error {
client, err := cli.New(config, log.New(os.Stdout, "", 0))

if err != nil {
return err
}

if generateErr := client.GenerateFile(); generateErr != nil {
return generateErr
}

return nil
}

func generatePluginViaCommandLine(config cli.Config, t *testing.T) error {

args := []string{"run", "../main.go"}
args = append(args, strings.Split(config.String(), " ")...)
cmd := exec.Command("go", args...)

t.Logf("Generating plugin with config: %+v", cmd)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
}

func runPlugin(code string, path string, config cli.Config) (string, error) {
file, err := os.OpenFile(config.OutputPath, os.O_APPEND|os.O_WRONLY, 0700)

Expand Down

0 comments on commit 698e30c

Please sign in to comment.