This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Call commands defined by extensions #119
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
00931ce
remove hardcode appsody
makandre 6e97062
extensions api route
makandre d79dd91
call extensions api
makandre 3fea17d
call command
makandre 121c133
typo
makandre 1890960
address a couple of comments
makandre 4b29510
rename function
makandre 0ad6861
return unknown
makandre c59beb7
revert a change
makandre 8c6072a
fix for python detection
makandre 3eaaf17
prevent path traversal
makandre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 IBM Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package apiroutes | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/eclipse/codewind-installer/config" | ||
"github.com/eclipse/codewind-installer/utils" | ||
) | ||
|
||
type ( | ||
// Extension represents a project extension defined by codewind.yaml | ||
Extension struct { | ||
ProjectType string `json:"projectType"` | ||
Detection string `json:"detection"` | ||
Commands []utils.ExtensionCommand `json:"commands"` | ||
} | ||
) | ||
|
||
// GetExtensions gets project extensions from PFE's REST API. | ||
func GetExtensions() ([]Extension, error) { | ||
resp, err := http.Get(config.PFEApiRoute() + "extensions") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
byteArray, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var extensions []Extension | ||
json.Unmarshal(byteArray, &extensions) | ||
|
||
return extensions, nil | ||
} |
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 |
---|---|---|
|
@@ -9,43 +9,45 @@ | |
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package utils | ||
package utils | ||
|
||
import ( | ||
import ( | ||
"bytes" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"log" | ||
"runtime" | ||
"path/filepath" | ||
) | ||
|
||
// SuccessfullyCallAppsodyInit calls Appsody Init to initialise Appsody projects and returns a boolean to indicate success | ||
func SuccessfullyCallAppsodyInit(projectPath string) (bool, error) { | ||
|
||
type ( | ||
// ExtensionCommand represents a command defined by a project extension | ||
ExtensionCommand struct { | ||
Name string `json:"name"` | ||
Command string `json:"command"` | ||
Args []string `json:"args"` | ||
} | ||
) | ||
|
||
// RunCommand runs a command defined by an extension | ||
func RunCommand(projectPath string, command ExtensionCommand) error { | ||
cwd, err := os.Executable() | ||
if err != nil { | ||
log.Println("There was a problem with locating appsody binary") | ||
return false, err | ||
log.Println("There was a problem with locating the command directory") | ||
return err | ||
} | ||
const GOOS string = runtime.GOOS | ||
installerPath := filepath.Dir(cwd) | ||
appsodyBinPath := "/appsody" | ||
if GOOS == "windows" { | ||
appsodyBinPath = "/appsody.exe" | ||
} | ||
appsodyBin := installerPath + appsodyBinPath | ||
cmd := exec.Command(appsodyBin, "init") | ||
commandBin := filepath.Join(installerPath, command.Command) | ||
cmd := exec.Command(commandBin, command.Args...) | ||
cmd.Dir = projectPath | ||
output := new(bytes.Buffer) | ||
cmd.Stdout = output | ||
cmd.Stderr = output | ||
if err := cmd.Start(); err != nil { // after 'Start' the program is continued and script is executing in background | ||
log.Println("There was a problem initializing the Appsody project: ", err, ". Project was not initialized.") | ||
return false, err | ||
log.Println("There was a problem running the command:", command.Command) | ||
return err | ||
} | ||
log.Printf("Please wait while the Appsody project is initialized... %s \n", output.String()) | ||
log.Printf("Please wait while the project is initialized... %s", output.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I needed to revert this back to |
||
cmd.Wait() | ||
log.Println(output.String()) // Wait to finish execution, so we can read all output | ||
return true, nil | ||
} | ||
|
||
return nil | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was actually the auto-linter that changed this :)