Skip to content

Instantly share code, notes, and snippets.

@jonforums

jonforums/uru_dirwalkup.go

Last active Dec 17, 2015
Embed
What would you like to do?
toying with uru's .ruby-version searching impl
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
}
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
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

This comment has been minimized.

Copy link

@luislavena luislavena commented May 24, 2013

@jonforums I think it should walk until HOME is reached (line 8)

@jonforums

This comment has been minimized.

Copy link
Owner Author

@jonforums jonforums commented May 24, 2013

@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
You can’t perform that action at this time.