这是indexloc提供的服务,不要输入任何密码
Skip to content

Use helper go functions to handle extracting files from a repository #7816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 38 additions & 76 deletions plugins/app-json/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/dokku/dokku/plugins/common"
Expand Down Expand Up @@ -59,106 +57,70 @@ 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
validator := func(appName string, path string) error {
if !common.FileExists(path) {
return nil
}
}

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))
}

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())
}

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
Expand Down
Loading
Loading