Last active
December 17, 2015 17:19
-
-
Save jonforums/5644904 to your computer and use it in GitHub Desktop.
toying with uru's .ruby-version searching impl
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"runtime" | |
"strings" | |
) | |
type checkFunc func(dir string) (string, error) | |
func main() { | |
useRubyVersionFile(rubyVersionChecker) | |
} | |
func useRubyVersionFile(checker checkFunc) { | |
cwd, err := os.Getwd() | |
if err != nil { | |
os.Exit(1) | |
} | |
absCwd, err := filepath.Abs(cwd) | |
if err != nil { | |
os.Exit(1) | |
} | |
var userHome string | |
if runtime.GOOS == `windows` { | |
userHome = os.Getenv(`USERPROFILE`) | |
} else { | |
userHome = os.Getenv(`HOME`) | |
} | |
userHome, err = filepath.Abs(userHome) | |
if err != nil { | |
os.Exit(1) | |
} | |
if userHome == `` { | |
os.Exit(1) | |
} | |
atRoot := false | |
for !atRoot { | |
// XXX scan stdlib to see if anything more robust than string compare | |
if absCwd == userHome { | |
absCwd = filepath.Dir(absCwd) | |
continue | |
} | |
fmt.Printf("---> searching in %s\n", absCwd) | |
checker(absCwd) | |
absCwd = filepath.Dir(absCwd) | |
err = os.Chdir(absCwd) | |
if err != nil { | |
os.Exit(1) | |
} | |
var path string | |
if runtime.GOOS == `windows` { | |
path = strings.Split(absCwd, `:`)[1] | |
} else { | |
path = absCwd | |
} | |
if strings.HasPrefix(path, string(os.PathSeparator)) && | |
strings.HasSuffix(path, string(os.PathSeparator)) { | |
atRoot = true | |
fmt.Printf("---> searching in %s\n", absCwd) | |
checker(absCwd) | |
err = os.Chdir(userHome) | |
if err != nil { | |
os.Exit(1) | |
} | |
fmt.Printf("---> searching in %s\n", userHome) | |
checker(userHome) | |
} | |
} | |
} | |
func rubyVersionChecker(dir string) (version string, err error) { | |
var path string | |
if strings.HasSuffix(dir, string(os.PathSeparator)) { | |
path = fmt.Sprintf("%s.ruby-version", dir) | |
} else { | |
path = fmt.Sprintf("%s%s.ruby-version", dir, string(os.PathSeparator)) | |
} | |
fmt.Printf("---> looking for %s\n", path) | |
return | |
} |
This file contains hidden or 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
C:\Users\Jon\Documents\GoDev\sandbox>go run dirwalkup.go | |
---> searching in C:\Users\Jon\Documents\GoDev\sandbox | |
---> looking for C:\Users\Jon\Documents\GoDev\sandbox\.ruby-version | |
---> searching in C:\Users\Jon\Documents\GoDev | |
---> looking for C:\Users\Jon\Documents\GoDev\.ruby-version | |
---> searching in C:\Users\Jon\Documents | |
---> looking for C:\Users\Jon\Documents\.ruby-version | |
---> searching in C:\Users\Jon <== suppress this check until end? | |
---> looking for C:\Users\Jon\.ruby-version | |
---> searching in C:\Users | |
---> looking for C:\Users\.ruby-version | |
---> searching in C:\ | |
---> looking for C:\.ruby-version | |
---> searching in C:\Users\Jon | |
---> looking for C:\Users\Jon\.ruby-version |
This file contains hidden or 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
C:\Users\Jon\Documents\GoDev\sandbox>go run dirwalkup.go | |
---> searching in C:\Users\Jon\Documents\GoDev\sandbox | |
---> looking for C:\Users\Jon\Documents\GoDev\sandbox\.ruby-version | |
---> searching in C:\Users\Jon\Documents\GoDev | |
---> looking for C:\Users\Jon\Documents\GoDev\.ruby-version | |
---> searching in C:\Users\Jon\Documents | |
---> looking for C:\Users\Jon\Documents\.ruby-version | |
---> searching in C:\Users | |
---> looking for C:\Users\.ruby-version | |
---> searching in C:\ | |
---> looking for C:\.ruby-version | |
---> searching in C:\Users\Jon | |
---> looking for C:\Users\Jon\.ruby-version |
@luislavena I also want to support dir structures like C:\Apps\projects\web\sinatra\trees-for-rent
and E:\web-apps\invoicer
in which the walk up will not pass through HOME
.
I'll update the go code so that only HOME | USERPROFILE
is checked as the final location after a complete walk up to /
has already occurred. See what you think
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jonforums I think it should walk until
HOME
is reached (line 8)