Skip to content

Commit

Permalink
package describe: added option "-c" to list content of specified package
Browse files Browse the repository at this point in the history
Signed-off-by: Waldemar Kozaczuk <jwkozaczuk@gmail.com>
  • Loading branch information
wkozaczuk committed Oct 24, 2019
1 parent 56241e7 commit d9a51ec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
16 changes: 15 additions & 1 deletion Documentation/generated/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ OPTIONS:
--loader_image value, -l value the base loader image (default: "osv-loader")
```

### capstan package describe
```
NAME:
capstan package describe - describes the package from local repository
USAGE:
capstan package describe [command options] [package-name]
OPTIONS:
--content, -c show file content
```

## Integrating existing packages
Expand Down Expand Up @@ -318,7 +332,7 @@ OPTIONS:
```

---
<sup> Documentation compiled on: 2019/10/23 02:54
<sup> Documentation compiled on: 2019/10/24 03:51
<br>
capstan version
</sup>
5 changes: 4 additions & 1 deletion capstan.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ func main() {
Name: "describe",
Usage: "describes the package from local repository",
ArgsUsage: "[package-name]",
Flags: []cli.Flag{
cli.BoolFlag{Name: "content, c", Usage: "show file content"},
},
Action: func(c *cli.Context) error {
// Name of the package is required argument.
if len(c.Args()) != 1 {
Expand All @@ -620,7 +623,7 @@ func main() {
packageName := c.Args()[0]

// Describe the package
if s, err := cmd.DescribePackage(repo, packageName); err != nil {
if s, err := cmd.DescribePackage(repo, packageName, c.Bool("content")); err != nil {
return cli.NewExitError(err.Error(), EX_DATAERR)
} else {
fmt.Println(s)
Expand Down
20 changes: 18 additions & 2 deletions cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func ensureDirectoryStructureForFile(currfilepath string) error {
}

// DescribePackage describes package with given name without extracting it.
func DescribePackage(repo *util.Repo, packageName string) (string, error) {
func DescribePackage(repo *util.Repo, packageName string, showContent bool) (string, error) {
if !repo.PackageExists(packageName) {
return "", fmt.Errorf("Package %s does not exist in your local repository. Pull it using "+
"'capstan package pull %s'", packageName, packageName)
Expand All @@ -575,6 +575,8 @@ func DescribePackage(repo *util.Repo, packageName string) (string, error) {
var cmdConf *runtime.CmdConfig
var readme string

var content []*tar.Header

for {
header, err := tarReader.Next()
if err != nil {
Expand Down Expand Up @@ -608,10 +610,12 @@ func DescribePackage(repo *util.Repo, packageName string) (string, error) {
return "", err
}
readme = string(data)
} else if showContent && !absTarPathMatches(header.Name, "/meta/") {
content = append(content, header)
}

// Stop reading if we have all the information
if pkg != nil && cmdConf != nil && readme != "" {
if pkg != nil && cmdConf != nil && readme != "" && !showContent {
break
}
}
Expand Down Expand Up @@ -667,6 +671,18 @@ func DescribePackage(repo *util.Repo, packageName string) (string, error) {
s += fmt.Sprintln(readme)
}

if showContent {
s += fmt.Sprintln("PACKAGE CONTENT")
for _, header := range content {
s += fmt.Sprintf("%s %d %d %d %11d %s %s",
header.FileInfo().Mode().String(), 0, 0, 0, header.Size, header.FileInfo().ModTime().Format(time.RFC822), header.Name)
if header.FileInfo().Mode()&os.ModeSymlink == os.ModeSymlink {
s += fmt.Sprintf(" -> %s", header.Linkname)
}
s += fmt.Sprintln("")
}
}

return s, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (s *suite) TestDescribePackage(c *C) {
ImportPackage(s.repo, s.packageDir)

// This is what we're testing here.
descr, err := DescribePackage(s.repo, "package-name")
descr, err := DescribePackage(s.repo, "package-name", false)

// Expectations.
c.Assert(err, IsNil)
Expand Down
3 changes: 2 additions & 1 deletion scripts/generate_cli_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self, title, description, commands):
Command('capstan package init'),
Command('capstan package collect'),
Command('capstan package compose'),
Command('capstan package describe'),
]),
Group('Integrating existing packages',
'These commands are useful when we intend to use package from remote repository.', [
Expand Down Expand Up @@ -147,4 +148,4 @@ def generate_cli_documentation():

print('Generating CLI documentation into %s' % RESULT_FILE)
generate_cli_documentation()
print('CLI documentation dumped into: %s' % RESULT_FILE)
print('CLI documentation dumped into: %s' % RESULT_FILE)

0 comments on commit d9a51ec

Please sign in to comment.