-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from wanghaoran1988/ocm_plugin
Support git style ocm plugin
- Loading branch information
Showing
3 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package plugin | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// Handler is capable of parsing command line arguments | ||
// and performing executable filename lookups to search | ||
// for valid plugin files, and execute found plugins. | ||
type Handler interface { | ||
// exists at the given filename, or a boolean false. | ||
// Lookup will iterate over a list of given prefixes | ||
// in order to recognize valid plugin filenames. | ||
// The first filepath to match a prefix is returned. | ||
Lookup(filename string) (string, bool) | ||
// Execute receives an executable's filepath, a slice | ||
// of arguments, and a slice of environment variables | ||
// to relay to the executable. | ||
Execute(executablePath string, cmdArgs, environment []string) error | ||
} | ||
|
||
// DefaultHandler implements Handler | ||
type DefaultHandler struct { | ||
ValidPrefixes []string | ||
} | ||
|
||
// NewDefaultPluginHandler instantiates the DefaultPluginHandler with a list of | ||
// given filename prefixes used to identify valid plugin filenames. | ||
func NewDefaultPluginHandler(validPrefixes []string) Handler { | ||
return &DefaultHandler{ | ||
ValidPrefixes: validPrefixes, | ||
} | ||
} | ||
|
||
// Lookup implements Handler | ||
func (h *DefaultHandler) Lookup(filename string) (string, bool) { | ||
for _, prefix := range h.ValidPrefixes { | ||
path, err := exec.LookPath(fmt.Sprintf("%s-%s", prefix, filename)) | ||
if err != nil || len(path) == 0 { | ||
continue | ||
} | ||
return path, true | ||
} | ||
|
||
return "", false | ||
} | ||
|
||
// Execute implements Handler | ||
func (h *DefaultHandler) Execute(executablePath string, cmdArgs, environment []string) error { | ||
// #nosec G204 | ||
cmd := exec.Command(executablePath, cmdArgs...) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdin = os.Stdin | ||
cmd.Env = environment | ||
return cmd.Run() | ||
} | ||
|
||
// HandlePluginCommand receives a pluginHandler and command-line arguments and attempts to find | ||
// a plugin executable on the PATH that satisfies the given arguments. | ||
func HandlePluginCommand(pluginHandler Handler, cmdArgs []string) (found bool, err error) { | ||
remainingArgs := []string{} // all "non-flag" arguments | ||
|
||
for idx := range cmdArgs { | ||
if strings.HasPrefix(cmdArgs[idx], "-") { | ||
break | ||
} | ||
remainingArgs = append(remainingArgs, strings.Replace(cmdArgs[idx], "-", "_", -1)) | ||
} | ||
|
||
foundBinaryPath := "" | ||
|
||
// attempt to find binary, starting at longest possible name with given cmdArgs | ||
for len(remainingArgs) > 0 { | ||
path, found := pluginHandler.Lookup(strings.Join(remainingArgs, "-")) | ||
if !found { | ||
remainingArgs = remainingArgs[:len(remainingArgs)-1] | ||
continue | ||
} | ||
|
||
foundBinaryPath = path | ||
break | ||
} | ||
|
||
if len(foundBinaryPath) == 0 { | ||
return false, nil | ||
} | ||
|
||
// invoke cmd binary relaying the current environment and args given | ||
if err := pluginHandler.Execute(foundBinaryPath, cmdArgs[len(remainingArgs):], os.Environ()); err != nil { | ||
return true, err | ||
} | ||
|
||
return true, nil | ||
} |