[server][cmd] improve chperm

This commit is contained in:
Abhishek Shroff
2024-10-18 01:42:29 +05:30
parent 7313ac3b10
commit 7665ab16bf

View File

@@ -2,53 +2,33 @@ package fs
import (
"context"
"errors"
"fmt"
"os"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/core"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func setupChpermCommand() *cobra.Command {
cmd := cobra.Command{
Use: "chperm user <path | uuid> [ none | read | write | share ]",
Short: "Change Resource Permissions",
Use: "chperm <path | uuid> <user> [ none | read | write | share ]",
Short: "Change Permissions",
Args: cobra.ExactArgs(3),
PreRun: func(cmd *cobra.Command, args []string) {
openFileSystemFromFlags(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
var user string
if _, err := core.Default.UserByEmail(context.Background(), args[0]); err != nil {
logrus.Fatal(err)
pathOrUuid := args[0]
r, err := resourceByPathOrUuid(pathOrUuid)
if err != nil {
fmt.Println("cannot update permissions for '" + pathOrUuid + "': " + err.Error())
os.Exit(1)
}
var r core.Resource
path := args[1]
if path[0] != '/' {
var id uuid.UUID
var err error
if id, err = uuid.Parse(path); err != nil {
logrus.Fatal(err)
}
if r, err = fs.ResourceByID(id); err != nil {
if errors.Is(err, core.ErrResourceNotFound) {
logrus.Fatal("Resource not found: " + path)
} else {
logrus.Fatal(err.Error())
}
}
} else {
var err error
if r, err = fs.ResourceByPath(path); err != nil {
if errors.Is(err, core.ErrResourceNotFound) {
logrus.Fatal("Resource not found: " + path)
} else {
logrus.Fatal(err.Error())
}
}
user := args[1]
if _, err := core.Default.UserByEmail(context.Background(), user); err != nil {
fmt.Println("cannot update permissions for user '" + user + "': " + err.Error())
os.Exit(1)
}
permission := core.PermissionNone
@@ -61,11 +41,13 @@ func setupChpermCommand() *cobra.Command {
case "share":
permission = core.PermissionReadWriteShare
default:
logrus.Fatal("Unrecognized premission: " + args[2])
fmt.Println("cannot update permissions for '" + pathOrUuid + "': unrecognized permission: " + args[2])
os.Exit(1)
}
if err := fs.UpdatePermissions(r, user, permission); err != nil {
logrus.Fatal(err)
fmt.Println("cannot update permissions for '" + pathOrUuid + "': " + err.Error())
os.Exit(1)
}
},
}