gopkg.in/essentialkaos/ek.v3@v3.5.1/fsutil/walker.go (about) 1 // +build !windows 2 3 package fsutil 4 5 // ////////////////////////////////////////////////////////////////////////////////// // 6 // // 7 // Copyright (c) 2009-2016 Essential Kaos // 8 // Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> // 9 // // 10 // ////////////////////////////////////////////////////////////////////////////////// // 11 12 import ( 13 "os" 14 ) 15 16 // ////////////////////////////////////////////////////////////////////////////////// // 17 18 var dirStack []string 19 20 // ////////////////////////////////////////////////////////////////////////////////// // 21 22 // Push change current working directory and add previous working directory to stack 23 func Push(dir string) string { 24 if dirStack == nil { 25 dirStack = append(dirStack, Current()) 26 } 27 28 err := os.Chdir(dir) 29 30 if err != nil { 31 return "" 32 } 33 34 wd, _ := os.Getwd() 35 36 dirStack = append(dirStack, wd) 37 38 return wd 39 } 40 41 // Pop change current working directory to previous in stack 42 func Pop() string { 43 if dirStack == nil { 44 dirStack = append(dirStack, Current()) 45 } 46 47 dl := len(dirStack) 48 49 switch dl { 50 51 case 0, 1: 52 // nop 53 54 default: 55 err := os.Chdir(dirStack[dl-2]) 56 57 if err != nil { 58 return "" 59 } 60 61 dirStack = dirStack[0 : dl-1] 62 } 63 64 wd, _ := os.Getwd() 65 66 return wd 67 } 68 69 // Current return current working directory 70 func Current() string { 71 wd, err := os.Getwd() 72 73 if err != nil { 74 return "" 75 } 76 77 return wd 78 }