This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Radu M <root@radu.sh>
- Loading branch information
Radu M
committed
Feb 24, 2020
1 parent
ff921e4
commit 4882130
Showing
11 changed files
with
308 additions
and
231 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/gosuri/uitable" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/cnabio/duffle/pkg/duffle" | ||
"github.com/cnabio/duffle/pkg/duffle/home" | ||
"github.com/cnabio/duffle/pkg/repo" | ||
) | ||
|
||
// NamedRepositoryList is a list of bundle references. | ||
// Implements a sorter on Name. | ||
type NamedRepositoryList []*NamedRepository | ||
|
||
// Len returns the length. | ||
func (bl NamedRepositoryList) Len() int { return len(bl) } | ||
|
||
// Swap swaps the position of two items in the versions slice. | ||
func (bl NamedRepositoryList) Swap(i, j int) { bl[i], bl[j] = bl[j], bl[i] } | ||
|
||
// Less returns true if the version of entry a is less than the version of entry b. | ||
func (bl NamedRepositoryList) Less(a, b int) bool { | ||
return strings.Compare(bl[a].Name(), bl[b].Name()) < 1 | ||
} | ||
|
||
// NamedRepository is a reference to a repository. | ||
type NamedRepository struct { | ||
name string | ||
tag string | ||
digest string | ||
} | ||
|
||
// Name returns the full name. | ||
func (n *NamedRepository) String() string { | ||
return n.name + ":" + n.tag | ||
} | ||
|
||
// Name returns the name. | ||
func (n *NamedRepository) Name() string { | ||
return n.name | ||
} | ||
|
||
// Tag returns the tag. | ||
func (n *NamedRepository) Tag() string { | ||
return n.tag | ||
} | ||
|
||
// Digest returns the digest. | ||
func (n *NamedRepository) Digest() string { | ||
return n.digest | ||
} | ||
|
||
func newBundleListCmd(w io.Writer) *cobra.Command { | ||
var short bool | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "list bundles pulled or built and stored locally", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
home := home.Home(homePath()) | ||
references, err := searchLocal(home) | ||
if err != nil { | ||
return err | ||
} | ||
sort.Sort(references) | ||
if short { | ||
for _, ref := range references { | ||
fmt.Println(ref.Name()) | ||
} | ||
return nil | ||
} | ||
|
||
table := uitable.New() | ||
table.AddRow("NAME", "VERSION", "DIGEST") | ||
for _, ref := range references { | ||
table.AddRow(ref.Name(), ref.Tag(), ref.Digest()) | ||
} | ||
fmt.Fprintln(w, table) | ||
|
||
return nil | ||
return duffle.List(w, home.Home(homePath()), short) | ||
}, | ||
} | ||
cmd.Flags().BoolVarP(&short, "short", "s", false, "output shorter listing format") | ||
|
||
return cmd | ||
} | ||
|
||
func searchLocal(home home.Home) (NamedRepositoryList, error) { | ||
references := NamedRepositoryList{} | ||
|
||
index, err := repo.LoadIndex(home.Repositories()) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot open %s: %v", home.Repositories(), err) | ||
} | ||
|
||
for repo, tagList := range index { | ||
for tag, digest := range tagList { | ||
_, err := loadBundle(filepath.Join(home.Bundles(), digest)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
references = append(references, &NamedRepository{ | ||
repo, | ||
tag, | ||
digest, | ||
}) | ||
} | ||
} | ||
|
||
return references, nil | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.