mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 02:15:43 +03:00
Added an impl for stating a file
This commit is contained in:
@@ -23,9 +23,9 @@ func (f *FSListCommand) Synopsis() string {
|
||||
return "Displays list of files of an alloc dir"
|
||||
}
|
||||
|
||||
func (c *FSListCommand) Run(args []string) int {
|
||||
flags := c.Meta.FlagSet("fs-list", FlagSetClient)
|
||||
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
||||
func (f *FSListCommand) Run(args []string) int {
|
||||
flags := f.Meta.FlagSet("fs-list", FlagSetClient)
|
||||
flags.Usage = func() { f.Ui.Output(f.Help()) }
|
||||
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
@@ -33,7 +33,7 @@ func (c *FSListCommand) Run(args []string) int {
|
||||
args = flags.Args()
|
||||
|
||||
if len(args) < 1 {
|
||||
c.Ui.Error("a valid alloc id is essential")
|
||||
f.Ui.Error("a valid alloc id is essential")
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -43,21 +43,21 @@ func (c *FSListCommand) Run(args []string) int {
|
||||
path = args[1]
|
||||
}
|
||||
|
||||
client, err := c.Meta.Client()
|
||||
client, err := f.Meta.Client()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
|
||||
f.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
alloc, _, err := client.Allocations().Info(allocID, nil)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error getting alloc: %v", err))
|
||||
f.Ui.Error(fmt.Sprintf("Error getting alloc: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
files, _, err := client.AllocFS().List(alloc, path, nil)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error listing alloc dir: %v", err))
|
||||
f.Ui.Error(fmt.Sprintf("Error listing alloc dir: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -73,6 +73,6 @@ func (c *FSListCommand) Run(args []string) int {
|
||||
file.Size)
|
||||
}
|
||||
|
||||
c.Ui.Output(formatList(out))
|
||||
f.Ui.Output(formatList(out))
|
||||
return 0
|
||||
}
|
||||
|
||||
75
command/fs_stat.go
Normal file
75
command/fs_stat.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FSStatCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (f *FSStatCommand) Help() string {
|
||||
helpText := `
|
||||
Usage: nomad fs-stat [alloc-id] [path]
|
||||
|
||||
Displays information about a file in an allocation directory at the given path.
|
||||
The path is relative to the allocation directory.
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (f *FSStatCommand) Synopsis() string {
|
||||
return "Stats a file in an allocation directory"
|
||||
}
|
||||
|
||||
func (f *FSStatCommand) Run(args []string) int {
|
||||
flags := f.Meta.FlagSet("fs-list", FlagSetClient)
|
||||
flags.Usage = func() { f.Ui.Output(f.Help()) }
|
||||
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
args = flags.Args()
|
||||
|
||||
if len(args) < 1 {
|
||||
f.Ui.Error("a valid alloc id is essential")
|
||||
return 1
|
||||
}
|
||||
|
||||
allocID := args[0]
|
||||
path := "/"
|
||||
if len(args) == 2 {
|
||||
path = args[1]
|
||||
}
|
||||
|
||||
client, err := f.Meta.Client()
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
alloc, _, err := client.Allocations().Info(allocID, nil)
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error getting alloc: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
file, _, err := client.AllocFS().Stat(alloc, path, nil)
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error stating file: %v:", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
out := make([]string, 2)
|
||||
out[0] = "Name|Size"
|
||||
if file != nil {
|
||||
fn := file.Name
|
||||
if file.IsDir {
|
||||
fn = fmt.Sprintf("%s/", fn)
|
||||
}
|
||||
out[1] = fmt.Sprintf("%s|%d", fn, file.Size)
|
||||
}
|
||||
f.Ui.Output(formatList(out))
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user