Skip to content

Instantly share code, notes, and snippets.

@exupero
Created July 30, 2015 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exupero/087c771c76bf4a97cf27 to your computer and use it in GitHub Desktop.
Save exupero/087c771c76bf4a97cf27 to your computer and use it in GitHub Desktop.
Find a file in an ancestory directory
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
func check(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func isRoot() bool {
d, err := os.Getwd()
if err != nil {
return true
}
fmt.Println(d)
return d == "/"
}
func main() {
flag.Parse()
d, err := os.Getwd()
if err != nil {
log.Println(err)
return
}
height := len(strings.Split(d, "/")) - 2
path := flag.Arg(0)
if path[0:2] == "./" {
path = path[2:]
}
for i := 0; i <= height; i++ {
if check(path) {
fmt.Println(path)
return
} else {
path = "../" + path
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment