Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI commands for public shares #1328

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/public-shares-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Add CLI commands for public shares

https://github.com/cs3org/reva/pull/1328
2 changes: 1 addition & 1 deletion cmd/reva/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *Completer) argumentCompleter(args ...string) []prompt.Suggest {
return prompt.FilterHasPrefix(c.lsArgumentCompleter(false), args[2], true)
}

case "rm", "stat", "share-create", "ocm-share-create", "open-file-in-app-provider", "download":
case "rm", "stat", "share-create", "ocm-share-create", "public-share-create", "open-file-in-app-provider", "download":
if len(args) == 2 {
return prompt.FilterHasPrefix(c.lsArgumentCompleter(false), args[1], true)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/reva/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ var (
ocmShareUpdateReceivedCommand(),
preferencesCommand(),
genCommand(),
publicShareCreateCommand(),
publicShareListCommand(),
publicShareRemoveCommand(),
publicShareUpdateCommand(),
recycleListCommand(),
recycleRestoreCommand(),
recyclePurgeCommand(),
Expand Down
107 changes: 107 additions & 0 deletions cmd/reva/public-share-create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2018-2020 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"io"
"os"
"time"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/jedib0t/go-pretty/table"
"github.com/pkg/errors"
)

func publicShareCreateCommand() *command {
cmd := newCommand("public-share-create")
cmd.Description = func() string { return "create a public share" }
cmd.Usage = func() string { return "Usage: public-share-create [-flags] <path>" }
rol := cmd.String("rol", "viewer", "the permission for the share (viewer or editor)")

cmd.ResetFlags = func() {
*rol = "viewer"
}

cmd.Action = func(w ...io.Writer) error {
if cmd.NArg() < 1 {
return errors.New("Invalid arguments: " + cmd.Usage())
}

fn := cmd.Args()[0]

ctx := getAuthContext()
client, err := getClient()
if err != nil {
return err
}

ref := &provider.Reference{
Spec: &provider.Reference_Path{Path: fn},
}

req := &provider.StatRequest{Ref: ref}
res, err := client.Stat(ctx, req)
if err != nil {
return err
}

if res.Status.Code != rpc.Code_CODE_OK {
return formatError(res.Status)
}

perm, err := getSharePerm(*rol)
if err != nil {
return err
}

grant := &link.Grant{
Permissions: &link.PublicSharePermissions{
Permissions: perm,
},
}
shareRequest := &link.CreatePublicShareRequest{
ResourceInfo: res.Info,
Grant: grant,
}

shareRes, err := client.CreatePublicShare(ctx, shareRequest)
if err != nil {
return err
}

if shareRes.Status.Code != rpc.Code_CODE_OK {
return formatError(shareRes.Status)
}

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Permissions", "Token", "Expiration", "Created", "Updated"})

s := shareRes.Share
t.AppendRows([]table.Row{
{s.Id.OpaqueId, s.Owner.Idp, s.Owner.OpaqueId, s.ResourceId.String(), s.Permissions.String(), s.Token, s.Expiration.String(), time.Unix(int64(s.Ctime.Seconds), 0), time.Unix(int64(s.Mtime.Seconds), 0)},
})
t.Render()

return nil
}
return cmd
}
102 changes: 102 additions & 0 deletions cmd/reva/public-share-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2018-2020 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"encoding/gob"
"fmt"
"io"
"os"
"strings"
"time"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/jedib0t/go-pretty/table"
)

func publicShareListCommand() *command {
cmd := newCommand("public-share-list")
cmd.Description = func() string { return "list public shares created by the user" }
cmd.Usage = func() string { return "Usage: public-share-list [-flags]" }
resID := cmd.String("by-resource-id", "", "filter by resource id (storage_id:opaque_id)")

cmd.ResetFlags = func() {
*resID = ""
}

cmd.Action = func(w ...io.Writer) error {
ctx := getAuthContext()
shareClient, err := getClient()
if err != nil {
return err
}

shareRequest := &link.ListPublicSharesRequest{}
if *resID != "" {
// check split by colon (:)
tokens := strings.Split(*resID, ":")
if len(tokens) != 2 {
return fmt.Errorf("resource id invalid")
}
id := &provider.ResourceId{
StorageId: tokens[0],
OpaqueId: tokens[1],
}
shareRequest.Filters = []*link.ListPublicSharesRequest_Filter{
&link.ListPublicSharesRequest_Filter{
Type: link.ListPublicSharesRequest_Filter_TYPE_RESOURCE_ID,
Term: &link.ListPublicSharesRequest_Filter_ResourceId{
ResourceId: id,
},
},
}
}

shareRes, err := shareClient.ListPublicShares(ctx, shareRequest)
if err != nil {
return err
}

if shareRes.Status.Code != rpc.Code_CODE_OK {
return formatError(shareRes.Status)
}

if len(w) == 0 {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Permissions", "Token", "Expiration", "Created", "Updated"})

for _, s := range shareRes.Share {
t.AppendRows([]table.Row{
{s.Id.OpaqueId, s.Owner.Idp, s.Owner.OpaqueId, s.ResourceId.String(), s.Permissions.String(), s.Token, s.Expiration.String(), time.Unix(int64(s.Ctime.Seconds), 0), time.Unix(int64(s.Mtime.Seconds), 0)},
})
}
t.Render()
} else {
enc := gob.NewEncoder(w[0])
if err := enc.Encode(shareRes.Share); err != nil {
return err
}
}
return nil
}
return cmd
}
70 changes: 70 additions & 0 deletions cmd/reva/public-share-remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2018-2020 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"fmt"
"io"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
"github.com/pkg/errors"
)

func publicShareRemoveCommand() *command {
cmd := newCommand("public-share-remove")
cmd.Description = func() string { return "remove a public share" }
cmd.Usage = func() string { return "Usage: public-share-remove [-flags] <share_id>" }
cmd.Action = func(w ...io.Writer) error {
if cmd.NArg() < 1 {
return errors.New("Invalid arguments: " + cmd.Usage())
}

id := cmd.Args()[0]

ctx := getAuthContext()
shareClient, err := getClient()
if err != nil {
return err
}

shareRequest := &link.RemovePublicShareRequest{
Ref: &link.PublicShareReference{
Spec: &link.PublicShareReference_Id{
Id: &link.PublicShareId{
OpaqueId: id,
},
},
},
}

shareRes, err := shareClient.RemovePublicShare(ctx, shareRequest)
if err != nil {
return err
}

if shareRes.Status.Code != rpc.Code_CODE_OK {
return formatError(shareRes.Status)
}

fmt.Println("OK")
return nil
}
return cmd
}
93 changes: 93 additions & 0 deletions cmd/reva/public-share-update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2018-2020 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"fmt"
"io"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
"github.com/pkg/errors"
)

func publicShareUpdateCommand() *command {
cmd := newCommand("public-share-update")
cmd.Description = func() string { return "update a public share" }
cmd.Usage = func() string { return "Usage: public-share-update [-flags] <share_id>" }
rol := cmd.String("rol", "viewer", "the permission for the share (viewer or editor)")

cmd.ResetFlags = func() {
*rol = "viewer"
}
cmd.Action = func(w ...io.Writer) error {
if cmd.NArg() < 1 {
return errors.New("Invalid arguments: " + cmd.Usage())
}

// validate flags
if *rol != viewerPermission && *rol != editorPermission {
return errors.New("Invalid rol: rol must be viewer or editor\n" + cmd.Usage())
}

id := cmd.Args()[0]

ctx := getAuthContext()
shareClient, err := getClient()
if err != nil {
return err
}

perm, err := getSharePerm(*rol)
if err != nil {
return err
}

shareRequest := &link.UpdatePublicShareRequest{
Ref: &link.PublicShareReference{
Spec: &link.PublicShareReference_Id{
Id: &link.PublicShareId{
OpaqueId: id,
},
},
},
Update: &link.UpdatePublicShareRequest_Update{
Type: link.UpdatePublicShareRequest_Update_TYPE_PERMISSIONS,
Grant: &link.Grant{
Permissions: &link.PublicSharePermissions{
Permissions: perm,
},
},
},
}

shareRes, err := shareClient.UpdatePublicShare(ctx, shareRequest)
if err != nil {
return err
}

if shareRes.Status.Code != rpc.Code_CODE_OK {
return formatError(shareRes.Status)
}

fmt.Println("OK")
return nil
}
return cmd
}
Loading