1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-16 16:40:56 +00:00
Beego/utils/file.go

102 lines
2.3 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-12-09 05:30:19 +00:00
package utils
import (
2013-12-09 08:01:24 +00:00
"bufio"
"errors"
"io"
2013-12-09 05:30:19 +00:00
"os"
"path/filepath"
2013-12-09 08:01:24 +00:00
"regexp"
2013-12-09 05:30:19 +00:00
)
// SelfPath gets compiled executable file absolute path
2013-12-09 05:30:19 +00:00
func SelfPath() string {
path, _ := filepath.Abs(os.Args[0])
return path
}
2013-12-09 08:01:24 +00:00
// SelfDir gets compiled executable file directory
2013-12-09 08:01:24 +00:00
func SelfDir() string {
return filepath.Dir(SelfPath())
}
// FileExists reports whether the named file or directory exists.
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
2015-09-14 15:35:13 +00:00
// SearchFile Search a file in paths.
// this is often used in search config file in /etc ~/
func SearchFile(filename string, paths ...string) (fullpath string, err error) {
2013-12-09 08:01:24 +00:00
for _, path := range paths {
if fullpath = filepath.Join(path, filename); FileExists(fullpath) {
return
}
}
err = errors.New(fullpath + " not found in paths")
return
}
2015-09-14 15:35:13 +00:00
// GrepFile like command grep -E
// for example: GrepFile(`^hello`, "hello.txt")
2013-12-09 08:01:24 +00:00
// \n is striped while read
2013-12-11 14:18:45 +00:00
func GrepFile(patten string, filename string) (lines []string, err error) {
2013-12-09 08:01:24 +00:00
re, err := regexp.Compile(patten)
if err != nil {
return
}
fd, err := os.Open(filename)
if err != nil {
return
}
lines = make([]string, 0)
reader := bufio.NewReader(fd)
prefix := ""
2017-04-28 14:36:28 +00:00
var isLongLine bool
2013-12-09 08:01:24 +00:00
for {
byteLine, isPrefix, er := reader.ReadLine()
if er != nil && er != io.EOF {
return nil, er
}
2014-05-20 06:32:06 +00:00
if er == io.EOF {
break
}
2013-12-09 08:01:24 +00:00
line := string(byteLine)
if isPrefix {
prefix += line
continue
2014-05-20 06:32:06 +00:00
} else {
isLongLine = true
2013-12-09 08:01:24 +00:00
}
line = prefix + line
2014-05-20 06:32:06 +00:00
if isLongLine {
prefix = ""
}
2013-12-09 08:01:24 +00:00
if re.MatchString(line) {
lines = append(lines, line)
}
}
return lines, nil
}