github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/chains/tm34/libs/os/os.go (about)

     1  package os
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/signal"
     9  	"syscall"
    10  )
    11  
    12  type logger interface {
    13  	Info(msg string, keyvals ...interface{})
    14  }
    15  
    16  // TrapSignal catches the SIGTERM/SIGINT and executes cb function. After that it exits
    17  // with code 0.
    18  func TrapSignal(logger logger, cb func()) {
    19  	c := make(chan os.Signal, 1)
    20  	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    21  	go func() {
    22  		for sig := range c {
    23  			logger.Info(fmt.Sprintf("captured %v, exiting...", sig))
    24  			if cb != nil {
    25  				cb()
    26  			}
    27  			os.Exit(0)
    28  		}
    29  	}()
    30  }
    31  
    32  // Kill the running process by sending itself SIGTERM.
    33  func Kill() error {
    34  	p, err := os.FindProcess(os.Getpid())
    35  	if err != nil {
    36  		return err
    37  	}
    38  	return p.Signal(syscall.SIGTERM)
    39  }
    40  
    41  func Exit(s string) {
    42  	fmt.Printf(s + "\n")
    43  	os.Exit(1)
    44  }
    45  
    46  // EnsureDir ensures the given directory exists, creating it if necessary.
    47  // Errors if the path already exists as a non-directory.
    48  func EnsureDir(dir string, mode os.FileMode) error {
    49  	err := os.MkdirAll(dir, mode)
    50  	if err != nil {
    51  		return fmt.Errorf("could not create directory %q: %w", dir, err)
    52  	}
    53  	return nil
    54  }
    55  
    56  func FileExists(filePath string) bool {
    57  	_, err := os.Stat(filePath)
    58  	return !os.IsNotExist(err)
    59  }
    60  
    61  func ReadFile(filePath string) ([]byte, error) {
    62  	return ioutil.ReadFile(filePath)
    63  }
    64  
    65  func MustReadFile(filePath string) []byte {
    66  	fileBytes, err := ioutil.ReadFile(filePath)
    67  	if err != nil {
    68  		Exit(fmt.Sprintf("MustReadFile failed: %v", err))
    69  		return nil
    70  	}
    71  	return fileBytes
    72  }
    73  
    74  func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
    75  	return ioutil.WriteFile(filePath, contents, mode)
    76  }
    77  
    78  func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
    79  	err := WriteFile(filePath, contents, mode)
    80  	if err != nil {
    81  		Exit(fmt.Sprintf("MustWriteFile failed: %v", err))
    82  	}
    83  }
    84  
    85  // CopyFile copies a file. It truncates the destination file if it exists.
    86  func CopyFile(src, dst string) error {
    87  	info, err := os.Stat(src)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	srcfile, err := os.Open(src)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	defer srcfile.Close()
    97  
    98  	// create new file, truncate if exists and apply same permissions as the original one
    99  	dstfile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, info.Mode().Perm())
   100  	if err != nil {
   101  		return err
   102  	}
   103  	defer dstfile.Close()
   104  
   105  	_, err = io.Copy(dstfile, srcfile)
   106  	return err
   107  }