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:
Jose Diaz-Gonzalez
2022-02-26 02:55:34 -05:00
parent 90eb5462f7
commit ac58b502c5
27 changed files with 83 additions and 128 deletions

View File

@@ -1,5 +1,5 @@
SUBCOMMANDS = subcommands/clone subcommands/create subcommands/destroy subcommands/exists subcommands/list subcommands/lock subcommands/locked subcommands/rename subcommands/report subcommands/unlock
TRIGGERS = triggers/app-create triggers/app-destroy triggers/app-exists triggers/app-list triggers/app-maybe-create triggers/deploy-source-set triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/report
TRIGGERS = triggers/app-create triggers/app-destroy triggers/app-exists triggers/app-maybe-create triggers/deploy-source-set triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/report
BUILD = commands subcommands triggers
PLUGIN_NAME = apps

View File

@@ -1,13 +1,5 @@
package apps
import (
"fmt"
"io/ioutil"
"strings"
"github.com/dokku/dokku/plugins/common"
)
var (
// DefaultProperties is a map of all valid network properties with corresponding default property values
DefaultProperties = map[string]string{
@@ -21,30 +13,3 @@ var (
"deploy-source-metadata": true,
}
)
// DokkuApps returns a list of all local apps
func DokkuApps() ([]string, error) {
apps := []string{}
dokkuRoot := common.MustGetEnv("DOKKU_ROOT")
files, err := ioutil.ReadDir(dokkuRoot)
if err != nil {
return apps, fmt.Errorf("You haven't deployed any applications yet")
}
for _, f := range files {
appRoot := common.AppRoot(f.Name())
if !common.DirectoryExists(appRoot) {
continue
}
if strings.HasPrefix(f.Name(), ".") {
continue
}
apps = append(apps, f.Name())
}
if len(apps) == 0 {
return apps, fmt.Errorf("You haven't deployed any applications yet")
}
return filterApps(apps)
}

View File

@@ -154,40 +154,3 @@ func maybeCreateApp(appName string) error {
return createApp(appName)
})
}
func filterApps(apps []string) ([]string, error) {
if !common.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, _ := common.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
}

View File

@@ -27,8 +27,6 @@ func main() {
case "app-exists":
appName := flag.Arg(0)
err = apps.TriggerAppExists(appName)
case "app-list":
err = apps.TriggerAppList()
case "app-maybe-create":
appName := flag.Arg(0)
err = apps.TriggerAppMaybeCreate(appName)

View File

@@ -85,7 +85,7 @@ func CommandExists(appName string) error {
// CommandList lists all apps
func CommandList() error {
common.LogInfo2Quiet("My Apps")
apps, err := DokkuApps()
apps, err := common.DokkuApps()
if err != nil {
common.LogWarn(err.Error())
return nil
@@ -177,7 +177,7 @@ func CommandRename(oldAppName string, newAppName string, skipDeploy bool) error
// CommandReport displays an app report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 {
apps, err := DokkuApps()
apps, err := common.DokkuApps()
if err != nil {
return err
}

View File

@@ -21,16 +21,6 @@ func TriggerAppExists(appName string) error {
return appExists(appName)
}
// TriggerAppList outputs each app name to stdout on a newline
func TriggerAppList() error {
apps, _ := DokkuApps()
for _, app := range apps {
common.Log(app)
}
return nil
}
// TriggerAppMaybeCreate is a trigger to allow gated app creation
func TriggerAppMaybeCreate(appName string) error {
return maybeCreateApp(appName)