Skip to content

Commit

Permalink
Fix --cd behavior for globbed resources
Browse files Browse the repository at this point in the history
  • Loading branch information
dpb587 committed Jan 21, 2021
1 parent d6dc63c commit 1bf5653
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
19 changes: 11 additions & 8 deletions cmd/gget/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"

"code.cloudfoundry.org/bytefmt"
Expand Down Expand Up @@ -79,21 +78,23 @@ func (c *Command) applySettings() {

if c.Stdout {
for resourceIdx, resource := range c.Args.Resources {
if resource.LocalPath != "" {
if resource.LocalPath() != "" {
continue
}

c.Args.Resources[resourceIdx].LocalPath = "-"
c.Args.Resources[resourceIdx].LocalName = "-"
}
}

if c.CD != "" {
for resourceIdx, resource := range c.Args.Resources {
if resource.LocalPath == "-" {
if resource.LocalPath() == "-" {
continue
} else if resource.LocalDir != "" {
continue
}

c.Args.Resources[resourceIdx].LocalPath = filepath.Join(c.CD, resource.LocalPath)
c.Args.Resources[resourceIdx].LocalDir = c.CD
}
}

Expand Down Expand Up @@ -187,11 +188,13 @@ func (c *Command) Execute(_ []string) error {
panic("TODO should always match by now?")
}

if _, found := resourceMap[resolved.LocalPath]; found {
return fmt.Errorf("target file already specified: %s", resolved.LocalPath)
localPath := resolved.LocalPath()

if _, found := resourceMap[localPath]; found {
return fmt.Errorf("target file already specified: %s", localPath)
}

resourceMap[resolved.LocalPath] = candidate
resourceMap[localPath] = candidate
}
}

Expand Down
6 changes: 6 additions & 0 deletions docs/releases/v0.5.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: v0.5.3
weight: 5003
---

* fix `--cd` behavior when using resource globs ([#19](https://github.com/dpb587/gget/issues/19))
22 changes: 14 additions & 8 deletions pkg/cli/opt/resource_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import (

type ResourceTransfer struct {
RemoteMatch ResourceMatcher
LocalPath string
LocalDir string
LocalName string
}

type ResourceTransferList []ResourceTransfer

func (o *ResourceTransfer) LocalPath() string {
return filepath.Join(o.LocalDir, o.LocalName)
}

func (o *ResourceTransfer) Resolve(remote string) (ResourceTransfer, bool) {
match := o.RemoteMatch.Match(remote)
if !match {
Expand All @@ -20,29 +25,30 @@ func (o *ResourceTransfer) Resolve(remote string) (ResourceTransfer, bool) {

res := ResourceTransfer{
RemoteMatch: ResourceMatcher(remote),
LocalPath: o.LocalPath,
LocalDir: o.LocalDir,
LocalName: o.LocalName,
}

if res.LocalPath == "" {
res.LocalPath = remote
} else if strings.HasSuffix(res.LocalPath, "/") {
res.LocalPath = filepath.Join(res.LocalPath, string(res.RemoteMatch))
if res.LocalName == "" {
res.LocalName = remote
}

return res, true
}

func (o *ResourceTransfer) UnmarshalFlag(data string) error {
dataSplit := strings.SplitN(data, "=", 2)
localPath := ""

if len(dataSplit) == 2 {
o.RemoteMatch = ResourceMatcher(dataSplit[1])
o.LocalPath = dataSplit[0]
localPath = dataSplit[0]
} else {
o.RemoteMatch = ResourceMatcher(dataSplit[0])
o.LocalPath = ""
}

o.LocalDir, o.LocalName = filepath.Split(localPath)

if err := o.RemoteMatch.Validate(); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/integration.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ rm -fr tmp/integrationtest/workdir
mkdir tmp/integrationtest/workdir
cd tmp/integrationtest/workdir

# dump-info, ref-version constraints, match constraint
# dump-info, ref-version constraints, match constraint, cd

../gget github.com/dpb587/gget --ref-version=0.2.x --export=json '*linux*' > export.json
../gget github.com/dpb587/gget --ref-version=0.2.x --export=json --cd="${PWD}" '*linux*' > export.json

diff <( shasum * ) - <<EOF
f43df59ff5b0ad32362ec72655017d6deed3704f export.json
Expand Down

0 comments on commit 1bf5653

Please sign in to comment.