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

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)

View File

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

View File

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

View File

@@ -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

View File

@@ -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

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

View File

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

View File

@@ -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")

View File

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

View File

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

View File

@@ -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{},

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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