Skip to content

Commit

Permalink
Merge pull request #19 from whywaita/feat/check-execute-plugin
Browse files Browse the repository at this point in the history
check to be able execute plugin binary
  • Loading branch information
whywaita authored Feb 5, 2021
2 parents 7c0f6dd + 17a4c5c commit 7d49230
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions internal/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ func Load() {
if err != nil {
log.Panicf("failed to fetch plugin binary: %+v", err)
}
Config.ShoesPluginPath = fp
log.Printf("use plugin path is %s", pluginPath)
absPath, err := checkBinary(fp)
if err != nil {
log.Panicf("failed to check plugin binary: %+v", err)
}
Config.ShoesPluginPath = absPath
log.Printf("use plugin path is %s\n", Config.ShoesPluginPath)

debug := os.Getenv(EnvDebug)
if debug == "true" {
Expand All @@ -72,6 +76,28 @@ func Load() {
}
}

func checkBinary(p string) (string, error) {
if _, err := os.Stat(p); err != nil {
return "", fmt.Errorf("failed to stat file: %w", err)
}

// need permission of execute
if err := os.Chmod(p, 0777); err != nil {
return "", fmt.Errorf("failed to chmod: %w", err)
}

if filepath.IsAbs(p) {
return p, nil
}

apath, err := filepath.Abs(p)
if err != nil {
return "", fmt.Errorf("failed to get abs: %w", err)
}

return apath, nil
}

// fetch retrieve plugin binaries.
// return saved file path.
func fetch(p string) (string, error) {
Expand All @@ -94,8 +120,9 @@ func fetch(p string) (string, error) {
}

// fetchHTTP fetch plugin binary over HTTP(s).
// save to
// save to current directory.
func fetchHTTP(u *url.URL) (string, error) {
log.Printf("fetch plugin binary from %s\n", u.String())
pwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to working directory: %w", err)
Expand Down

0 comments on commit 7d49230

Please sign in to comment.