github.com/remind101/go-getter@v0.0.0-20180809191950-4bda8fa99001/source.go (about) 1 package getter 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "strings" 7 ) 8 9 // SourceDirSubdir takes a source and returns a tuple of the URL without 10 // the subdir and the URL with the subdir. 11 func SourceDirSubdir(src string) (string, string) { 12 // Calcaulate an offset to avoid accidentally marking the scheme 13 // as the dir. 14 var offset int 15 if idx := strings.Index(src, "://"); idx > -1 { 16 offset = idx + 3 17 } 18 19 // First see if we even have an explicit subdir 20 idx := strings.Index(src[offset:], "//") 21 if idx == -1 { 22 return src, "" 23 } 24 25 idx += offset 26 subdir := src[idx+2:] 27 src = src[:idx] 28 29 // Next, check if we have query parameters and push them onto the 30 // URL. 31 if idx = strings.Index(subdir, "?"); idx > -1 { 32 query := subdir[idx:] 33 subdir = subdir[:idx] 34 src += query 35 } 36 37 return src, subdir 38 } 39 40 // SubdirGlob returns the actual subdir with globbing processed. 41 // 42 // dst should be a destination directory that is already populated (the 43 // download is complete) and subDir should be the set subDir. If subDir 44 // is an empty string, this returns an empty string. 45 // 46 // The returned path is the full absolute path. 47 func SubdirGlob(dst, subDir string) (string, error) { 48 matches, err := filepath.Glob(filepath.Join(dst, subDir)) 49 if err != nil { 50 return "", err 51 } 52 53 if len(matches) == 0 { 54 return "", fmt.Errorf("subdir %q not found", subDir) 55 } 56 57 if len(matches) > 1 { 58 return "", fmt.Errorf("subdir %q matches multiple paths", subDir) 59 } 60 61 return matches[0], nil 62 }