Skip to content

Commit

Permalink
[1 of 6] getter.Detect(...): extract function handleDetected(...)
Browse files Browse the repository at this point in the history
Once the Detect(...) function finds a detector for a source string it
combines the obtained result string with other data bits to produce the
result returned to the caller. That processing is here extracted into a
new handleDetected(...) function, which will be called from an
additional context in an upcoming commit.

The new handleDetected(...) function lives in a new file:

    detect_common.go

The move emphasizes the function's slightly wider use now by both the
Detect(...) and (soon-to-be-introduced) CtxDetect(...) dispatch
functions. With the introduction of CtxDetect, the logic in
handleDetected is no longer used exclusively by Detect.

This changeset provides an implementation modification, but no
behavioral change.
  • Loading branch information
salewski committed Aug 31, 2020
1 parent fef0a23 commit 2f6c376
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
39 changes: 3 additions & 36 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package getter

import (
"fmt"
"path/filepath"

"github.com/hashicorp/go-getter/helper/url"
)
Expand Down Expand Up @@ -61,41 +60,9 @@ func Detect(src string, pwd string, ds []Detector) (string, error) {
continue
}

var detectForce string
detectForce, result = getForcedGetter(result)
result, detectSubdir := SourceDirSubdir(result)

// If we have a subdir from the detection, then prepend it to our
// requested subdir.
if detectSubdir != "" {
if subDir != "" {
subDir = filepath.Join(detectSubdir, subDir)
} else {
subDir = detectSubdir
}
}

if subDir != "" {
u, err := url.Parse(result)
if err != nil {
return "", fmt.Errorf("Error parsing URL: %s", err)
}
u.Path += "//" + subDir

// a subdir may contain wildcards, but in order to support them we
// have to ensure the path isn't escaped.
u.RawPath = u.Path

result = u.String()
}

// Preserve the forced getter if it exists. We try to use the
// original set force first, followed by any force set by the
// detector.
if getForce != "" {
result = fmt.Sprintf("%s::%s", getForce, result)
} else if detectForce != "" {
result = fmt.Sprintf("%s::%s", detectForce, result)
result, err = handleDetected(result, getForce, subDir)
if err != nil {
return "", err
}

return result, nil
Expand Down
66 changes: 66 additions & 0 deletions detect_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package getter

import (
"fmt"
"path/filepath"

"github.com/hashicorp/go-getter/helper/url"
)

// handleDetected is a helper function for the Detect(...) and CtxDetect(...)
// dispatch functions.
//
// Both dispatch functions work in the same general way:
//
// * Each breaks-apart its input string to extract any provided 'force'
// token and/or extract any '//some/subdir' element before supplying the
// downstream {,Ctx}Detect methods with input to chew on.
//
// * When a given detector indicates that it has processed the input
// string, the dispatch function needs to re-introduce the previously
// extracted bits before returning the reconstituted result string to
// its caller.
//
// Given the originally extracted bits along with the result obtained from the
// detector, this function performs that reconstitution.
//
func handleDetected(detectedResult, srcGetForce, subDir string) (string, error) {
var detectForce string
detectForce, result := getForcedGetter(detectedResult)
result, detectSubdir := SourceDirSubdir(result)

// If we have a subdir from the detection, then prepend it to our
// requested subdir.
if detectSubdir != "" {
if subDir != "" {
subDir = filepath.Join(detectSubdir, subDir)
} else {
subDir = detectSubdir
}
}

if subDir != "" {
u, err := url.Parse(result)
if err != nil {
return "", fmt.Errorf("Error parsing URL: %s", err)
}
u.Path += "//" + subDir

// a subdir may contain wildcards, but in order to support them we
// have to ensure the path isn't escaped.
u.RawPath = u.Path

result = u.String()
}

// Preserve the forced getter if it exists. We try to use the
// original set force first, followed by any force set by the
// detector.
if srcGetForce != "" {
result = fmt.Sprintf("%s::%s", srcGetForce, result)
} else if detectForce != "" {
result = fmt.Sprintf("%s::%s", detectForce, result)
}

return result, nil
}

0 comments on commit 2f6c376

Please sign in to comment.