[server][cli] Improve output of ls command

This commit is contained in:
Abhishek Shroff
2025-06-08 23:19:34 +05:30
parent 9144d74d99
commit 3bb1069d9d
4 changed files with 98 additions and 48 deletions
+73 -10
View File
@@ -1,8 +1,10 @@
package common
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/jackc/pgx/v5/pgtype"
@@ -17,21 +19,30 @@ func FormatResourceSummary(r core.Resource, name string, deleted pgtype.Timestam
name += "/"
}
v := r.LatestVersion()
sha256 := v.SHA256
sha256 := ""
sizeStr := " -"
versions := "-"
if r.Dir() {
sha256 = "- "
sha256 = "-- "
} else {
sizeStr = FormatSize(int(v.Size))
if v.SHA256 == "" {
sha256 = "xxxxxxxx"
} else {
sha256 = v.SHA256[0:8]
}
versionCount := parseVersions(r.Versions())
versions = strconv.Itoa(versionCount)
if versionCount > 9 {
versions = "+"
}
}
if sha256 == "" {
sha256 = "xxxxxxxx"
}
delStr := " "
if deleted != r.Deleted() {
delStr = "*"
}
return fmt.Sprintf("%s %s %s %s%-24s %s %s", r.ID().String(), sizeStr, sha256[0:8], delStr, name, formatGrantsJson(r.Grants()), r.Links())
return fmt.Sprintf("%s %d %s %s %s %s%-24s %s", r.ID().String(), len(parseLinks(r.Links())), versions, sizeStr, sha256, delStr, name, FormatGrantsJson(r.Grants()))
}
func FormatSize(size int) string {
@@ -42,20 +53,72 @@ func FormatSize(size int) string {
return fmt.Sprintf("%4d%s", size, suffix[si])
}
func formatGrantsJson(j []byte) string {
p := make(map[string]interface{})
func FormatPermissionsJson(j []byte) string {
p := make(map[string]core.Permission)
json.Unmarshal(j, &p)
if len(p) == 0 {
return ""
}
perm := make([]string, 0, len(p))
for k, v := range p {
perm = append(perm, k+": "+FormatPermission(int32((v.(map[string]interface{})["p"]).(float64))))
email, err := userEmail(k)
if err != nil {
email = "unknown user (" + k + ")"
}
perm = append(perm, email+": "+formatPermission(v))
}
return "[" + strings.Join(perm, ", ") + "]"
}
func FormatPermission(p core.Permission) string {
func FormatGrantsJson(j []byte) string {
g := make(map[string]grant)
json.Unmarshal(j, &g)
if len(g) == 0 {
return ""
}
perm := make([]string, 0, len(g))
for k, v := range g {
email, err := userEmail(k)
if err != nil {
email = "unknown user (" + k + ")"
}
perm = append(perm, email+": "+formatPermission(v.Permission))
}
return "[" + strings.Join(perm, ", ") + "]"
}
type grant struct {
Permission core.Permission `json:"p"`
Timestamp int `json:"t"`
}
func userEmail(s string) (string, error) {
id, err := strconv.Atoi(s)
if err != nil {
return "", err
}
return core.UserManagerFromContext(context.Background()).UserEmailByID(id)
}
func parseLinks(j []byte) []string {
l := make([]map[string]interface{}, 0)
json.Unmarshal(j, &l)
links := make([]string, 0, len(l))
for _, v := range l {
links = append(links, v["id"].(string))
}
return links
}
func parseVersions(j []byte) int {
v := make([]map[string]interface{}, 0)
json.Unmarshal(j, &v)
return len(v)
}
func formatPermission(p core.Permission) string {
if p == core.PermissionSU {
return "su"
}
+13 -38
View File
@@ -1,16 +1,13 @@
package fs
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/shroff/phylum/server/internal/command/common"
"github.com/shroff/phylum/server/internal/core"
"github.com/spf13/cobra"
)
@@ -33,18 +30,27 @@ func setupLsCommand() *cobra.Command {
fmt.Println("cannot get path:" + err.Error())
os.Exit(1)
}
fmt.Println(path)
fmt.Println("ID: " + r.ID().String())
fmt.Println("Path: " + path)
fmt.Println("Created: " + r.Created().Local().Format(time.RFC1123))
fmt.Println("Modified: " + r.Modified().Local().Format(time.RFC1123))
if r.Deleted().Valid {
fmt.Println("Deleted: " + r.Deleted().Time.Local().Format(time.RFC1123))
}
fmt.Println("Inherited Permissions: " + formatPermissionsJson(r.InheritedPermissions()))
fmt.Println("Permission Grants: " + formatGrantsJson(r.Grants()))
fmt.Println()
fmt.Println(common.FormatResourceSummary(r, ".", pgtype.Timestamp{}))
fmt.Println("Inherited Permissions: " + common.FormatPermissionsJson(r.InheritedPermissions()))
grants := common.FormatGrantsJson(r.Grants())
if grants == "" {
grants = "None"
}
fmt.Println("Permission Grants: " + grants)
fmt.Println()
// fmt.Println(common.FormatResourceSummary(r, ".", pgtype.Timestamp{}))
if r.Dir() {
fmt.Println("Children:")
includeDeleted, _ := cmd.Flags().GetBool("include-deleted")
children, err := f.ReadDirDeleted(r, false, includeDeleted)
if err != nil {
@@ -70,34 +76,3 @@ func setupLsCommand() *cobra.Command {
cmd.Flags().BoolP("include-deleted", "x", false, "Include deleted files")
return &cmd
}
func formatPermissionsJson(j []byte) string {
p := make(map[string]core.Permission)
json.Unmarshal(j, &p)
if len(p) == 0 {
return ""
}
perm := make([]string, 0, len(p))
for k, v := range p {
perm = append(perm, k+": "+common.FormatPermission(v))
}
return "[" + strings.Join(perm, ", ") + "]"
}
type Grant struct {
Permission core.Permission `json:"p"`
Timestamp int `json:"t"`
}
func formatGrantsJson(j []byte) string {
g := make(map[string]Grant)
json.Unmarshal(j, &g)
if len(g) == 0 {
return ""
}
perm := make([]string, 0, len(g))
for k, v := range g {
perm = append(perm, k+": "+common.FormatPermission(v.Permission))
}
return "[" + strings.Join(perm, ", ") + "]"
}
+1
View File
@@ -85,6 +85,7 @@ type UserManager interface {
CreateUser(email, name string, noCreateHome bool) (User, error)
ListUsers(since int64) ([]User, error)
UserByEmail(email string) (User, error)
UserEmailByID(id int) (string, error)
UserHome(email string) (pgtype.UUID, error)
// user_auth.go
+11
View File
@@ -76,6 +76,17 @@ func (m manager) ListUsers(since int64) ([]User, error) {
}
}
func (m manager) UserEmailByID(id int) (string, error) {
const q = "SELECT email FROM users WHERE id = $1"
row := m.db.QueryRow(q, id)
var email string
if err := row.Scan(&email); err != nil {
return "", err
} else {
return email, nil
}
}
func (m manager) UserByEmail(email string) (User, error) {
const q = "SELECT id, email, name, home, permissions FROM users WHERE email = $1"
if rows, err := m.db.Query(q, strings.ToLower(email)); err != nil {