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

Refactor main command package #876

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 2 additions & 95 deletions cmd/duffle/bundle_list.go
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
}
73 changes: 3 additions & 70 deletions cmd/duffle/bundle_remove.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/cnabio/duffle/pkg/duffle"
"github.com/cnabio/duffle/pkg/duffle/home"
"github.com/cnabio/duffle/pkg/repo"

"github.com/Masterminds/semver"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -43,6 +39,7 @@ func newBundleRemoveCmd(w io.Writer) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
remove.bundleRef = args[0]
remove.home = home.Home(homePath())

return remove.run()
},
}
Expand All @@ -52,69 +49,5 @@ func newBundleRemoveCmd(w io.Writer) *cobra.Command {
}

func (rm *bundleRemoveCmd) run() error {
index, err := repo.LoadIndex(rm.home.Repositories())
if err != nil {
return err
}

vers, ok := index.GetVersions(rm.bundleRef)
if !ok {
fmt.Fprintf(rm.out, "Bundle %q not found. Nothing deleted.", rm.bundleRef)
return nil
}

// If versions is set, we short circuit and only delete specific versions.
if rm.versions != "" {
fmt.Fprintln(rm.out, "Only deleting versions")
matcher, err := semver.NewConstraint(rm.versions)
if err != nil {
return err
}
deletions := []repo.BundleVersion{}
for _, ver := range vers {
if ok, _ := matcher.Validate(ver.Version); ok {
fmt.Fprintf(rm.out, "Version %s matches constraint %q\n", ver, rm.versions)
deletions = append(deletions, ver)
index.DeleteVersion(rm.bundleRef, ver.Version.String())
// If there are no more versions, remove the entire entry.
if vers, ok := index.GetVersions(rm.bundleRef); ok && len(vers) == 0 {
index.Delete(rm.bundleRef)
}

}
}

if len(deletions) == 0 {
return nil
}
if err := index.WriteFile(rm.home.Repositories(), 0644); err != nil {
return err
}
deleteBundleVersions(deletions, index, rm.home, rm.out)
return nil
}

// If no version was specified, delete entire record
if !index.Delete(rm.bundleRef) {
fmt.Fprintf(rm.out, "Bundle %q not found. Nothing deleted.", rm.bundleRef)
return nil
}
if err := index.WriteFile(rm.home.Repositories(), 0644); err != nil {
return err
}

deleteBundleVersions(vers, index, rm.home, rm.out)
return nil
}

// deleteBundleVersions removes the given SHAs from bundle storage
//
// It warns, but does not fail, if a given SHA is not found.
func deleteBundleVersions(vers []repo.BundleVersion, index repo.Index, h home.Home, w io.Writer) {
for _, ver := range vers {
fpath := filepath.Join(h.Bundles(), ver.Digest)
if err := os.Remove(fpath); err != nil {
fmt.Fprintf(w, "WARNING: could not delete stake record %q", fpath)
}
}
return duffle.Remove(rm.out, rm.bundleRef, rm.home, rm.versions)
}
32 changes: 4 additions & 28 deletions cmd/duffle/bundle_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"io"

"github.com/docker/go/canonical/json"
"github.com/cnabio/duffle/pkg/duffle"
"github.com/cnabio/duffle/pkg/duffle/home"

"github.com/spf13/cobra"
)

Expand All @@ -27,8 +29,7 @@ func newBundleShowCmd(w io.Writer) *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
bsc.name = args[0]

return bsc.run()
return duffle.Show(bsc.w, home.Home(homePath()), bsc.name, bsc.raw)
},
}

Expand All @@ -51,28 +52,3 @@ func (bsc *bundleShowCmd) usage(bundleSubCommand bool) string {
`, commandName)
}

func (bsc *bundleShowCmd) run() error {
bundleFile, err := getBundleFilepath(bsc.name, homePath())
if err != nil {
return err
}

bun, err := loadBundle(bundleFile)
if err != nil {
return err
}

if bsc.raw {
_, err = bun.WriteTo(bsc.w)
return err
}

d, err := json.MarshalIndent(bun, " ", " ")
if err != nil {
return err
}
_, err = bsc.w.Write(d)

return err
}
3 changes: 3 additions & 0 deletions cmd/duffle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func loadRelMapping(relMap string) (string, error) {
return "", nil
}

// TODO
//
// remove from main
func loadBundle(bundleFile string) (*bundle.Bundle, error) {
l := loader.NewLoader()

Expand Down
1 change: 0 additions & 1 deletion cmd/duffle/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func newRootCmd(outputRedirect io.Writer) *cobra.Command {
newBuildCmd(outLog),
newBundleCmd(outLog),
newInitCmd(outLog),
newShowCmd(outLog),
newListCmd(outLog),
newRelocateCmd(outLog),
newVersionCmd(outLog),
Expand Down
29 changes: 0 additions & 29 deletions cmd/duffle/show.go

This file was deleted.

Loading