Skip to content

Commit

Permalink
Add yorkie document ls command (#366)
Browse files Browse the repository at this point in the history
This commit adds document list command.
It can use by `yorkie document ls [ProjectName]`

Co-authored-by: Youngteac Hong <[email protected]>
  • Loading branch information
Eithea and hackerwins authored Jul 25, 2022
1 parent 1d3755e commit 69fb07c
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 13 deletions.
15 changes: 15 additions & 0 deletions admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ func (c *Client) UpdateProject(
return converter.FromProject(response.Project)
}

// ListDocuments lists documents.
func (c *Client) ListDocuments(ctx context.Context, projectName string) ([]*types.DocumentSummary, error) {
response, err := c.client.ListDocuments(
ctx,
&api.ListDocumentsRequest{
ProjectName: projectName,
},
)
if err != nil {
return nil, err
}

return converter.FromDocumentSummaries(response.Documents)
}

// ListChangeSummaries returns the change summaries of the given document.
func (c *Client) ListChangeSummaries(
ctx context.Context,
Expand Down
37 changes: 37 additions & 0 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@ func FromProject(pbProject *api.Project) (*types.Project, error) {
}, nil
}

// FromDocumentSummaries converts the given Protobuf formats to model format.
func FromDocumentSummaries(pbSummaries []*api.DocumentSummary) ([]*types.DocumentSummary, error) {
var summaries []*types.DocumentSummary
for _, pbSummary := range pbSummaries {
summary, err := FromDocumentSummary(pbSummary)
if err != nil {
return nil, err
}
summaries = append(summaries, summary)
}
return summaries, nil
}

// FromDocumentSummary converts the given Protobuf formats to model format.
func FromDocumentSummary(pbSummary *api.DocumentSummary) (*types.DocumentSummary, error) {
createdAt, err := protoTypes.TimestampFromProto(pbSummary.CreatedAt)
if err != nil {
return nil, err
}
accessedAt, err := protoTypes.TimestampFromProto(pbSummary.AccessedAt)
if err != nil {
return nil, err
}
updatedAt, err := protoTypes.TimestampFromProto(pbSummary.UpdatedAt)
if err != nil {
return nil, err
}
return &types.DocumentSummary{
ID: types.ID(pbSummary.Id),
Key: key.Key(pbSummary.Key),
CreatedAt: createdAt,
AccessedAt: accessedAt,
UpdatedAt: updatedAt,
Snapshot: pbSummary.Snapshot,
}, nil
}

// FromClient converts the given Protobuf formats to model format.
func FromClient(pbClient *api.Client) (*types.Client, error) {
id, err := time.ActorIDFromBytes(pbClient.Id)
Expand Down
26 changes: 13 additions & 13 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ func ToProject(project *types.Project) (*api.Project, error) {
}, nil
}

// ToDocumentSummaries converts the given model to Protobuf.
func ToDocumentSummaries(summaries []*types.DocumentSummary) ([]*api.DocumentSummary, error) {
var pbSummaries []*api.DocumentSummary
for _, summary := range summaries {
pbSummary, err := ToDocumentSummary(summary)
if err != nil {
return nil, err
}
pbSummaries = append(pbSummaries, pbSummary)
}
return pbSummaries, nil
}

// ToDocumentSummary converts the given model to Protobuf format.
func ToDocumentSummary(summary *types.DocumentSummary) (*api.DocumentSummary, error) {
pbCreatedAt, err := protoTypes.TimestampProto(summary.CreatedAt)
Expand All @@ -94,19 +107,6 @@ func ToDocumentSummary(summary *types.DocumentSummary) (*api.DocumentSummary, er
}, nil
}

// ToDocumentSummaries converts the given model to Protobuf.
func ToDocumentSummaries(summaries []*types.DocumentSummary) ([]*api.DocumentSummary, error) {
var pbSummaries []*api.DocumentSummary
for _, summary := range summaries {
pbSummary, err := ToDocumentSummary(summary)
if err != nil {
return nil, err
}
pbSummaries = append(pbSummaries, pbSummary)
}
return pbSummaries, nil
}

// ToClient converts the given model to Protobuf format.
func ToClient(client types.Client) *api.Client {
return &api.Client{
Expand Down
2 changes: 2 additions & 0 deletions cmd/yorkie/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/cobra"

"github.com/yorkie-team/yorkie/cmd/yorkie/config"
"github.com/yorkie-team/yorkie/cmd/yorkie/document"
"github.com/yorkie-team/yorkie/cmd/yorkie/project"
)

Expand All @@ -39,6 +40,7 @@ func Run() int {

func init() {
rootCmd.AddCommand(project.SubCmd)
rootCmd.AddCommand(document.SubCmd)
// TODO(chacha912): set adminAddr from env using viper.
// https://github.com/spf13/cobra/blob/main/user_guide.md#bind-flags-with-config
rootCmd.PersistentFlags().StringVar(&config.AdminAddr, "admin-addr", "localhost:11103", "Address of the admin server")
Expand Down
27 changes: 27 additions & 0 deletions cmd/yorkie/document/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 The Yorkie Authors. All rights reserved.
*
* 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.
*/

package document

import "github.com/spf13/cobra"

var (
// SubCmd represents the document command
SubCmd = &cobra.Command{
Use: "document",
Short: "Manage documents",
}
)
88 changes: 88 additions & 0 deletions cmd/yorkie/document/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2022 The Yorkie Authors. All rights reserved.
*
* 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.
*/

package document

import (
"context"
"errors"
"time"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"

"github.com/yorkie-team/yorkie/admin"
"github.com/yorkie-team/yorkie/cmd/yorkie/config"
"github.com/yorkie-team/yorkie/pkg/units"
)

func newListCommand() *cobra.Command {
return &cobra.Command{
Use: "ls [project name]",
Short: "List all documents in the project",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("project is required")
}

projectName := args[0]
cli, err := admin.Dial(config.AdminAddr)
if err != nil {
return err
}
defer func() {
_ = cli.Close()
}()

ctx := context.Background()
documents, err := cli.ListDocuments(ctx, projectName)
if err != nil {
return err
}

tw := table.NewWriter()
tw.Style().Options.DrawBorder = false
tw.Style().Options.SeparateColumns = false
tw.Style().Options.SeparateFooter = false
tw.Style().Options.SeparateHeader = false
tw.Style().Options.SeparateRows = false
tw.AppendHeader(table.Row{
"ID",
"KEY",
"CREATED AT",
"ACCESSED AT",
"UPDATED AT",
"SNAPSHOT",
})
for _, document := range documents {
tw.AppendRow(table.Row{
document.ID,
document.Key,
units.HumanDuration(time.Now().UTC().Sub(document.CreatedAt)),
units.HumanDuration(time.Now().UTC().Sub(document.AccessedAt)),
units.HumanDuration(time.Now().UTC().Sub(document.UpdatedAt)),
document.Snapshot,
})
}
cmd.Printf("%s\n", tw.Render())
return nil
},
}
}

func init() {
SubCmd.AddCommand(newListCommand())
}
16 changes: 16 additions & 0 deletions server/documents/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,28 @@ func ListDocumentSummaries(

var summaries []*types.DocumentSummary
for _, docInfo := range docInfo {
doc, err := packs.BuildDocumentForServerSeq(ctx, be, docInfo, docInfo.ServerSeq)
if err != nil {
return nil, err
}

var snapshot string
fullSnapshot := doc.Marshal()
snapshotCutline := 50

if len(fullSnapshot) < snapshotCutline {
snapshot = fullSnapshot
} else {
snapshot = fullSnapshot[:snapshotCutline] + " ..."
}

summaries = append(summaries, &types.DocumentSummary{
ID: docInfo.ID,
Key: docInfo.Key,
CreatedAt: docInfo.CreatedAt,
AccessedAt: docInfo.AccessedAt,
UpdatedAt: docInfo.UpdatedAt,
Snapshot: snapshot,
})
}

Expand Down

0 comments on commit 69fb07c

Please sign in to comment.