From 31d672035c322500ce8fec70fd2208a6a67b9981 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 9 Feb 2024 09:38:23 +0100 Subject: [PATCH] feat: add file helper functions (#55) --- file/file.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 file/file.go diff --git a/file/file.go b/file/file.go new file mode 100644 index 0000000..0869f81 --- /dev/null +++ b/file/file.go @@ -0,0 +1,39 @@ +package file + +import "os" + +// The MSDN docs appear to say that a normal path that is 248 bytes long will work; +// empirically the path must be less then 248 bytes long. +// See https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation +const maxPathLenght = 248 + +// ReadStringOrFile returns the content of a string or a file if the file exists. +// The returned boolean value indicates whether the specified `input` was a file path or not. +func ReadStringOrFile(input string) (string, bool, error) { + if len(input) >= maxPathLenght { + return input, false, nil + } + + // Check if input is a file path + if _, err := os.Stat(input); err != nil && os.IsNotExist(err) { + // No file found => use input as result + return input, false, nil + } else if err != nil { + return "", false, err + } + + result, err := os.ReadFile(input) + if err != nil { + return "", true, err + } + + return string(result), true, nil +} + +func DeleteDir(path string) error { + if _, err := os.Stat(path); os.IsNotExist(err) { + return nil + } + + return os.Remove(path) +}