-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
yorkie document ls
command (#366)
This commit adds document list command. It can use by `yorkie document ls [ProjectName]` Co-authored-by: Youngteac Hong <[email protected]>
- Loading branch information
1 parent
1d3755e
commit 69fb07c
Showing
7 changed files
with
198 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters