Documentation ¶
Overview ¶
Package writer handles the part of writing output to a file
Use the function writer.Start to initialize the package
Index ¶
- func AddHandler(handler Handler)
- func IsAbsPathErr(err error) bool
- func IsHandlerNotFoundErr(err error) bool
- func IsInvalidExtErr(err error) bool
- func IsInvalidFileErr(err error) bool
- func IsPathDirErr(err error) bool
- func IsPathNotWriteableErr(err error) bool
- func IsReadFileErr(err error) bool
- func Start(confPath string) error
- func Write(info *DirInfo) error
- type Checksums
- type DirInfo
- type FileInfo
- type Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddHandler ¶
func AddHandler(handler Handler)
AddHandler registers a Handler that will be used to handle a particular output file, these handlers are detected based on file extensions
func IsAbsPathErr ¶
IsAbsPathErr checks if an error occurred because a relative path could not be converted to absolute path
func IsHandlerNotFoundErr ¶
IsHandlerNotFoundErr checks if an error occurred because no handler could be found to handle the output file type
func IsInvalidExtErr ¶
IsInvalidExtErr checks if an error returned by package `writer` was caused by the file having an invalid extension
func IsInvalidFileErr ¶
IsInvalidFileErr checks if an error returned by package writer was caused because the output file could not be located from the path
func IsPathDirErr ¶
IsPathDirErr checks if an error was caused because the path to output file points to an existing directory
func IsPathNotWriteableErr ¶
IsPathNotWriteableErr checks if an error was caused because the path is not writeable
func IsReadFileErr ¶
IsReadFileErr checks if an error was being caused because the output cannot be read
func Start ¶
Start initializes package `writer` - should be executed before calls to any function from this package
Returns error if the output file could not be parsed from the path, if the output file contains an invalid extension, or if the path could not be converted to absolute path, the path points to an existing directory, or if the path is not writeable. Use the functions IsInvalidFileErr, IsInvalidExtErr, IsAbsPathErr, IsPathNotWriteableErr, IsReadFileErr, and IsPathDirErr to explicitly check for these errors
Note: This function can be run once (at most)
func Write ¶
Write writes a DirInfo object to the output file while replacing existing contents in the file
Calls to the function fail if no handler can interact with the given filetype, if the file cannot be written to, or if marshaling the DirInfo object fails. Use the functions IsHandlerNotFoundErr, and IsPathNotWriteableErr to check against these errors. If neither function matches, assume the cause of the error is that the DirInfo object cannot be marshall-ed
Types ¶
type Checksums ¶
type Checksums struct {
CRC32 string
}
Checksums contains the various checksums generated for files
type DirInfo ¶
type DirInfo struct { // Path contains the full path to the directory Path string // Dirs maps all the directories present in this directory as DirInfo objects Dirs []DirInfo // Files maps all the files present in the directory as FileInfo objects Files []FileInfo // LastMod indicates the time when the directory was last modified. Represents epoch // time, not intended to be human-readable LastMod int64 }
DirInfo defines contents of a directory. Each directory can contain multiple directories, and files
Note: It is recommended to use the NewDir function to create DirInfo objects
var RootDir DirInfo
RootDir contains contents from the existing output file. Populated within the Start function. Modifications can be made as needed, use the Write function to write the updated contents back to the output file
func NewDir ¶
NewDir is a wrapper to create DirInfo objects. Objects created using this method would ensure they have DirInfo.LastMod value set and more
It is recommended to use this function to create DirInfo objects ¶
Note: If `dirName` is not empty, it will be merged into `parentPath` to form the final path to the directory. If not, `parentPath` will be assumed to be the complete path
func (*DirInfo) CalcModTime ¶
CalcModTime calculates the LastMod time for a directory, in case this has already been calculated, the previous value is directly returned
For directories without a set value of LastMod time, this method will iterate over each file and directory, fetching the last mod time for each, and setting the greatest value as the last modification time for this directory
Note: For worst-case scenario, this method ends up being recursive -- a call is made to CalcModTime for each directory in Dirs
type FileInfo ¶
type FileInfo struct { // Path contains the full path to the file Path string // Checksums contains checksums generated for the file Checksums Checksums // Size contains the file size, in bytes - not intended to be human-readable Size int64 // LastMod indicates time when the file was last modified. Represents epoch time, // not intended to be human-readable LastMod int64 }
FileInfo defines the structure of the output for an individual file - this will be written to the output file
type Handler ¶
type Handler interface { // Marshal takes an object of DirInfo as input, and converts it into a byte array // that can be easily written to a file. // // The second argument indicates if the marshall-ed output needs to be indented Marshal(info *DirInfo, indent ...bool) ([]byte, error) // Unmarshal parses encoded data and stores the result in the DirInfo object Unmarshal([]byte, *DirInfo) error // FileTypes returns an array of strings - indicating the supported output file // extensions. The extensions are case-insensitive FileTypes() []string }
Handler defines a simple interface to interact with data from the output file - this includes reading data from the file, and writing to the same file when done
Each Handler defines an object that can interact with certain file types (based on file extensions)