github.com/astaxie/beego@v1.12.3/utils/file.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import (
    18  	"bufio"
    19  	"errors"
    20  	"io"
    21  	"os"
    22  	"path/filepath"
    23  	"regexp"
    24  )
    25  
    26  // SelfPath gets compiled executable file absolute path
    27  func SelfPath() string {
    28  	path, _ := filepath.Abs(os.Args[0])
    29  	return path
    30  }
    31  
    32  // SelfDir gets compiled executable file directory
    33  func SelfDir() string {
    34  	return filepath.Dir(SelfPath())
    35  }
    36  
    37  // FileExists reports whether the named file or directory exists.
    38  func FileExists(name string) bool {
    39  	if _, err := os.Stat(name); err != nil {
    40  		if os.IsNotExist(err) {
    41  			return false
    42  		}
    43  	}
    44  	return true
    45  }
    46  
    47  // SearchFile Search a file in paths.
    48  // this is often used in search config file in /etc ~/
    49  func SearchFile(filename string, paths ...string) (fullpath string, err error) {
    50  	for _, path := range paths {
    51  		if fullpath = filepath.Join(path, filename); FileExists(fullpath) {
    52  			return
    53  		}
    54  	}
    55  	err = errors.New(fullpath + " not found in paths")
    56  	return
    57  }
    58  
    59  // GrepFile like command grep -E
    60  // for example: GrepFile(`^hello`, "hello.txt")
    61  // \n is striped while read
    62  func GrepFile(patten string, filename string) (lines []string, err error) {
    63  	re, err := regexp.Compile(patten)
    64  	if err != nil {
    65  		return
    66  	}
    67  
    68  	fd, err := os.Open(filename)
    69  	if err != nil {
    70  		return
    71  	}
    72  	lines = make([]string, 0)
    73  	reader := bufio.NewReader(fd)
    74  	prefix := ""
    75  	var isLongLine bool
    76  	for {
    77  		byteLine, isPrefix, er := reader.ReadLine()
    78  		if er != nil && er != io.EOF {
    79  			return nil, er
    80  		}
    81  		if er == io.EOF {
    82  			break
    83  		}
    84  		line := string(byteLine)
    85  		if isPrefix {
    86  			prefix += line
    87  			continue
    88  		} else {
    89  			isLongLine = true
    90  		}
    91  
    92  		line = prefix + line
    93  		if isLongLine {
    94  			prefix = ""
    95  		}
    96  		if re.MatchString(line) {
    97  			lines = append(lines, line)
    98  		}
    99  	}
   100  	return lines, nil
   101  }