Skip to content

Commit

Permalink
buildutils: add Touch helper
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Feb 21, 2024
1 parent 3a4e1ff commit 58cf83e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions buildutils/buildutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/DavidGamba/dgtools/run"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 58cf83e

Please sign in to comment.