Skip to content

Commit

Permalink
Add list command and read records from db (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
lylex committed Dec 17, 2018
1 parent 2e5d849 commit 0764148
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 25 deletions.
66 changes: 66 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"fmt"

"github.com/apcera/termtables"
"github.com/lylex/drm/pkg/blobs"
"github.com/lylex/drm/pkg/utils"
"github.com/spf13/cobra"
)

const (
listCmdName = "list"

timeFormat = "2006-01-02 15:04:05"
)

type table struct {
*termtables.Table
}

func createTable() *table {
t := &table{
termtables.CreateTable(),
}

t.Style.SkipBorder = true

t.Style.BorderX = ""
t.Style.BorderY = ""
t.Style.BorderI = ""

return t
}

func (t *table) addHeaders(headers ...interface{}) {
t.AddHeaders(headers...)
}

func (t *table) addRow(items ...interface{}) {
t.AddRow(items...)
}

// listCmd represents the list command.
var listCmd = &cobra.Command{
Use: listCmdName,
Short: "list all the deleted objects",
Long: `list all the deleted objects, and all can be resumed, try "drm resume" to resume an object`,
Run: func(cmd *cobra.Command, args []string) {
table := createTable()
table.addHeaders("Name", "Path", "DeleteAt")

blobs, err := blobs.GetAll()
if err != nil {
utils.ErrExit(fmt.Sprintf("%s %s: failed to retrieve data from database\n", RootCmdName, listCmdName), err)
}
for _, blob := range blobs {
table.addRow(blob.FileName, blob.Dir, blob.CreatedAt.Format(timeFormat))
}
fmt.Println(table.Render())
},
}

func init() {
RootCmd.AddCommand(listCmd)
}
25 changes: 0 additions & 25 deletions cmd/ls.go

This file was deleted.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module github.com/lylex/drm

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.5
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055 h1:IkPAzP+QjchKXXFX6LCcpDKa89b/e/0gPCUbQGWtUUY=
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055/go.mod h1:8mHYHlOef9UC51cK1/WRvE/iQVM8O8QlYFa8eh8r5I8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
Expand All @@ -12,6 +14,8 @@ github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswD
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
Expand Down
31 changes: 31 additions & 0 deletions pkg/blobs/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,34 @@ func (b *Blob) Save() error {

return nil
}

// GetAll returns all the blobs in database.
func GetAll() ([]*Blob, error) {
db, err := buntdb.Open(viper.GetString(CfgDataPathKey) + MetaDataDB)
if err != nil {
return nil, err
}
defer db.Close()

rawValues := make([]string, 0)
err = db.View(func(tx *buntdb.Tx) error {
err := tx.Ascend("", func(key, value string) bool {
rawValues = append(rawValues, value)
return true
})
return err
})
if err != nil {
return nil, err
}

blobs := make([]*Blob, 0)
for _, value := range rawValues {
var obj Blob
if err := utils.Unmarshal(value, &obj); err != nil {
return nil, err
}
blobs = append(blobs, &obj)
}
return blobs, nil
}
3 changes: 3 additions & 0 deletions scripts/install_mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CFG_DIR=/etc/drm
DATA_DIR=/usr/local/lib/drm
BLOB_DIR=$DATA_DIR/blob
BIN_DIR=/usr/local/bin/
BIN_NAME=drm

[ -d $CFG_DIR ] || mkdir $CFG_DIR

Expand All @@ -17,3 +18,5 @@ fi

cp $WORK_DIR/drm.conf $CFG_DIR/drm.conf
cp $WORK_DIR/../drm $BIN_DIR

alias rm='$$BIN_NAME' >> ~/.bashrc

0 comments on commit 0764148

Please sign in to comment.