diff --git a/plugins/app-json/subcommands.go b/plugins/app-json/subcommands.go index 30ef7c3d4..b2f396281 100644 --- a/plugins/app-json/subcommands.go +++ b/plugins/app-json/subcommands.go @@ -1,14 +1,13 @@ package appjson import ( - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) // CommandReport displays a network report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/apps/Makefile b/plugins/apps/Makefile index b24c25864..8a92e94b9 100644 --- a/plugins/apps/Makefile +++ b/plugins/apps/Makefile @@ -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 diff --git a/plugins/apps/apps.go b/plugins/apps/apps.go index d125c3a6e..3369609ca 100644 --- a/plugins/apps/apps.go +++ b/plugins/apps/apps.go @@ -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) -} diff --git a/plugins/apps/functions.go b/plugins/apps/functions.go index 7d1221a30..d19b8b370 100644 --- a/plugins/apps/functions.go +++ b/plugins/apps/functions.go @@ -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 -} diff --git a/plugins/apps/src/triggers/triggers.go b/plugins/apps/src/triggers/triggers.go index 9679ab9e4..9a0ce9d5c 100644 --- a/plugins/apps/src/triggers/triggers.go +++ b/plugins/apps/src/triggers/triggers.go @@ -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) diff --git a/plugins/apps/subcommands.go b/plugins/apps/subcommands.go index 2911808de..bac59eb94 100644 --- a/plugins/apps/subcommands.go +++ b/plugins/apps/subcommands.go @@ -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 } diff --git a/plugins/apps/triggers.go b/plugins/apps/triggers.go index 0a1017946..b70076c64 100644 --- a/plugins/apps/triggers.go +++ b/plugins/apps/triggers.go @@ -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) diff --git a/plugins/builder/subcommands.go b/plugins/builder/subcommands.go index 513c8ebc5..9278f8493 100644 --- a/plugins/builder/subcommands.go +++ b/plugins/builder/subcommands.go @@ -1,14 +1,13 @@ package builder import ( - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) // CommandReport displays a builder report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/buildpacks/subcommands.go b/plugins/buildpacks/subcommands.go index 525e790ef..9a49ffabc 100644 --- a/plugins/buildpacks/subcommands.go +++ b/plugins/buildpacks/subcommands.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) @@ -107,7 +106,7 @@ func CommandRemove(appName string, buildpack string, index int) (err error) { // CommandReport displays a buildpacks report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } @@ -153,7 +152,7 @@ func CommandSetProperty(appName string, property string, value string) error { return common.PlugnTrigger("post-stack-set", []string{appName, value}...) } - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/common/Makefile b/plugins/common/Makefile index e3823d2da..c0852a223 100644 --- a/plugins/common/Makefile +++ b/plugins/common/Makefile @@ -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 diff --git a/plugins/common/common.go b/plugins/common/common.go index bd6516ae5..c4c0cf506 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -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 diff --git a/plugins/common/functions.go b/plugins/common/functions.go new file mode 100644 index 000000000..ea475737b --- /dev/null +++ b/plugins/common/functions.go @@ -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 +} diff --git a/plugins/common/src/triggers/triggers.go b/plugins/common/src/triggers/triggers.go index cefabbe28..8ba0d3866 100644 --- a/plugins/common/src/triggers/triggers.go +++ b/plugins/common/src/triggers/triggers.go @@ -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) diff --git a/plugins/common/triggers.go b/plugins/common/triggers.go index b20c655b3..9bb6ef840 100644 --- a/plugins/common/triggers.go +++ b/plugins/common/triggers.go @@ -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") diff --git a/plugins/cron/functions.go b/plugins/cron/functions.go index 18cb1b1da..ebdd7fb61 100644 --- a/plugins/cron/functions.go +++ b/plugins/cron/functions.go @@ -11,7 +11,6 @@ import ( "text/template" appjson "github.com/dokku/dokku/plugins/app-json" - apps "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" cronparser "github.com/robfig/cron/v3" @@ -100,7 +99,7 @@ func deleteCrontab() error { } func writeCronEntries() error { - apps, _ := apps.DokkuApps() + apps, _ := common.DokkuApps() commands := []templateCommand{} for _, appName := range apps { scheduler := common.GetAppScheduler(appName) diff --git a/plugins/cron/subcommands.go b/plugins/cron/subcommands.go index 6038e1564..88bca6231 100644 --- a/plugins/cron/subcommands.go +++ b/plugins/cron/subcommands.go @@ -3,7 +3,6 @@ package cron import ( "fmt" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" "github.com/ryanuber/columnize" ) @@ -32,7 +31,7 @@ func CommandList(appName string) error { // CommandReport displays a cron report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/logs/functions.go b/plugins/logs/functions.go index 91d6eea99..50aa6a2ca 100644 --- a/plugins/logs/functions.go +++ b/plugins/logs/functions.go @@ -10,7 +10,6 @@ import ( "strings" "time" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" "github.com/joncalhoun/qson" ) @@ -157,7 +156,7 @@ func sinkValueToConfig(appName string, sinkValue string) (vectorSink, error) { } func writeVectorConfig() error { - apps, _ := apps.DokkuApps() + apps, _ := common.DokkuApps() data := vectorConfig{ Sources: map[string]vectorSource{}, Sinks: map[string]vectorSink{}, diff --git a/plugins/logs/subcommands.go b/plugins/logs/subcommands.go index 277213fe7..365a67892 100644 --- a/plugins/logs/subcommands.go +++ b/plugins/logs/subcommands.go @@ -7,7 +7,6 @@ import ( "strconv" "time" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) @@ -53,7 +52,7 @@ func CommandFailed(appName string, allApps bool) error { // CommandReport displays a logs report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/network/subcommands.go b/plugins/network/subcommands.go index f052281e8..bf04f5784 100644 --- a/plugins/network/subcommands.go +++ b/plugins/network/subcommands.go @@ -7,7 +7,6 @@ import ( "os" "strings" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) @@ -108,7 +107,7 @@ func CommandList() error { // CommandRebuildall rebuilds network settings for all apps func CommandRebuildall() error { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } @@ -126,7 +125,7 @@ func CommandRebuildall() error { // CommandReport displays a network report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/network/triggers.go b/plugins/network/triggers.go index 71840ea9e..2906c1428 100644 --- a/plugins/network/triggers.go +++ b/plugins/network/triggers.go @@ -7,7 +7,6 @@ import ( "strings" "unicode/utf8" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config" ) @@ -34,7 +33,7 @@ func TriggerInstall() error { return fmt.Errorf("Unable to install the network plugin: %s", err.Error()) } - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return nil } diff --git a/plugins/proxy/subcommands.go b/plugins/proxy/subcommands.go index 836c9cf53..1dcb66558 100644 --- a/plugins/proxy/subcommands.go +++ b/plugins/proxy/subcommands.go @@ -4,7 +4,6 @@ import ( "errors" "strings" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config" ) @@ -153,7 +152,7 @@ func CommandPortsSet(appName string, portMaps []string) error { // CommandReport displays a proxy report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/ps/subcommands.go b/plugins/ps/subcommands.go index 16c2f5ff6..2d00b87b4 100644 --- a/plugins/ps/subcommands.go +++ b/plugins/ps/subcommands.go @@ -6,7 +6,6 @@ import ( "path/filepath" "strings" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" dockeroptions "github.com/dokku/dokku/plugins/docker-options" "github.com/gofrs/flock" @@ -38,7 +37,7 @@ func CommandRebuild(appName string, allApps bool, parallelCount int) error { // CommandReport displays a ps report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/ps/triggers.go b/plugins/ps/triggers.go index fbd87868b..88c590ef0 100644 --- a/plugins/ps/triggers.go +++ b/plugins/ps/triggers.go @@ -10,7 +10,6 @@ import ( "strings" sh "github.com/codeskyblue/go-sh" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config" dockeroptions "github.com/dokku/dokku/plugins/docker-options" @@ -75,7 +74,7 @@ func TriggerInstall() error { return err } - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return nil } diff --git a/plugins/registry/subcommands.go b/plugins/registry/subcommands.go index ba27a1e0a..beabc002a 100644 --- a/plugins/registry/subcommands.go +++ b/plugins/registry/subcommands.go @@ -7,7 +7,6 @@ import ( "os" "strings" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) @@ -56,7 +55,7 @@ func CommandLogin(server string, username string, password string, passwordStdin // CommandReport displays a registry report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/resource/subcommands.go b/plugins/resource/subcommands.go index 43402b572..f13217389 100644 --- a/plugins/resource/subcommands.go +++ b/plugins/resource/subcommands.go @@ -1,7 +1,6 @@ package resource import ( - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) @@ -27,7 +26,7 @@ func CommandLimitClear(appName string, processType string) error { // CommandReport displays a resource report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/scheduler/subcommands.go b/plugins/scheduler/subcommands.go index 4cded4dcb..98ed46e44 100644 --- a/plugins/scheduler/subcommands.go +++ b/plugins/scheduler/subcommands.go @@ -1,14 +1,13 @@ package scheduler import ( - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" ) // CommandReport displays a scheduler report for one or more apps func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return err } diff --git a/plugins/scheduler/triggers.go b/plugins/scheduler/triggers.go index f3ed30531..d5ae16b1d 100644 --- a/plugins/scheduler/triggers.go +++ b/plugins/scheduler/triggers.go @@ -3,7 +3,6 @@ package scheduler import ( "fmt" - "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config" ) @@ -32,7 +31,7 @@ func TriggerInstall() error { return fmt.Errorf("Unable to install the scheduler plugin: %s", err.Error()) } - apps, err := apps.DokkuApps() + apps, err := common.DokkuApps() if err != nil { return nil }