feat: app filtering to commands listing or iterating over apps

This increases the time to list apps from .05s to .2s~.3s on the simplest check, but the increased time is worth it for actually verifyng access to an app (should that need arise).

We'll also want to add the same to app existence checks.
This commit is contained in:
Jose Diaz-Gonzalez
2021-11-27 14:20:47 -05:00
parent 62a3eff5bb
commit 1a052969dc
5 changed files with 97 additions and 1 deletions

View File

@@ -0,0 +1 @@
hook

View File

@@ -46,5 +46,5 @@ func DokkuApps() ([]string, error) {
return apps, fmt.Errorf("You haven't deployed any applications yet")
}
return apps, nil
return filterApps(apps)
}

View File

@@ -154,3 +154,40 @@ 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

@@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/codeskyblue/go-sh"
@@ -128,3 +129,19 @@ func PlugnTriggerSetup(triggerName string, args ...string) *sh.Session {
}
return sh.Command("plugn", shellArgs...)
}
// PlugnTriggerExists returns whether a plugin trigger exists (ignoring the existence of any within the 20_events plugin)
func PlugnTriggerExists(triggerName string) bool {
pluginPath := MustGetEnv("PLUGIN_ENABLED_PATH")
glob := filepath.Join(pluginPath, "*", triggerName)
exists := false
files, _ := filepath.Glob(glob)
for _, file := range files {
plugin := strings.Trim(strings.TrimPrefix(strings.TrimSuffix(file, "/user-auth-app"), pluginPath), "/")
if plugin != "20_events" {
exists = true
break
}
}
return exists
}