-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a utility for container file listing [Updates #2]
- Loading branch information
Showing
4 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
kingpin "gopkg.in/alecthomas/kingpin.v1" | ||
) | ||
|
||
var ( | ||
Version = "0.1.0" | ||
app = kingpin.New("files", "Command for listing a file in JSON") | ||
appPath = app.Flag("path", "path to list").Required().Short('s').String() | ||
) | ||
|
||
type ShortFileInfo struct { | ||
Name string | ||
Size int64 | ||
IsDir bool | ||
IsLink bool | ||
ModTime time.Time | ||
} | ||
|
||
func NewShortFileInfo(f os.FileInfo) *ShortFileInfo { | ||
fi := ShortFileInfo{} | ||
fi.Name = f.Name() | ||
fi.Size = f.Size() | ||
fi.IsDir = f.IsDir() | ||
fi.ModTime = f.ModTime() | ||
fi.IsLink = (f.Mode()&os.ModeType)&os.ModeSymlink > 0 | ||
|
||
return &fi | ||
} | ||
|
||
// | ||
// Go helper utility for container | ||
// to perform file commands. | ||
// | ||
// Will output FileInfo to JSON | ||
// | ||
func main() { | ||
|
||
app.Version(Version) | ||
_, err := app.Parse(os.Args[1:]) | ||
|
||
if err != nil { | ||
app.Usage(os.Stderr) | ||
log.Fatal(err) | ||
os.Exit(1) | ||
} | ||
|
||
files, err := ioutil.ReadDir(*appPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sfiles := make([]*ShortFileInfo, 0) | ||
for _, f := range files { | ||
sfile := NewShortFileInfo(f) | ||
sfiles = append(sfiles, sfile) | ||
} | ||
bytes, err := json.Marshal(sfiles) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
os.Stdout.Write(bytes) | ||
|
||
os.Exit(0) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package codetainer | ||
package codetainer | ||
|
||
import ( | ||
"fmt" | ||
|
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