add other utils
Showing
3 changed files
with
123 additions
and
0 deletions
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bufio" | ||
| 5 | "errors" | ||
| 6 | "io" | ||
| 4 | "os" | 7 | "os" |
| 5 | "path/filepath" | 8 | "path/filepath" |
| 9 | "regexp" | ||
| 6 | ) | 10 | ) |
| 7 | 11 | ||
| 8 | func SelfPath() string { | 12 | func SelfPath() string { |
| 9 | path, _ := filepath.Abs(os.Args[0]) | 13 | path, _ := filepath.Abs(os.Args[0]) |
| 10 | return path | 14 | return path |
| 11 | } | 15 | } |
| 16 | |||
| 17 | func SelfDir() string { | ||
| 18 | return filepath.Dir(SelfPath()) | ||
| 19 | } | ||
| 20 | |||
| 21 | // FileExists reports whether the named file or directory exists. | ||
| 22 | func FileExists(name string) bool { | ||
| 23 | if _, err := os.Stat(name); err != nil { | ||
| 24 | if os.IsNotExist(err) { | ||
| 25 | return false | ||
| 26 | } | ||
| 27 | } | ||
| 28 | return true | ||
| 29 | } | ||
| 30 | |||
| 31 | // search a file in paths. | ||
| 32 | // this is offen used in search config file in /etc ~/ | ||
| 33 | func LookFile(filename string, paths ...string) (fullpath string, err error) { | ||
| 34 | for _, path := range paths { | ||
| 35 | if fullpath = filepath.Join(path, filename); FileExists(fullpath) { | ||
| 36 | return | ||
| 37 | } | ||
| 38 | } | ||
| 39 | err = errors.New(fullpath + " not found in paths") | ||
| 40 | return | ||
| 41 | } | ||
| 42 | |||
| 43 | // like command grep -E | ||
| 44 | // for example: GrepE(`^hello`, "hello.txt") | ||
| 45 | // \n is striped while read | ||
| 46 | func GrepE(patten string, filename string) (lines []string, err error) { | ||
| 47 | re, err := regexp.Compile(patten) | ||
| 48 | if err != nil { | ||
| 49 | return | ||
| 50 | } | ||
| 51 | |||
| 52 | fd, err := os.Open(filename) | ||
| 53 | if err != nil { | ||
| 54 | return | ||
| 55 | } | ||
| 56 | lines = make([]string, 0) | ||
| 57 | reader := bufio.NewReader(fd) | ||
| 58 | prefix := "" | ||
| 59 | for { | ||
| 60 | byteLine, isPrefix, er := reader.ReadLine() | ||
| 61 | if er != nil && er != io.EOF { | ||
| 62 | return nil, er | ||
| 63 | } | ||
| 64 | line := string(byteLine) | ||
| 65 | if isPrefix { | ||
| 66 | prefix += line | ||
| 67 | continue | ||
| 68 | } | ||
| 69 | |||
| 70 | line = prefix + line | ||
| 71 | if re.MatchString(line) { | ||
| 72 | lines = append(lines, line) | ||
| 73 | } | ||
| 74 | if er == io.EOF { | ||
| 75 | break | ||
| 76 | } | ||
| 77 | } | ||
| 78 | return lines, nil | ||
| 79 | } | ... | ... |
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "path/filepath" | ||
| 5 | "reflect" | ||
| 4 | "testing" | 6 | "testing" |
| 5 | ) | 7 | ) |
| 6 | 8 | ||
| 9 | var noExistedFile = "/tmp/not_existed_file" | ||
| 10 | |||
| 7 | func TestSelfPath(t *testing.T) { | 11 | func TestSelfPath(t *testing.T) { |
| 8 | path := SelfPath() | 12 | path := SelfPath() |
| 9 | if path == "" { | 13 | if path == "" { |
| ... | @@ -11,3 +15,47 @@ func TestSelfPath(t *testing.T) { | ... | @@ -11,3 +15,47 @@ func TestSelfPath(t *testing.T) { |
| 11 | } | 15 | } |
| 12 | t.Logf("SelfPath: %s", path) | 16 | t.Logf("SelfPath: %s", path) |
| 13 | } | 17 | } |
| 18 | |||
| 19 | func TestSelfDir(t *testing.T) { | ||
| 20 | dir := SelfDir() | ||
| 21 | t.Logf("SelfDir: %s", dir) | ||
| 22 | } | ||
| 23 | |||
| 24 | func TestFileExists(t *testing.T) { | ||
| 25 | if !FileExists("/bin/echo") { | ||
| 26 | t.Errorf("/bin/echo should exists, but it didn't") | ||
| 27 | } | ||
| 28 | |||
| 29 | if FileExists(noExistedFile) { | ||
| 30 | t.Errorf("Wierd, how could this file exists: %s", noExistedFile) | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | func TestLookFile(t *testing.T) { | ||
| 35 | path, err := LookFile(filepath.Base(SelfPath()), SelfDir()) | ||
| 36 | if err != nil { | ||
| 37 | t.Error(err) | ||
| 38 | } | ||
| 39 | t.Log(path) | ||
| 40 | |||
| 41 | path, err = LookFile(noExistedFile, ".") | ||
| 42 | if err == nil { | ||
| 43 | t.Errorf("err shouldnot be nil, got path: %s", SelfDir()) | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | func TestGrepE(t *testing.T) { | ||
| 48 | _, err := GrepE("", noExistedFile) | ||
| 49 | if err == nil { | ||
| 50 | t.Error("expect file-not-existed error, but got nothing") | ||
| 51 | } | ||
| 52 | |||
| 53 | path := filepath.Join(".", "testdata", "grepe.test") | ||
| 54 | lines, err := GrepE(`^\s*[^#]+`, path) | ||
| 55 | if err != nil { | ||
| 56 | t.Error(err) | ||
| 57 | } | ||
| 58 | if !reflect.DeepEqual(lines, []string{"hello", "world"}) { | ||
| 59 | t.Errorf("expect [hello world], but receive %v", lines) | ||
| 60 | } | ||
| 61 | } | ... | ... |
-
Please register or sign in to post a comment