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 committed 2022-02-26 02:55:34 -05:00
1 parent 90eb5462f7
commit ac58b502c5
27 files changed
+83 -128

No files matched your search

+1 -2
View File
@@ -1,14 +1,13 @@
package appjson package appjson
import ( import (
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// CommandReport displays a network report for one or more apps // CommandReport displays a network report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -1
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 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 BUILD = commands subcommands triggers
PLUGIN_NAME = apps PLUGIN_NAME = apps
-35
View File
@@ -1,13 +1,5 @@
package apps package apps
import (
"fmt"
"io/ioutil"
"strings"
"github.com/dokku/dokku/plugins/common"
)
var ( var (
// DefaultProperties is a map of all valid network properties with corresponding default property values // DefaultProperties is a map of all valid network properties with corresponding default property values
DefaultProperties = map[string]string{ DefaultProperties = map[string]string{
@@ -21,30 +13,3 @@ var (
"deploy-source-metadata": true, "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)
}
-37
View File
@@ -154,40 +154,3 @@ func maybeCreateApp(appName string) error {
return createApp(appName) 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
}
-2
View File
@@ -27,8 +27,6 @@ func main() {
case "app-exists": case "app-exists":
appName := flag.Arg(0) appName := flag.Arg(0)
err = apps.TriggerAppExists(appName) err = apps.TriggerAppExists(appName)
case "app-list":
err = apps.TriggerAppList()
case "app-maybe-create": case "app-maybe-create":
appName := flag.Arg(0) appName := flag.Arg(0)
err = apps.TriggerAppMaybeCreate(appName) err = apps.TriggerAppMaybeCreate(appName)
+2 -2
View File
@@ -85,7 +85,7 @@ func CommandExists(appName string) error {
// CommandList lists all apps // CommandList lists all apps
func CommandList() error { func CommandList() error {
common.LogInfo2Quiet("My Apps") common.LogInfo2Quiet("My Apps")
apps, err := DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
common.LogWarn(err.Error()) common.LogWarn(err.Error())
return nil 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 // CommandReport displays an app report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
-10
View File
@@ -21,16 +21,6 @@ func TriggerAppExists(appName string) error {
return appExists(appName) 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 // TriggerAppMaybeCreate is a trigger to allow gated app creation
func TriggerAppMaybeCreate(appName string) error { func TriggerAppMaybeCreate(appName string) error {
return maybeCreateApp(appName) return maybeCreateApp(appName)
+1 -2
View File
@@ -1,14 +1,13 @@
package builder package builder
import ( import (
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// CommandReport displays a builder report for one or more apps // CommandReport displays a builder report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+2 -3
View File
@@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "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 // CommandReport displays a buildpacks report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
@@ -153,7 +152,7 @@ func CommandSetProperty(appName string, property string, value string) error {
return common.PlugnTrigger("post-stack-set", []string{appName, value}...) return common.PlugnTrigger("post-stack-set", []string{appName, value}...)
} }
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -1
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 BUILD = prop common triggers
PLUGIN_NAME = common PLUGIN_NAME = common
+5 -6
View File
@@ -244,12 +244,12 @@ func GetRunningImageTag(appName string, imageTag string) (string, error) {
} }
// DokkuApps returns a list of all local apps // DokkuApps returns a list of all local apps
func DokkuApps() (apps []string, err error) { func DokkuApps() ([]string, error) {
apps := []string{}
dokkuRoot := MustGetEnv("DOKKU_ROOT") dokkuRoot := MustGetEnv("DOKKU_ROOT")
files, err := ioutil.ReadDir(dokkuRoot) files, err := ioutil.ReadDir(dokkuRoot)
if err != nil { if err != nil {
err = fmt.Errorf("You haven't deployed any applications yet") return apps, fmt.Errorf("You haven't deployed any applications yet")
return
} }
for _, f := range files { for _, f := range files {
@@ -264,11 +264,10 @@ func DokkuApps() (apps []string, err error) {
} }
if len(apps) == 0 { if len(apps) == 0 {
err = fmt.Errorf("You haven't deployed any applications yet") return apps, fmt.Errorf("You haven't deployed any applications yet")
return
} }
return return filterApps(apps)
} }
// GetAppImageName returns image identifier for a given app, tag tuple. validate if tag is presented // GetAppImageName returns image identifier for a given app, tag tuple. validate if tag is presented
+44
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
}
+2
View File
@@ -17,6 +17,8 @@ func main() {
var err error var err error
switch trigger { switch trigger {
case "app-list":
err = common.TriggerAppList()
case "core-post-deploy": case "core-post-deploy":
appName := flag.Arg(0) appName := flag.Arg(0)
err = common.TriggerCorePostDeploy(appName) err = common.TriggerCorePostDeploy(appName)
+10
View File
@@ -5,6 +5,16 @@ import (
"os" "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 // TriggerCorePostDeploy associates the container with a specified network
func TriggerCorePostDeploy(appName string) error { func TriggerCorePostDeploy(appName string) error {
quiet := os.Getenv("DOKKU_QUIET_OUTPUT") quiet := os.Getenv("DOKKU_QUIET_OUTPUT")
+1 -2
View File
@@ -11,7 +11,6 @@ import (
"text/template" "text/template"
appjson "github.com/dokku/dokku/plugins/app-json" appjson "github.com/dokku/dokku/plugins/app-json"
apps "github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
cronparser "github.com/robfig/cron/v3" cronparser "github.com/robfig/cron/v3"
@@ -100,7 +99,7 @@ func deleteCrontab() error {
} }
func writeCronEntries() error { func writeCronEntries() error {
apps, _ := apps.DokkuApps() apps, _ := common.DokkuApps()
commands := []templateCommand{} commands := []templateCommand{}
for _, appName := range apps { for _, appName := range apps {
scheduler := common.GetAppScheduler(appName) scheduler := common.GetAppScheduler(appName)
+1 -2
View File
@@ -3,7 +3,6 @@ package cron
import ( import (
"fmt" "fmt"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/ryanuber/columnize" "github.com/ryanuber/columnize"
) )
@@ -32,7 +31,7 @@ func CommandList(appName string) error {
// CommandReport displays a cron report for one or more apps // CommandReport displays a cron report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -2
View File
@@ -10,7 +10,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/joncalhoun/qson" "github.com/joncalhoun/qson"
) )
@@ -157,7 +156,7 @@ func sinkValueToConfig(appName string, sinkValue string) (vectorSink, error) {
} }
func writeVectorConfig() error { func writeVectorConfig() error {
apps, _ := apps.DokkuApps() apps, _ := common.DokkuApps()
data := vectorConfig{ data := vectorConfig{
Sources: map[string]vectorSource{}, Sources: map[string]vectorSource{},
Sinks: map[string]vectorSink{}, Sinks: map[string]vectorSink{},
+1 -2
View File
@@ -7,7 +7,6 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "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 // CommandReport displays a logs report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+2 -3
View File
@@ -7,7 +7,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
@@ -108,7 +107,7 @@ func CommandList() error {
// CommandRebuildall rebuilds network settings for all apps // CommandRebuildall rebuilds network settings for all apps
func CommandRebuildall() error { func CommandRebuildall() error {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
@@ -126,7 +125,7 @@ func CommandRebuildall() error {
// CommandReport displays a network report for one or more apps // CommandReport displays a network report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -2
View File
@@ -7,7 +7,6 @@ import (
"strings" "strings"
"unicode/utf8" "unicode/utf8"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config" "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()) return fmt.Errorf("Unable to install the network plugin: %s", err.Error())
} }
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return nil return nil
} }
+1 -2
View File
@@ -4,7 +4,6 @@ import (
"errors" "errors"
"strings" "strings"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config" "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 // CommandReport displays a proxy report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -2
View File
@@ -6,7 +6,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
dockeroptions "github.com/dokku/dokku/plugins/docker-options" dockeroptions "github.com/dokku/dokku/plugins/docker-options"
"github.com/gofrs/flock" "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 // CommandReport displays a ps report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -2
View File
@@ -10,7 +10,6 @@ import (
"strings" "strings"
sh "github.com/codeskyblue/go-sh" sh "github.com/codeskyblue/go-sh"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config" "github.com/dokku/dokku/plugins/config"
dockeroptions "github.com/dokku/dokku/plugins/docker-options" dockeroptions "github.com/dokku/dokku/plugins/docker-options"
@@ -75,7 +74,7 @@ func TriggerInstall() error {
return err return err
} }
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return nil return nil
} }
+1 -2
View File
@@ -7,7 +7,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "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 // CommandReport displays a registry report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -2
View File
@@ -1,7 +1,6 @@
package resource package resource
import ( import (
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "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 // CommandReport displays a resource report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -2
View File
@@ -1,14 +1,13 @@
package scheduler package scheduler
import ( import (
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// CommandReport displays a scheduler report for one or more apps // CommandReport displays a scheduler report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error { func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 { if len(appName) == 0 {
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return err return err
} }
+1 -2
View File
@@ -3,7 +3,6 @@ package scheduler
import ( import (
"fmt" "fmt"
"github.com/dokku/dokku/plugins/apps"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config" "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()) return fmt.Errorf("Unable to install the scheduler plugin: %s", err.Error())
} }
apps, err := apps.DokkuApps() apps, err := common.DokkuApps()
if err != nil { if err != nil {
return nil return nil
} }