github.com/aldelo/common@v1.5.1/helper-io.go (about)

     1  package helper
     2  
     3  /*
     4   * Copyright 2020-2023 Aldelo, LP
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"io/ioutil"
    23  	"os"
    24  	"path"
    25  )
    26  
    27  // FileRead will read all file content of given file in path,
    28  // return as string if successful,
    29  // if failed, error will contain the error reason
    30  func FileRead(path string) (string, error) {
    31  	data, err := ioutil.ReadFile(path)
    32  
    33  	if err != nil {
    34  		return "", err
    35  	}
    36  
    37  	return string(data), nil
    38  }
    39  
    40  // FileReadBytes will read all file content and return slice of byte
    41  func FileReadBytes(path string) ([]byte, error) {
    42  	data, err := ioutil.ReadFile(path)
    43  
    44  	if err != nil {
    45  		return []byte{}, err
    46  	}
    47  
    48  	return data, nil
    49  }
    50  
    51  // FileWrite will write data into file at the given path,
    52  // if successful, no error is returned (nil)
    53  func FileWrite(path string, data string) error {
    54  	err := ioutil.WriteFile(path, []byte(data), 0644)
    55  
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  // FileWriteBytes will write byte data into file at the given path,
    64  // if successful, no error is returned (nil)
    65  func FileWriteBytes(path string, data []byte) error {
    66  	err := ioutil.WriteFile(path, data, 0644)
    67  
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // FileExists checks if input file in path exists
    76  func FileExists(path string) bool {
    77  	if _, err := os.Stat(path); err == nil {
    78  		return true
    79  	} else {
    80  		return false
    81  	}
    82  }
    83  
    84  // CopyFile - File copies a single file from src to dst
    85  func CopyFile(src string, dst string) error {
    86  	var err error
    87  	var srcfd *os.File
    88  	var dstfd *os.File
    89  	var srcinfo os.FileInfo
    90  
    91  	if srcfd, err = os.Open(src); err != nil {
    92  		return err
    93  	}
    94  	defer srcfd.Close()
    95  
    96  	if dstfd, err = os.Create(dst); err != nil {
    97  		return err
    98  	}
    99  	defer dstfd.Close()
   100  
   101  	if _, err = io.Copy(dstfd, srcfd); err != nil {
   102  		return err
   103  	}
   104  	if srcinfo, err = os.Stat(src); err != nil {
   105  		return err
   106  	}
   107  	return os.Chmod(dst, srcinfo.Mode())
   108  }
   109  
   110  // CopyDir - Dir copies a whole directory recursively
   111  func CopyDir(src string, dst string) error {
   112  	var err error
   113  	var fds []os.FileInfo
   114  	var srcinfo os.FileInfo
   115  
   116  	if srcinfo, err = os.Stat(src); err != nil {
   117  		return err
   118  	}
   119  
   120  	if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
   121  		return err
   122  	}
   123  
   124  	if fds, err = ioutil.ReadDir(src); err != nil {
   125  		return err
   126  	}
   127  	for _, fd := range fds {
   128  		srcfp := path.Join(src, fd.Name())
   129  		dstfp := path.Join(dst, fd.Name())
   130  
   131  		if fd.IsDir() {
   132  			if err = CopyDir(srcfp, dstfp); err != nil {
   133  				fmt.Println(err)
   134  			}
   135  		} else {
   136  			if err = CopyFile(srcfp, dstfp); err != nil {
   137  				fmt.Println(err)
   138  			}
   139  		}
   140  	}
   141  	return nil
   142  }