fileutil

package
v0.0.0-...-a9ed6da Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

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

Examples

Constants

View Source
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 BuildPath

func BuildPath(root string, elements ...string) string

BuildPath joins the orchestration directory path with valid components.

Example
orchestrationDirectory := "/Users/"
// Path with invalid char
name1 := "Fix:ThisPath"
content1 := BuildPath(orchestrationDirectory, name1)
fmt.Println(content1)

// path with no invalid char
name2 := "DoNotFixThisPath"
content2 := BuildPath(orchestrationDirectory, name2)
fmt.Println(content2)

// empty path should not return error
name3 := ""
content3 := BuildPath("", name3)
fmt.Println(content3)

// Path with several invalid chars
name4 := "Fix:ThisPath1"
name5 := "Fix:ThisPath2"
content4 := BuildPath(orchestrationDirectory, name4, name5)
fmt.Println(content4)
Output:

/Users/FixThisPath
/Users/DoNotFixThisPath

/Users/FixThisPath1/FixThisPath2

func BuildS3Path

func BuildS3Path(root string, elements ...string) string

BuildS3Path joins the root directory path with valid components.

func CreateUTF8ByteOrderMark

func CreateUTF8ByteOrderMark() (result []byte)

func DeleteDirectory

func DeleteDirectory(dirName string) (err error)

DeleteDirectory deletes a directory and all its content.

func DeleteFile

func DeleteFile(filepath string) (err error)

DeleteFile deletes the specified file

func Exists

func Exists(filePath string) bool

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

func GetDirectoryNames(srcPath string) (directories []string, err error)

GetDirectoryNames returns the names of all directories under a give srcPath

func GetFileModificationTime

func GetFileModificationTime(srcPath string) (modificationTime time.Time, err error)

GetFileModificationTime returns the modification time of the file

func GetFileNames

func GetFileNames(srcPath string) (files []string, err error)

GetFileNames returns the names of all non-directories under a give srcPath

func Harden

func Harden(path string) (err error)

Harden the provided path with non-inheriting ACL for admin access only.

func HardenDataFolder

func HardenDataFolder() error

HardenDataFolder sets permission of %PROGRAM_DATA% folder for Windows. In Linux, each components handles the permission of its data.

func HardenedWriteFile

func HardenedWriteFile(filename string, data []byte) (err error)

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

func IsDirEmpty(location string) (bool, error)

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

func IsDirectory(srcPath string) bool

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

func IsFile(srcPath string) bool

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

func LocalFileExist(path string) (bool, error)

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 MakeDirs

func MakeDirs(destinationDir string) (err error)

MakeDirs create the directories along the path if missing.

func MakeDirsWithExecuteAccess

func MakeDirsWithExecuteAccess(destinationDir string) (err error)

MakeDirsWithExecuteAccess create the directories along the path if missing.

func MoveAndRenameFile

func MoveAndRenameFile(srcPath, originalName, dstPath, newName string) (result bool, err error)

MoveAndRenameFile moves a file from the srcPath directory to dstPath directory and gives it a new name

func MoveFile

func MoveFile(filename, srcPath, dstPath string) (result bool, err error)

MoveFile moves file from srcPath directory to dstPath directory only if both directories exist

func ReadAllText

func ReadAllText(filePath string) (text string, err error)

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 ReadDir

func ReadDir(location string) ([]os.FileInfo, error)

ReadDir returns files within the given location

func RecursivelyHarden

func RecursivelyHarden(path string) error

RecursivelyHarden the files and directory under the specified path.

func Uncompress

func Uncompress(src, dest string) error

Uncompress untar the installation package

func Unzip

func Unzip(src, dest string) error

Unzip unzips the installation package (using platform agnostic zip functionality) For platform specific implementation that uses tar.gz on Linux, use Uncompress

func WriteAllText

func WriteAllText(filePath string, text string) (err error)

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

type DiskSpaceInfo struct {
	AvailBytes int64
	FreeBytes  int64
	TotalBytes int64
}

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: meloniam@ https://sim.amazon.com/issues/SSM-3322 TODO: This package is a start to migration of the fileutil code to be inside an interface for better mocking.
TODO: meloniam@ https://sim.amazon.com/issues/SSM-3322 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL