mirror of https://github.com/curusarn/resh
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.0 KiB
55 lines
1.0 KiB
// futil implements common file-related utilities
|
|
package futil
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func CopyFile(source, dest string) error {
|
|
from, err := os.Open(source)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer from.Close()
|
|
|
|
// This is equivalent to: os.OpenFile(dest, os.O_RDWR|os.O_CREATE, 0666)
|
|
to, err := os.Create(dest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer to.Close()
|
|
|
|
_, err = io.Copy(to, from)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FileExists(fpath string) (bool, error) {
|
|
_, err := os.Stat(fpath)
|
|
if err == nil {
|
|
// File exists
|
|
return true, nil
|
|
}
|
|
if os.IsNotExist(err) {
|
|
// File doesn't exist
|
|
return false, nil
|
|
}
|
|
// Any other error
|
|
return false, fmt.Errorf("could not stat file: %w", err)
|
|
}
|
|
|
|
func TouchFile(fpath string) error {
|
|
file, err := os.OpenFile(fpath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
|
if err != nil {
|
|
return fmt.Errorf("could not open/create file: %w", err)
|
|
}
|
|
err = file.Close()
|
|
if err != nil {
|
|
return fmt.Errorf("could not close file: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|