Documentation ¶
Overview ¶
Package fileutil contains utilities for working with the file system.
Package fileutil contains utilities for working with the file system.
Package fileutil contains utilities for working with the file system.
Index ¶
- Constants
- func AppendToFile(fileDirectory string, filename string, content string) (filePath string, err error)
- func BuildPath(root string, elements ...string) string
- func BuildS3Path(root string, elements ...string) string
- func CollectFilesAndRebase(sourceDir, rebaseToDir string) (files []string, err error)
- func CreateFile(name string) (*os.File, error)
- func CreateTempDir(dir, prefix string) (name string, err error)
- func CreateUTF8ByteOrderMark() (result []byte)
- func DeleteDirectory(dirName string) (err error)
- func DeleteFile(filepath string) (err error)
- func Exists(filePath string) bool
- func GetDirectoryNames(srcPath string) (directories []string, err error)
- func GetDirectoryNamesUnsortedOlderThan(srcPath string, olderThan *time.Time) ([]string, error)
- func GetFileMode(path string) (mode os.FileMode)
- func GetFileModificationTime(srcPath string) (modificationTime time.Time, err error)
- func GetFileNames(srcPath string) (files []string, err error)
- func GetFileNamesUnsortedLaterThan(srcPath string, laterThan *time.Time) ([]string, error)
- func Harden(path string) (err error)
- func HardenDataFolder(log log.T) error
- func HardenedWriteFile(filename string, data []byte) (err error)
- func IsDirEmpty(location string) (bool, error)
- func IsDirectory(srcPath string) bool
- func IsFile(srcPath string) bool
- func LocalFileExist(path string) (bool, error)
- func MakeDirs(destinationDir string) (err error)
- func MakeDirsWithExecuteAccess(destinationDir string) (err error)
- func MoveAndRenameFile(srcPath, originalName, dstPath, newName string) (result bool, err error)
- func MoveFile(filename, srcPath, dstPath string) (result bool, err error)
- func MoveFiles(sourceDir, destDir string) error
- func ReadAllText(filePath string) (text string, err error)
- func ReadDir(location string) ([]os.FileInfo, error)
- func RecursivelyHarden(path string) error
- func Uncompress(log log.T, src, dest string) error
- func Unzip(src, dest string) error
- func WriteAllText(filePath string, text string) (err error)
- func WriteIntoFileWithPermissions(absolutePath, content string, perm os.FileMode) (result bool, err error)
- func WriteIntoFileWithPermissionsExtended(absolutePath, content string, perm os.FileMode, byteOrderMark ByteOrderMark) (result bool, err error)
- type ByteOrderMark
- type DiskSpaceInfo
Examples ¶
Constants ¶
const (
RWPermission = 0600
)
Variables ¶
This section is empty.
Functions ¶
func AppendToFile ¶
func AppendToFile(fileDirectory string, filename string, content string) (filePath string, err error)
AppendToFile appends content to file
func BuildS3Path ¶
BuildS3Path joins the root directory path with valid components.
func CollectFilesAndRebase ¶
CollectFilesAndRebase collects all files and directories of the *sourceDir* changing their relative path to be rooted in *rebaseToDir*. E.g: #### sourceDir = "/a/b" filesInSourceDir = ["/a/b/file.txt", "/a/b/c/another_file.txt"] rebaseToDir = "/d"
outputFiles = ["/d/file.txt", "/d/c/another_file.txt"] ####
func CreateFile ¶
CreateFile creates a file with the given name
func CreateTempDir ¶
CreateTempDir creates a new temporary directory in the directory dir with a name beginning with prefix
func CreateUTF8ByteOrderMark ¶
func CreateUTF8ByteOrderMark() (result []byte)
func DeleteDirectory ¶
DeleteDirectory deletes a directory and all its content.
func DeleteFile ¶
DeleteFile deletes the specified file
func Exists ¶
Exists returns true if the given file exists, false otherwise, ignoring any underlying error
Example ¶
path := filepath.Join("artifact", "testdata", "CheckMyHash.txt") content := Exists(path) fmt.Println(content)
Output: true
func GetDirectoryNames ¶
GetDirectoryNames returns the names of all directories under a give srcPath
func GetDirectoryNamesUnsortedOlderThan ¶
GetDirectoryNamesUnsortedOlderThan returns directory name unsorted ioutil.ReadDir uses sorting to sort the directory names if date value passed in olderThan param, this function returns all files older than the passed time.
func GetFileMode ¶
func GetFileModificationTime ¶
GetFileModificationTime returns the modification time of the file
func GetFileNames ¶
GetFileNames returns the names of all non-directories under a give srcPath
func GetFileNamesUnsortedLaterThan ¶
GetFileNamesUnsortedLaterThan returns directory name unsorted ioutil.ReadDir uses sorting to sort the directory names if date value passed in laterThan param, this function returns all files after the passed time.
func HardenDataFolder ¶
HardenDataFolder sets permission of %PROGRAM_DATA% folder for Windows. In Linux, each components handles the permission of its data.
func HardenedWriteFile ¶
HardenedWriteFile calls ioutil.WriteFile and guarantees a hardened permission control. If the file already exists, it hardens the permissions before writing data to it.
func IsDirEmpty ¶
IsDirEmpty returns true if the given directory is empty else it returns false
Example ¶
path := filepath.Join("artifact", "testdata") content, err := IsDirEmpty(path) if err != nil { log.Fatalf("error: %v", err) } fmt.Println(content)
Output: false
func IsDirectory ¶
IsDirectory returns true or false depending if given srcPath is directory or not
Example ¶
path := filepath.Join("artifact", "testdata", "CheckMyHash.txt") content := IsDirectory(path) fmt.Println(content)
Output: false
func IsFile ¶
IsFile returns true or false depending if given srcPath is a regular file or not
Example ¶
path := filepath.Join("artifact", "testdata", "CheckMyHash.txt") content := IsFile(path) fmt.Println(content)
Output: true
func LocalFileExist ¶
LocalFileExist returns true if the given file exists, false otherwise.
Example ¶
// file exists path := filepath.Join("artifact", "testdata", "CheckMyHash.txt") content, err := LocalFileExist(path) if err != nil { log.Fatalf("error: %v", err) } fmt.Println(content) // file does not exist path = filepath.Join("testdata", "blah") content, err = LocalFileExist(path) if err != nil { log.Fatalf("error: %v", err) } fmt.Println(content)
Output: true false
func MakeDirsWithExecuteAccess ¶
MakeDirsWithExecuteAccess create the directories along the path if missing.
func MoveAndRenameFile ¶
MoveAndRenameFile moves a file from the srcPath directory to dstPath directory and gives it a new name
func MoveFile ¶
MoveFile moves file from srcPath directory to dstPath directory only if both directories exist
func ReadAllText ¶
ReadAllText reads all content from the specified file
Example ¶
// valid file path := filepath.Join("artifact", "testdata", "CheckMyHash.txt") content, err := ReadAllText(path) if err != nil { log.Fatalf("error: %v", err) } fmt.Println(content) // invalid file path = filepath.Join("testdata", "invalid.txt") content, err = ReadAllText(path) if err != nil { log.Fatalf("error: %v", err) } fmt.Println(content)
Output: Will you please check my hash? Will you please check my hash? Will you please check my hash?
func RecursivelyHarden ¶
RecursivelyHarden the files and directory under the specified path.
func Uncompress ¶
Uncompress untar the installation package
func Unzip ¶
Unzip unzips the installation package (using platform agnostic zip functionality) For platform specific implementation that uses tar.gz on Linux, use Uncompress
func WriteAllText ¶
WriteAllText writes all text content to the specified file
func WriteIntoFileWithPermissions ¶
func WriteIntoFileWithPermissions(absolutePath, content string, perm os.FileMode) (result bool, err error)
WriteIntoFileWithPermissions writes into file with given file mode permissions
func WriteIntoFileWithPermissionsExtended ¶
func WriteIntoFileWithPermissionsExtended(absolutePath, content string, perm os.FileMode, byteOrderMark ByteOrderMark) (result bool, err error)
WriteIntoFileWithPermissionsExtended writes into file with given file mode permissions
Types ¶
type ByteOrderMark ¶
type ByteOrderMark uint8
const ( ByteOrderMarkEmit ByteOrderMark = iota ByteOrderMarkSkip ByteOrderMark = iota )
type DiskSpaceInfo ¶
DiskSpaceInfo stores the available, free, and total bytes
func GetDiskSpaceInfo ¶
func GetDiskSpaceInfo() (diskSpaceInfo DiskSpaceInfo, err error)
GetDiskSpaceInfo returns DiskSpaceInfo with available, free, and total bytes from system disk space
Directories ¶
Path | Synopsis |
---|---|
Package artifact contains utilities for working downloading files.
|
Package artifact contains utilities for working downloading files. |
TODO: This package is a start to migration of the fileutil code to be inside an interface for better mocking.
|
TODO: This package is a start to migration of the fileutil code to be inside an interface for better mocking. |
mock
Package fileutil_mock has mock functions for filemanager package
|
Package fileutil_mock has mock functions for filemanager package |
mocks
|
|