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:
1
plugins/20_events/user-auth-app
Symbolic link
1
plugins/20_events/user-auth-app
Symbolic link
@@ -0,0 +1 @@
|
||||
hook
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user