writer

package
v0.0.0-...-d363ebf Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package writer handles the part of writing output to a file

Use the function writer.Start to initialize the package

Index

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

func IsAbsPathErr(err error) bool

IsAbsPathErr checks if an error occurred because a relative path could not be converted to absolute path

func IsHandlerNotFoundErr

func IsHandlerNotFoundErr(err error) bool

IsHandlerNotFoundErr checks if an error occurred because no handler could be found to handle the output file type

func IsInvalidExtErr

func IsInvalidExtErr(err error) bool

IsInvalidExtErr checks if an error returned by package `writer` was caused by the file having an invalid extension

func IsInvalidFileErr

func IsInvalidFileErr(err error) bool

IsInvalidFileErr checks if an error returned by package writer was caused because the output file could not be located from the path

func IsPathDirErr

func IsPathDirErr(err error) bool

IsPathDirErr checks if an error was caused because the path to output file points to an existing directory

func IsPathNotWriteableErr

func IsPathNotWriteableErr(err error) bool

IsPathNotWriteableErr checks if an error was caused because the path is not writeable

func IsReadFileErr

func IsReadFileErr(err error) bool

IsReadFileErr checks if an error was being caused because the output cannot be read

func Start

func Start(confPath string) error

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

func Write(info *DirInfo) error

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

func NewDir(
	dirName, parentPath string, dirs []DirInfo, files []FileInfo, lastMod int64,
) DirInfo

NewDir is a wrapper to create DirInfo objects. Objects created using this method would ensure they have DirInfo.LastMod value set and more

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

func (dir *DirInfo) CalcModTime() int64

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

func (*DirInfo) Name

func (dir *DirInfo) Name() string

Name returns the name of the directory

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

func (*FileInfo) Name

func (file *FileInfo) Name() string

Name returns the name of the 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)

Directories

Path Synopsis
Package json handles reading from, and writing to JSON output files
Package json handles reading from, and writing to JSON output files

Jump to

Keyboard shortcuts

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