Skip to content

Commit

Permalink
refactor(commands): use tab writer (#7241)
Browse files Browse the repository at this point in the history
Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
  • Loading branch information
james-d-elliott committed Apr 27, 2024
1 parent 029050f commit b8264fa
Show file tree
Hide file tree
Showing 3 changed files with 719 additions and 670 deletions.
14 changes: 10 additions & 4 deletions internal/commands/acl.go
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"net"
"net/url"
"os"
"strings"
"text/tabwriter"

"github.com/spf13/cobra"
"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -116,7 +118,9 @@ func accessControlCheckWriteObjectSubject(object authorization.Object, subject a
func accessControlCheckWriteOutput(object authorization.Object, subject authorization.Subject, results []authorization.RuleMatchResult, defaultPolicy string, verbose bool) {
accessControlCheckWriteObjectSubject(object, subject)

fmt.Printf(" #\tDomain\tResource\tMethod\tNetwork\tSubject\n")
w := tabwriter.NewWriter(os.Stdout, 1, 1, 4, ' ', 0)

_, _ = fmt.Fprintln(w, " #\tDomain\tResource\tMethod\tNetwork\tSubject")

var (
appliedPos int
Expand All @@ -135,18 +139,20 @@ func accessControlCheckWriteOutput(object authorization.Object, subject authoriz
case result.IsMatch() && !result.Skipped:
appliedPos, applied = i+1, result

fmt.Printf("* %d\t%s\t%s\t\t%s\t%s\t%s\n", i+1, hitMissMay(result.MatchDomain), hitMissMay(result.MatchResources), hitMissMay(result.MatchMethods), hitMissMay(result.MatchNetworks), hitMissMay(result.MatchSubjects, result.MatchSubjectsExact))
_, _ = fmt.Fprintf(w, "* %d\t%s\t%s\t%s\t%s\t%s\n", i+1, hitMissMay(result.MatchDomain), hitMissMay(result.MatchResources), hitMissMay(result.MatchMethods), hitMissMay(result.MatchNetworks), hitMissMay(result.MatchSubjects, result.MatchSubjectsExact))
case result.IsPotentialMatch() && !result.Skipped:
if potentialPos == 0 {
potentialPos, potential = i+1, result
}

fmt.Printf("~ %d\t%s\t%s\t\t%s\t%s\t%s\n", i+1, hitMissMay(result.MatchDomain), hitMissMay(result.MatchResources), hitMissMay(result.MatchMethods), hitMissMay(result.MatchNetworks), hitMissMay(result.MatchSubjects, result.MatchSubjectsExact))
_, _ = fmt.Fprintf(w, "~ %d\t%s\t%s\t%s\t%s\t%s\n", i+1, hitMissMay(result.MatchDomain), hitMissMay(result.MatchResources), hitMissMay(result.MatchMethods), hitMissMay(result.MatchNetworks), hitMissMay(result.MatchSubjects, result.MatchSubjectsExact))
default:
fmt.Printf(" %d\t%s\t%s\t\t%s\t%s\t%s\n", i+1, hitMissMay(result.MatchDomain), hitMissMay(result.MatchResources), hitMissMay(result.MatchMethods), hitMissMay(result.MatchNetworks), hitMissMay(result.MatchSubjects, result.MatchSubjectsExact))
_, _ = fmt.Fprintf(w, " %d\t%s\t%s\t%s\t%s\t%s\n", i+1, hitMissMay(result.MatchDomain), hitMissMay(result.MatchResources), hitMissMay(result.MatchMethods), hitMissMay(result.MatchNetworks), hitMissMay(result.MatchSubjects, result.MatchSubjectsExact))
}
}

_ = w.Flush()

switch {
case appliedPos != 0 && (potentialPos == 0 || (potentialPos > appliedPos)):
fmt.Printf("\nThe policy '%s' from rule #%d will be applied to this request.\n\n", applied.Rule.Policy, appliedPos)
Expand Down
43 changes: 29 additions & 14 deletions internal/commands/storage_run.go
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"sort"
"strings"
"text/tabwriter"

"github.com/google/uuid"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -266,13 +267,17 @@ func (ctx *CmdCtx) StorageMigrateHistoryRunE(_ *cobra.Command, _ []string) (err
return errors.New("no migration history found which may indicate a broken schema")
}

fmt.Printf("Migration History:\n\nID\tDate\t\t\t\tBefore\tAfter\tAuthelia Version\n")
fmt.Printf("Migration History:\n\n")

w := tabwriter.NewWriter(os.Stdout, 1, 1, 4, ' ', 0)

_, _ = fmt.Fprintln(w, "ID\tDate\tBefore\tAfter\tAuthelia Version")

for _, m := range migrations {
fmt.Printf("%d\t%s\t%d\t%d\t%s\n", m.ID, m.Applied.Format("2006-01-02 15:04:05 -0700"), m.Before, m.After, m.Version)
_, _ = fmt.Fprintf(w, "%d\t%s\t%d\t%d\t%s\n", m.ID, m.Applied.Format("2006-01-02 15:04:05 -0700"), m.Before, m.After, m.Version)
}

return nil
return w.Flush()
}

// NewStorageMigrateListRunE creates the RunE for the authelia storage migrate list command.
Expand Down Expand Up @@ -302,11 +307,17 @@ func (ctx *CmdCtx) NewStorageMigrateListRunE(up bool) func(cmd *cobra.Command, a
if len(migrations) == 0 {
fmt.Printf("Storage Schema Migration List (%s)\n\nNo Migrations Available\n", directionStr)
} else {
fmt.Printf("Storage Schema Migration List (%s)\n\nVersion\t\tDescription\n", directionStr)
fmt.Printf("Storage Schema Migration List (%s)\n\n", directionStr)

w := tabwriter.NewWriter(os.Stdout, 1, 1, 4, ' ', 0)

_, _ = fmt.Fprintln(w, "Version\tDescription")

for _, migration := range migrations {
fmt.Printf("%d\t\t%s\n", migration.Version, migration.Name)
_, _ = fmt.Fprintf(w, "%d\t%s\n", migration.Version, migration.Name)
}

return w.Flush()
}

return nil
Expand Down Expand Up @@ -558,14 +569,17 @@ func (ctx *CmdCtx) StorageUserWebAuthnListRunE(cmd *cobra.Command, args []string
return fmt.Errorf("can't list devices for user '%s': %w", user, err)
default:
fmt.Printf("WebAuthn Credentials for user '%s':\n\n", user)
fmt.Printf("ID\tKID\tDescription\n")

w := tabwriter.NewWriter(os.Stdout, 1, 1, 4, ' ', 0)

_, _ = fmt.Fprintln(w, "ID\tKID\tDescription")

for _, device := range devices {
fmt.Printf("%d\t%s\t%s", device.ID, device.KID, device.Description)
_, _ = fmt.Fprintf(w, "%d\t%s\t%s\n", device.ID, device.KID, device.Description)
}
}

return nil
return w.Flush()
}
}

// StorageUserWebAuthnListAllRunE is the RunE for the authelia storage user webauthn list command when no args are specified.
Expand All @@ -582,7 +596,9 @@ func (ctx *CmdCtx) StorageUserWebAuthnListAllRunE(_ *cobra.Command, _ []string)

limit := 10

output := strings.Builder{}
w := tabwriter.NewWriter(os.Stdout, 1, 1, 4, ' ', 0)

_, _ = fmt.Fprintln(w, "ID\tKID\tDescription\tUsername")

for page := 0; true; page++ {
if devices, err = ctx.providers.StorageProvider.LoadWebAuthnCredentials(ctx, limit, page); err != nil {
Expand All @@ -594,18 +610,17 @@ func (ctx *CmdCtx) StorageUserWebAuthnListAllRunE(_ *cobra.Command, _ []string)
}

for _, device := range devices {
output.WriteString(fmt.Sprintf("%d\t%s\t%s\t%s\n", device.ID, device.KID, device.Description, device.Username))
_, _ = fmt.Fprintf(w, "%d\t%s\t%s\t%s\n", device.ID, device.KID, device.Description, device.Username)
}

if len(devices) < limit {
break
}
}

fmt.Printf("WebAuthn Credentials:\n\nID\tKID\tDescription\tUsername\n")
fmt.Println(output.String())
fmt.Printf("WebAuthn Credentials:\n\n")

return nil
return w.Flush()
}

// StorageUserWebAuthnDeleteRunE is the RunE for the authelia storage user webauthn delete command.
Expand Down

0 comments on commit b8264fa

Please sign in to comment.