wisski-cloud-distillery/pkg/fsx/touch.go
Tom Wiesing f19619ef9f
Add 'environment' package
This commit adds a new environment package that manages all calls to the
underlying operating system.
2022-09-18 14:24:22 +02:00

30 lines
699 B
Go

package fsx
import (
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
)
// Touch touches a file.
// It is similar to the unix 'touch' command.
//
// If the file does not exist exists, it is created using [env.Create].
// If the file does exist, it's access and modification times are updated to the current time.
func Touch(env environment.Environment, path string) error {
_, err := env.Stat(path)
switch {
case environment.IsNotExist(err):
f, err := env.Create(path, environment.DefaultFilePerm)
if err != nil {
return err
}
defer f.Close()
return nil
case err != nil:
return err
default:
now := time.Now().Local()
return env.Chtimes(path, now, now)
}
}