refactor: move apps listing from common plugin to apps plugin

This helps centralize app-related listing fetching.
This commit is contained in:
Jose Diaz-Gonzalez
2021-11-12 15:54:08 -05:00
parent 9d8de8a9db
commit 62a3eff5bb
45 changed files with 136 additions and 55 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-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-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
BUILD = commands subcommands triggers
PLUGIN_NAME = apps

View File

@@ -1,5 +1,13 @@
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{
@@ -13,3 +21,30 @@ 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 apps, nil
}

View File

@@ -27,6 +27,8 @@ 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 := common.DokkuApps()
apps, err := 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 := common.DokkuApps()
apps, err := DokkuApps()
if err != nil {
return err
}

View File

@@ -21,6 +21,16 @@ 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)