forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1 of 6] getter.Detect(...): extract function handleDetected(...)
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
Showing
2 changed files
with
69 additions
and
36 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
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 |
---|---|---|
@@ -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 | ||
} |