open-nomad/command/fs_list.go

79 lines
1.5 KiB
Go
Raw Normal View History

2016-01-26 22:31:52 +00:00
package command
import (
"fmt"
"strings"
)
type FSListCommand struct {
Meta
}
func (f *FSListCommand) Help() string {
helpText := `
Usage: nomad fs-list [alloc-id] [path]
Displays the files in the alloc-dir of the given alloc id. The path
is relative to the root of the alloc dir.
`
return strings.TrimSpace(helpText)
}
func (f *FSListCommand) Synopsis() string {
return "Displays list of files of an alloc dir"
}
2016-01-26 23:03:26 +00:00
func (f *FSListCommand) Run(args []string) int {
flags := f.Meta.FlagSet("fs-list", FlagSetClient)
flags.Usage = func() { f.Ui.Output(f.Help()) }
2016-01-26 22:31:52 +00:00
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) < 1 {
2016-01-26 23:03:26 +00:00
f.Ui.Error("a valid alloc id is essential")
2016-01-26 22:31:52 +00:00
return 1
}
allocID := args[0]
path := "/"
if len(args) == 2 {
path = args[1]
}
2016-01-26 23:03:26 +00:00
client, err := f.Meta.Client()
2016-01-26 22:31:52 +00:00
if err != nil {
2016-01-26 23:03:26 +00:00
f.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
2016-01-26 22:31:52 +00:00
return 1
}
alloc, _, err := client.Allocations().Info(allocID, nil)
if err != nil {
2016-01-26 23:03:26 +00:00
f.Ui.Error(fmt.Sprintf("Error getting alloc: %v", err))
2016-01-26 22:31:52 +00:00
return 1
}
files, _, err := client.AllocFS().List(alloc, path, nil)
if err != nil {
2016-01-26 23:03:26 +00:00
f.Ui.Error(fmt.Sprintf("Error listing alloc dir: %v", err))
2016-01-26 22:31:52 +00:00
return 1
}
out := make([]string, len(files)+1)
out[0] = "Name|Size"
for i, file := range files {
fn := file.Name
if file.IsDir {
fn = fmt.Sprintf("%s/", fn)
}
out[i+1] = fmt.Sprintf("%s|%d",
fn,
file.Size)
}
2016-01-26 23:03:26 +00:00
f.Ui.Output(formatList(out))
2016-01-26 22:31:52 +00:00
return 0
}