Add Touch file helper

This commit is contained in:
Ettore Di Giacinto 2020-04-13 19:28:52 +02:00
parent 32cf132a0c
commit eb56956c65
No known key found for this signature in database
GPG Key ID: 1ADA699B145A2D1C

View File

@ -19,6 +19,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"
copy "github.com/otiai10/copy"
)
@ -39,6 +40,25 @@ func ListDir(dir string) ([]string, error) {
return content, err
}
// Touch creates an empty file
func Touch(f string) error {
_, err := os.Stat(f)
if os.IsNotExist(err) {
file, err := os.Create(f)
if err != nil {
return err
}
defer file.Close()
} else {
currentTime := time.Now().Local()
err = os.Chtimes(f, currentTime, currentTime)
if err != nil {
return err
}
}
return nil
}
// Exists reports whether the named file or directory exists.
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {