refactor: use helper go functions to handle extracting files from a repository
This standardizes the extraction code and makes it easier to reuse in future implementations of file extraction. Additionally, this adds a way to extract folders from codebases.
This commit is contained in:
@@ -4,8 +4,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
@@ -59,95 +57,39 @@ func TriggerAppJSONGetContent(appName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TriggerCorePostDeploy sets a property to
|
||||
// allow the app to be restored on boot
|
||||
// TriggerCorePostDeploy moves the extracted app.json to the app data directory
|
||||
// allowing the app to be restored on boot
|
||||
func TriggerCorePostDeploy(appName string) error {
|
||||
existingAppJSON := getAppJSONPath(appName)
|
||||
processSpecificAppJSON := fmt.Sprintf("%s.%s", existingAppJSON, os.Getenv("DOKKU_PID"))
|
||||
if common.FileExists(processSpecificAppJSON) {
|
||||
if err := os.Rename(processSpecificAppJSON, existingAppJSON); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if common.FileExists(fmt.Sprintf("%s.missing", processSpecificAppJSON)) {
|
||||
if err := os.Remove(fmt.Sprintf("%s.missing", processSpecificAppJSON)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if common.FileExists(existingAppJSON) {
|
||||
if err := os.Remove(existingAppJSON); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return common.CorePostDeploy(common.CorePostDeployInput{
|
||||
AppName: appName,
|
||||
Destination: common.GetAppDataDirectory("app-json", appName),
|
||||
PluginName: "app-json",
|
||||
ExtractedPaths: []common.CorePostDeployPath{
|
||||
{Path: "app.json", IsDirectory: false},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// TriggerCorePostExtract ensures that the main app.json is the one specified by app-json-path
|
||||
func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
|
||||
destination := common.GetAppDataDirectory("app-json", appName)
|
||||
appJSONPath := strings.Trim(reportComputedAppjsonpath(appName), "/")
|
||||
if appJSONPath == "" {
|
||||
appJSONPath = "app.json"
|
||||
}
|
||||
|
||||
existingAppJSON := getAppJSONPath(appName)
|
||||
files, err := filepath.Glob(fmt.Sprintf("%s.*", existingAppJSON))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range files {
|
||||
if err := os.Remove(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
processSpecificAppJSON := fmt.Sprintf("%s.%s", existingAppJSON, os.Getenv("DOKKU_PID"))
|
||||
results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "git-get-property",
|
||||
Args: []string{appName, "source-image"},
|
||||
})
|
||||
appSourceImage := results.StdoutContents()
|
||||
|
||||
results, _ = common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "builder-get-property",
|
||||
Args: []string{appName, "build-dir"},
|
||||
})
|
||||
buildDir := results.StdoutContents()
|
||||
|
||||
repoDefaultAppJSONPath := path.Join(sourceWorkDir, "app.json")
|
||||
if appSourceImage == "" {
|
||||
repoAppJSONPath := path.Join(sourceWorkDir, buildDir, appJSONPath)
|
||||
if !common.FileExists(repoAppJSONPath) {
|
||||
if appJSONPath != "app.json" && common.FileExists(repoDefaultAppJSONPath) {
|
||||
if err := os.Remove(repoDefaultAppJSONPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return common.TouchFile(fmt.Sprintf("%s.missing", processSpecificAppJSON))
|
||||
validator := func(appName string, path string) error {
|
||||
if !common.FileExists(path) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := common.Copy(repoAppJSONPath, processSpecificAppJSON); err != nil {
|
||||
return fmt.Errorf("Unable to extract app.json: %v", err.Error())
|
||||
}
|
||||
|
||||
if appJSONPath != "app.json" {
|
||||
if err := common.Copy(repoAppJSONPath, repoDefaultAppJSONPath); err != nil {
|
||||
return fmt.Errorf("Unable to move app.json into place: %v", err.Error())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := common.CopyFromImage(appName, appSourceImage, path.Join(buildDir, appJSONPath), processSpecificAppJSON); err != nil {
|
||||
return common.TouchFile(fmt.Sprintf("%s.missing", processSpecificAppJSON))
|
||||
}
|
||||
}
|
||||
|
||||
if common.FileExists(processSpecificAppJSON) {
|
||||
result, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "app-json-is-valid",
|
||||
Args: []string{appName, processSpecificAppJSON},
|
||||
Args: []string{appName, path},
|
||||
StreamStdout: true,
|
||||
StreamStderr: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if result.StderrContents() != "" {
|
||||
return errors.New(result.StderrContents())
|
||||
@@ -155,10 +97,30 @@ func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
|
||||
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: add validation to app.json file by ensuring it can be deserialized
|
||||
return nil
|
||||
results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "builder-get-property",
|
||||
Args: []string{appName, "build-dir"},
|
||||
})
|
||||
buildDir := results.StdoutContents()
|
||||
return common.CorePostExtract(common.CorePostExtractInput{
|
||||
AppName: appName,
|
||||
BuildDir: buildDir,
|
||||
Destination: destination,
|
||||
PluginName: "app-json",
|
||||
SourceWorkDir: sourceWorkDir,
|
||||
ToExtract: []common.CorePostExtractToExtract{
|
||||
{
|
||||
Path: appJSONPath,
|
||||
IsDirectory: false,
|
||||
Name: "app.json",
|
||||
Destination: "app.json",
|
||||
Validator: validator,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// TriggerInstall initializes app-json directory structures
|
||||
|
||||
Reference in New Issue
Block a user