Skip to content

Commit

Permalink
New command "filestore ls-files"
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed May 17, 2016
1 parent dcdc14e commit cc52645
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package commands

import (
"errors"
//"fmt"
"fmt"
"io"
"io/ioutil"

Expand All @@ -22,11 +22,12 @@ var FileStoreCmd = &cmds.Command{
},
Subcommands: map[string]*cmds.Command{
"ls": lsFileStore,
"ls-files": lsFiles,
"verify": verifyFileStore,
"rm": rmFilestoreObjs,
"clean": cleanFileStore,
"fix-pins": repairPins,
"unpinned": fsUnpinned,
"fix-pins": repairPins,
"unpinned": fsUnpinned,
"rm-dups": rmDups,
},
}
Expand Down Expand Up @@ -64,7 +65,7 @@ represents the whole file.
res.SetError(err, cmds.ErrNormal)
return
}
if (quiet) {
if quiet {
ch, _ := fsutil.ListKeys(fs)
res.SetOutput(&chanWriter{ch, "", 0, false})
} else {
Expand All @@ -79,6 +80,39 @@ represents the whole file.
},
}

var lsFiles = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List files in filestore",
ShortDescription: `
Lis files in the filestore. If --quiet is specified only the
file names are printed, otherwise the fields are as follows:
<filepath> <hash> <size>
`,
},
Options: []cmds.Option{
cmds.BoolOption("quiet", "q", "Write just filenames."),
},
Run: func(req cmds.Request, res cmds.Response) {
_, fs, err := extractFilestore(req)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
quiet, _, err := res.Request().Option("quiet").Bool()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
ch, _ := fsutil.ListWholeFile(fs)
res.SetOutput(&chanWriterByFile{ch, "", 0, quiet})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
return res.(io.Reader), nil
},
},
}

type chanWriter struct {
ch <-chan fsutil.ListRes
buf string
Expand All @@ -104,6 +138,32 @@ func (w *chanWriter) Read(p []byte) (int, error) {
return sz, nil
}

type chanWriterByFile struct {
ch <-chan fsutil.ListRes
buf string
offset int
quiet bool
}

func (w *chanWriterByFile) Read(p []byte) (int, error) {
if w.offset >= len(w.buf) {
w.offset = 0
res, more := <-w.ch
if !more {
return 0, io.EOF
}
if w.quiet {
w.buf = fmt.Sprintf("%s\n", res.FilePath)
} else {
w.buf = fmt.Sprintf("%s %s %d\n", res.FilePath, res.MHash(), res.Size)
}
}
sz := copy(p, w.buf[w.offset:])
w.offset += sz
return sz, nil
}


var verifyFileStore = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Verify objects in filestore",
Expand Down

0 comments on commit cc52645

Please sign in to comment.