Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Commit 4882130

Browse files
author
Radu M
committed
Refactor main command package
Signed-off-by: Radu M <root@radu.sh>
1 parent ff921e4 commit 4882130

File tree

11 files changed

+308
-231
lines changed

11 files changed

+308
-231
lines changed

cmd/duffle/bundle_list.go

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,24 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
6-
"path/filepath"
7-
"sort"
8-
"strings"
95

10-
"github.com/gosuri/uitable"
116
"github.com/spf13/cobra"
127

8+
"github.com/cnabio/duffle/pkg/duffle"
139
"github.com/cnabio/duffle/pkg/duffle/home"
14-
"github.com/cnabio/duffle/pkg/repo"
1510
)
1611

17-
// NamedRepositoryList is a list of bundle references.
18-
// Implements a sorter on Name.
19-
type NamedRepositoryList []*NamedRepository
20-
21-
// Len returns the length.
22-
func (bl NamedRepositoryList) Len() int { return len(bl) }
23-
24-
// Swap swaps the position of two items in the versions slice.
25-
func (bl NamedRepositoryList) Swap(i, j int) { bl[i], bl[j] = bl[j], bl[i] }
26-
27-
// Less returns true if the version of entry a is less than the version of entry b.
28-
func (bl NamedRepositoryList) Less(a, b int) bool {
29-
return strings.Compare(bl[a].Name(), bl[b].Name()) < 1
30-
}
31-
32-
// NamedRepository is a reference to a repository.
33-
type NamedRepository struct {
34-
name string
35-
tag string
36-
digest string
37-
}
38-
39-
// Name returns the full name.
40-
func (n *NamedRepository) String() string {
41-
return n.name + ":" + n.tag
42-
}
43-
44-
// Name returns the name.
45-
func (n *NamedRepository) Name() string {
46-
return n.name
47-
}
48-
49-
// Tag returns the tag.
50-
func (n *NamedRepository) Tag() string {
51-
return n.tag
52-
}
53-
54-
// Digest returns the digest.
55-
func (n *NamedRepository) Digest() string {
56-
return n.digest
57-
}
58-
5912
func newBundleListCmd(w io.Writer) *cobra.Command {
6013
var short bool
6114
cmd := &cobra.Command{
6215
Use: "list",
6316
Aliases: []string{"ls"},
6417
Short: "list bundles pulled or built and stored locally",
6518
RunE: func(cmd *cobra.Command, args []string) error {
66-
home := home.Home(homePath())
67-
references, err := searchLocal(home)
68-
if err != nil {
69-
return err
70-
}
71-
sort.Sort(references)
72-
if short {
73-
for _, ref := range references {
74-
fmt.Println(ref.Name())
75-
}
76-
return nil
77-
}
78-
79-
table := uitable.New()
80-
table.AddRow("NAME", "VERSION", "DIGEST")
81-
for _, ref := range references {
82-
table.AddRow(ref.Name(), ref.Tag(), ref.Digest())
83-
}
84-
fmt.Fprintln(w, table)
85-
86-
return nil
19+
return duffle.List(w, home.Home(homePath()), short)
8720
},
8821
}
8922
cmd.Flags().BoolVarP(&short, "short", "s", false, "output shorter listing format")
90-
9123
return cmd
9224
}
93-
94-
func searchLocal(home home.Home) (NamedRepositoryList, error) {
95-
references := NamedRepositoryList{}
96-
97-
index, err := repo.LoadIndex(home.Repositories())
98-
if err != nil {
99-
return nil, fmt.Errorf("cannot open %s: %v", home.Repositories(), err)
100-
}
101-
102-
for repo, tagList := range index {
103-
for tag, digest := range tagList {
104-
_, err := loadBundle(filepath.Join(home.Bundles(), digest))
105-
if err != nil {
106-
return nil, err
107-
}
108-
references = append(references, &NamedRepository{
109-
repo,
110-
tag,
111-
digest,
112-
})
113-
}
114-
}
115-
116-
return references, nil
117-
}

cmd/duffle/bundle_remove.go

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
6-
"os"
7-
"path/filepath"
85

6+
"github.com/cnabio/duffle/pkg/duffle"
97
"github.com/cnabio/duffle/pkg/duffle/home"
10-
"github.com/cnabio/duffle/pkg/repo"
118

12-
"github.com/Masterminds/semver"
139
"github.com/spf13/cobra"
1410
)
1511

@@ -43,6 +39,7 @@ func newBundleRemoveCmd(w io.Writer) *cobra.Command {
4339
RunE: func(cmd *cobra.Command, args []string) error {
4440
remove.bundleRef = args[0]
4541
remove.home = home.Home(homePath())
42+
4643
return remove.run()
4744
},
4845
}
@@ -52,69 +49,5 @@ func newBundleRemoveCmd(w io.Writer) *cobra.Command {
5249
}
5350

5451
func (rm *bundleRemoveCmd) run() error {
55-
index, err := repo.LoadIndex(rm.home.Repositories())
56-
if err != nil {
57-
return err
58-
}
59-
60-
vers, ok := index.GetVersions(rm.bundleRef)
61-
if !ok {
62-
fmt.Fprintf(rm.out, "Bundle %q not found. Nothing deleted.", rm.bundleRef)
63-
return nil
64-
}
65-
66-
// If versions is set, we short circuit and only delete specific versions.
67-
if rm.versions != "" {
68-
fmt.Fprintln(rm.out, "Only deleting versions")
69-
matcher, err := semver.NewConstraint(rm.versions)
70-
if err != nil {
71-
return err
72-
}
73-
deletions := []repo.BundleVersion{}
74-
for _, ver := range vers {
75-
if ok, _ := matcher.Validate(ver.Version); ok {
76-
fmt.Fprintf(rm.out, "Version %s matches constraint %q\n", ver, rm.versions)
77-
deletions = append(deletions, ver)
78-
index.DeleteVersion(rm.bundleRef, ver.Version.String())
79-
// If there are no more versions, remove the entire entry.
80-
if vers, ok := index.GetVersions(rm.bundleRef); ok && len(vers) == 0 {
81-
index.Delete(rm.bundleRef)
82-
}
83-
84-
}
85-
}
86-
87-
if len(deletions) == 0 {
88-
return nil
89-
}
90-
if err := index.WriteFile(rm.home.Repositories(), 0644); err != nil {
91-
return err
92-
}
93-
deleteBundleVersions(deletions, index, rm.home, rm.out)
94-
return nil
95-
}
96-
97-
// If no version was specified, delete entire record
98-
if !index.Delete(rm.bundleRef) {
99-
fmt.Fprintf(rm.out, "Bundle %q not found. Nothing deleted.", rm.bundleRef)
100-
return nil
101-
}
102-
if err := index.WriteFile(rm.home.Repositories(), 0644); err != nil {
103-
return err
104-
}
105-
106-
deleteBundleVersions(vers, index, rm.home, rm.out)
107-
return nil
108-
}
109-
110-
// deleteBundleVersions removes the given SHAs from bundle storage
111-
//
112-
// It warns, but does not fail, if a given SHA is not found.
113-
func deleteBundleVersions(vers []repo.BundleVersion, index repo.Index, h home.Home, w io.Writer) {
114-
for _, ver := range vers {
115-
fpath := filepath.Join(h.Bundles(), ver.Digest)
116-
if err := os.Remove(fpath); err != nil {
117-
fmt.Fprintf(w, "WARNING: could not delete stake record %q", fpath)
118-
}
119-
}
52+
return duffle.Remove(rm.out, rm.bundleRef, rm.home, rm.versions)
12053
}

cmd/duffle/bundle_show.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"io"
66

7-
"github.com/docker/go/canonical/json"
7+
"github.com/cnabio/duffle/pkg/duffle"
8+
"github.com/cnabio/duffle/pkg/duffle/home"
9+
810
"github.com/spf13/cobra"
911
)
1012

@@ -27,8 +29,7 @@ func newBundleShowCmd(w io.Writer) *cobra.Command {
2729
Args: cobra.ExactArgs(1),
2830
RunE: func(cmd *cobra.Command, args []string) error {
2931
bsc.name = args[0]
30-
31-
return bsc.run()
32+
return duffle.Show(bsc.w, home.Home(homePath()), bsc.name, bsc.raw)
3233
},
3334
}
3435

@@ -51,28 +52,3 @@ func (bsc *bundleShowCmd) usage(bundleSubCommand bool) string {
5152
5253
`, commandName)
5354
}
54-
55-
func (bsc *bundleShowCmd) run() error {
56-
bundleFile, err := getBundleFilepath(bsc.name, homePath())
57-
if err != nil {
58-
return err
59-
}
60-
61-
bun, err := loadBundle(bundleFile)
62-
if err != nil {
63-
return err
64-
}
65-
66-
if bsc.raw {
67-
_, err = bun.WriteTo(bsc.w)
68-
return err
69-
}
70-
71-
d, err := json.MarshalIndent(bun, " ", " ")
72-
if err != nil {
73-
return err
74-
}
75-
_, err = bsc.w.Write(d)
76-
77-
return err
78-
}

cmd/duffle/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ func loadRelMapping(relMap string) (string, error) {
177177
return "", nil
178178
}
179179

180+
// TODO
181+
//
182+
// remove from main
180183
func loadBundle(bundleFile string) (*bundle.Bundle, error) {
181184
l := loader.NewLoader()
182185

cmd/duffle/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func newRootCmd(outputRedirect io.Writer) *cobra.Command {
4545
newBuildCmd(outLog),
4646
newBundleCmd(outLog),
4747
newInitCmd(outLog),
48-
newShowCmd(outLog),
4948
newListCmd(outLog),
5049
newRelocateCmd(outLog),
5150
newVersionCmd(outLog),

cmd/duffle/show.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)