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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user