From 58cf83eb7ae667f6e24a79ced8a340cb21aade58 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Tue, 20 Feb 2024 22:41:36 -0700 Subject: [PATCH] buildutils: add Touch helper --- buildutils/buildutils.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/buildutils/buildutils.go b/buildutils/buildutils.go index 8abaee7..e64530d 100644 --- a/buildutils/buildutils.go +++ b/buildutils/buildutils.go @@ -19,6 +19,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/DavidGamba/dgtools/run" ) @@ -118,3 +119,21 @@ func FindFileUpwards(ctx context.Context, filename string) (string, error) { return "", fmt.Errorf("%w: %s", ErrNotFound, filename) } + +func Touch(filename string) error { + _, err := os.Stat(filename) + if os.IsNotExist(err) { + file, err := os.Create(filename) + if err != nil { + return err + } + _ = file.Close() + } else { + currentTime := time.Now().Local() + err = os.Chtimes(filename, currentTime, currentTime) + if err != nil { + return err + } + } + return nil +}