refactor: move app listing back to common
Without this, we'd need to duplicate some logic or make it more complex via a plugin trigger.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
TRIGGERS = triggers/core-post-deploy triggers/install triggers/post-delete
|
||||
TRIGGERS = triggers/app-list triggers/core-post-deploy triggers/install triggers/post-delete
|
||||
BUILD = prop common triggers
|
||||
PLUGIN_NAME = common
|
||||
|
||||
|
||||
@@ -244,12 +244,12 @@ func GetRunningImageTag(appName string, imageTag string) (string, error) {
|
||||
}
|
||||
|
||||
// DokkuApps returns a list of all local apps
|
||||
func DokkuApps() (apps []string, err error) {
|
||||
func DokkuApps() ([]string, error) {
|
||||
apps := []string{}
|
||||
dokkuRoot := MustGetEnv("DOKKU_ROOT")
|
||||
files, err := ioutil.ReadDir(dokkuRoot)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("You haven't deployed any applications yet")
|
||||
return
|
||||
return apps, fmt.Errorf("You haven't deployed any applications yet")
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
@@ -264,11 +264,10 @@ func DokkuApps() (apps []string, err error) {
|
||||
}
|
||||
|
||||
if len(apps) == 0 {
|
||||
err = fmt.Errorf("You haven't deployed any applications yet")
|
||||
return
|
||||
return apps, fmt.Errorf("You haven't deployed any applications yet")
|
||||
}
|
||||
|
||||
return
|
||||
return filterApps(apps)
|
||||
}
|
||||
|
||||
// GetAppImageName returns image identifier for a given app, tag tuple. validate if tag is presented
|
||||
|
||||
44
plugins/common/functions.go
Normal file
44
plugins/common/functions.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func filterApps(apps []string) ([]string, error) {
|
||||
if !PlugnTriggerExists("user-auth-app") {
|
||||
return apps, nil
|
||||
}
|
||||
|
||||
sshUser := os.Getenv("SSH_USER")
|
||||
if sshUser == "" {
|
||||
sshUser = os.Getenv("USER")
|
||||
}
|
||||
|
||||
sshName := os.Getenv("SSH_NAME")
|
||||
if sshName == "" {
|
||||
sshName = "default"
|
||||
}
|
||||
|
||||
args := append([]string{sshUser, sshName}, apps...)
|
||||
b, _ := PlugnTriggerOutput("user-auth-app", args...)
|
||||
filteredApps := strings.Split(strings.TrimSpace(string(b[:])), "\n")
|
||||
filteredApps = removeEmptyEntries(filteredApps)
|
||||
|
||||
if len(filteredApps) == 0 {
|
||||
return filteredApps, fmt.Errorf("You haven't deployed any applications yet")
|
||||
}
|
||||
|
||||
return filteredApps, nil
|
||||
}
|
||||
|
||||
func removeEmptyEntries(s []string) []string {
|
||||
var r []string
|
||||
for _, str := range s {
|
||||
if str != "" {
|
||||
r = append(r, str)
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
@@ -17,6 +17,8 @@ func main() {
|
||||
|
||||
var err error
|
||||
switch trigger {
|
||||
case "app-list":
|
||||
err = common.TriggerAppList()
|
||||
case "core-post-deploy":
|
||||
appName := flag.Arg(0)
|
||||
err = common.TriggerCorePostDeploy(appName)
|
||||
|
||||
@@ -5,6 +5,16 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// TriggerAppList outputs each app name to stdout on a newline
|
||||
func TriggerAppList() error {
|
||||
apps, _ := DokkuApps()
|
||||
for _, app := range apps {
|
||||
Log(app)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TriggerCorePostDeploy associates the container with a specified network
|
||||
func TriggerCorePostDeploy(appName string) error {
|
||||
quiet := os.Getenv("DOKKU_QUIET_OUTPUT")
|
||||
|
||||
Reference in New Issue
Block a user