github.com/mborho/rem@v0.17.0/file.go (about)

     1  // rem - A tool to remember things on the command line.
     2  // Copyright (C) 2015 Martin Borho (martin@borho.net)
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	_ "fmt"
    21  	"os"
    22  	"os/user"
    23  	"path"
    24  )
    25  
    26  type File struct {
    27  	filepath string
    28  	filename string
    29  	file     *os.File
    30  	global   bool
    31  }
    32  
    33  func (f *File) clearFile() error {
    34  	return os.Remove(f.filepath)
    35  }
    36  
    37  func (f *File) Close() {
    38  	f.file.Close()
    39  }
    40  
    41  func (f *File) setFile(appendTo bool) error {
    42  	// which mode to use to open file
    43  	var openFlags int
    44  	if appendTo {
    45  		openFlags = os.O_CREATE | os.O_APPEND | os.O_WRONLY
    46  	} else {
    47  		openFlags = os.O_CREATE | os.O_RDONLY
    48  	}
    49  
    50  	// open history file
    51  	file, err := os.OpenFile(f.filepath, openFlags, 0600)
    52  	if err == nil {
    53  		f.file = file
    54  	}
    55  	return nil
    56  }
    57  
    58  func (f *File) createLocalFile() error {
    59  	// Create history file in the current directory.
    60  	dir, err := os.Getwd()
    61  	if err != nil {
    62  		return err
    63  	}
    64  	localFile := path.Join(dir, f.filename)
    65  	f.file, err = os.OpenFile(localFile, os.O_CREATE, 0600)
    66  	defer f.file.Close()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	f.filepath = localFile
    72  	return nil
    73  }
    74  
    75  func (f *File) checkPath(dir string) bool {
    76  	// Checks if rem file exists in given directory.
    77  	checkFile := path.Join(dir, f.filename)
    78  	if _, err := os.Stat(checkFile); err == nil {
    79  		return true
    80  	}
    81  	return false
    82  }
    83  
    84  func (f *File) setPath() error {
    85  	// ignore current dir if global .rem file is wanted
    86  	if f.global == false {
    87  		// Set path to history file in current dir if one exists
    88  		dir, err := os.Getwd()
    89  		if err != nil {
    90  			return err
    91  		}
    92  		for {
    93  			// traverse through dir path
    94  			localFile := path.Join(dir, f.filename)
    95  			if f.checkPath(dir) {
    96  				f.filepath = localFile
    97  				return nil
    98  			}
    99  			if dir == "/" {
   100  				break
   101  			}
   102  			dir = path.Dir(dir)
   103  		}
   104  	}
   105  
   106  	// Set default path to rem's history file
   107  	usr, err := user.Current()
   108  	if err == nil {
   109  		f.filepath = path.Join(usr.HomeDir, f.filename)
   110  	}
   111  	return err
   112  }
   113  
   114  func (f *File) write(line string) error {
   115  	if _, err := f.file.WriteString(line); err != nil {
   116  		panic(err)
   117  	}
   118  	return nil
   119  }