github.com/darkliquid/glide@v0.12.3/action/plugin.go (about)

     1  package action
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  
     7  	"github.com/Masterminds/glide/msg"
     8  )
     9  
    10  // Plugin attempts to find and execute a plugin based on a command.
    11  //
    12  // Exit code 99 means the plugin was never executed. Code 1 means the program
    13  // exited badly.
    14  func Plugin(command string, args []string) {
    15  
    16  	cwd, err := os.Getwd()
    17  	if err != nil {
    18  		msg.ExitCode(99)
    19  		msg.Die("Could not get working directory: %s", err)
    20  	}
    21  
    22  	cmd := "glide-" + command
    23  	var fullcmd string
    24  	if fullcmd, err = exec.LookPath(cmd); err != nil {
    25  		fullcmd = cwd + "/" + cmd
    26  		if _, err := os.Stat(fullcmd); err != nil {
    27  			msg.ExitCode(99)
    28  			msg.Die("Command %s does not exist.", cmd)
    29  		}
    30  	}
    31  
    32  	// Turning os.Args first argument from `glide` to `glide-command`
    33  	args[0] = cmd
    34  	// Removing the first argument (command)
    35  	removed := false
    36  	for i, v := range args {
    37  		if removed == false && v == command {
    38  			args = append(args[:i], args[i+1:]...)
    39  			removed = true
    40  		}
    41  	}
    42  	pa := os.ProcAttr{
    43  		Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
    44  		Dir:   cwd,
    45  	}
    46  
    47  	msg.Debug("Delegating to plugin %s (%v)\n", fullcmd, args)
    48  
    49  	proc, err := os.StartProcess(fullcmd, args, &pa)
    50  	if err != nil {
    51  		msg.Err("Failed to execute %s: %s", cmd, err)
    52  		os.Exit(98)
    53  	}
    54  
    55  	if _, err := proc.Wait(); err != nil {
    56  		msg.Err(err.Error())
    57  		os.Exit(1)
    58  	}
    59  }