core

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package core implements the data models and helper utilities for printing tree structure of the specified directory

By Vinit Kumar Rai <vinitrai.marshal@gmail.com>

Index

Constants

View Source
const (
	FILE = 1
	DIR  = 2
)

Enum for FileType

Variables

View Source
var ColorMap = map[string]Colorize{

	"gray":  colorCurry(aurora.GrayFg, false),
	"grayb": colorCurry(aurora.GrayFg, true),

	"green":  colorCurry(aurora.GreenFg, false),
	"greenb": colorCurry(aurora.GreenFg, true),

	"blue":  colorCurry(aurora.BlueFg, false),
	"blueb": colorCurry(aurora.BlueFg, true),

	"brown":  colorCurry(aurora.BrownFg, false),
	"brownb": colorCurry(aurora.BrownFg, true),

	"red":  colorCurry(aurora.RedFg, false),
	"redb": colorCurry(aurora.RedFg, true),

	"black":  colorCurry(aurora.BlackFg, false),
	"blackb": colorCurry(aurora.BlackFg, true),

	"magenta":  colorCurry(aurora.MagentaFg, false),
	"magentab": colorCurry(aurora.MagentaFg, true),

	"cyan":  colorCurry(aurora.CyanFg, false),
	"cyanb": colorCurry(aurora.CyanFg, true),
}

ColorMap A map of supported colors, wich uses internal partial function and returns a function of type Colorize. Uses of partial function allows us to limit the uses of Aurora's way of colorizing the string at one place and gives us the flexibility to easily switch to any other lib in future.

View Source
var FTypeIds = map[string]FileType{
	"file": FILE,
	"dir":  DIR,
}

FTypeIds Map of String Name to Its FileType

View Source
var FTypeName = map[FileType]string{
	FILE: "file",
	DIR:  "dir",
}

FTypeName Map of fileType to Its String name

Functions

func FileFilterPattern

func FileFilterPattern(fis []os.FileInfo, pattern string, include bool) []os.FileInfo

FileFilterPattern Filter fileInfos whose filepath matched with patern. Include flag is used to control the inclusion/exclusion of matched fileinfos in the result set. eg. If include flag is true, and file path matched with given pattern, then that file info will be incuded in the result. Note: This filter will only be applied on files

func Filter

func Filter(eles []os.FileInfo, f filterFunc) []os.FileInfo

Filter utility to Filter files

func FilterInDir

func FilterInDir(fis []os.FileInfo) []os.FileInfo

FilterInDir return on those file infos which are of type directory

func FilterOutHidden

func FilterOutHidden(fis []os.FileInfo) []os.FileInfo

FilterOutHidden return on those file which are not hidden

func GetExtra added in v0.1.2

func GetExtra(tree Tree, opt Options) string

GetExtra ...

func InitAurora

func InitAurora(enableColorOutput bool)

InitAurora Utility method to initialize Aurora(ANSI color library), being used to print colorize output. Aurora Provides flag to allow enabling or disabling output

Types

type ByModTime added in v0.1.2

type ByModTime []os.FileInfo

ByModTime Sort FileInfos by ModificationTime

func (ByModTime) Len added in v0.1.2

func (a ByModTime) Len() int

func (ByModTime) Less added in v0.1.2

func (a ByModTime) Less(i, j int) bool

func (ByModTime) Swap added in v0.1.2

func (a ByModTime) Swap(i, j int)

type ByNameReverse added in v0.1.2

type ByNameReverse []os.FileInfo

ByNameReverse Sort FileInfos by name

func (ByNameReverse) Len added in v0.1.2

func (a ByNameReverse) Len() int

func (ByNameReverse) Less added in v0.1.2

func (a ByNameReverse) Less(i, j int) bool

func (ByNameReverse) Swap added in v0.1.2

func (a ByNameReverse) Swap(i, j int)

type Colorize

type Colorize func(interface{}) aurora.Value

Colorize A function type which is being used in Options

type FileFilterFunc

type FileFilterFunc func(fis []os.FileInfo) []os.FileInfo

FileFilterFunc ...

type FileType

type FileType int

FileType EnumType for filetype

func GetFileType

func GetFileType(fi os.FileInfo) FileType

GetFileType Utility method to get fileType

func (FileType) MarshalJSON

func (ftype FileType) MarshalJSON() ([]byte, error)

MarshalJSON Marshal FileType as JSON String

func (FileType) String

func (ftype FileType) String() string

String Implementation of Stringer interface

func (*FileType) UnmarshalJSON

func (ftype *FileType) UnmarshalJSON(b []byte) error

UnmarshalJSON UnMarshal FileType JSON String as FileType

type JSONTree

type JSONTree struct {
	Name    string     `json:"name"`
	FType   FileType   `json:"file_type"`
	FStats  *Stats     `json:"stats,omitempty"`
	SubTree []JSONTree `json:"subtree"`
}

JSONTree Json Representation of Tree

type Options

type Options struct {
	IncludeHidden    bool
	DirOnly          bool
	ShowFullPath     bool
	NoReport         bool
	FollowLink       bool
	Prune            bool
	PrintProtection  bool
	PrintSize        bool
	PrintUID         bool
	PrintGID         bool
	PrintModTime     bool
	SortReverse      bool
	SortByModTime    bool
	FileLimit        int
	MaxLevel         int16
	Indent           int
	TimeFormat       string
	IncludePattern   string
	ExcludePattern   string
	OutputPath       string
	JSONIncludeStats bool
	DirColor         Colorize
	FileColor        Colorize
	SymLinkColor     Colorize
	TLinkColor       Colorize
	LLinkColor       Colorize
	PipeColor        Colorize
}

Options Data model to hold command line options

func DefaultOptions

func DefaultOptions() Options

DefaultOptions A utility method to create default Options for hitree command This is intensionally created for test cases

type Stats

type Stats struct {
	DirCount         int       `json:"dir_count"`
	FileCount        int       `json:"file_count"`
	Size             int64     `json:"size"`
	ModificationTime time.Time `json:"mod_time"`
	Permission       string    `json:"permission"`
}

Stats Data Model to hold the File & Dir Count in the tree. Going further this will be enhanced to keep other stats like disk utilization, modification time etc at file and dir level which could be helpful in including / excluding files and directories based on these values.

func NewEmptyStats added in v0.1.2

func NewEmptyStats(fi os.FileInfo) Stats

NewEmptyStats ...

type Tree

type Tree struct {
	Root      os.FileInfo
	Childrens []Tree
	Stats     Stats
}

Tree Data Model representing a file/directory as Root and Its childrens(in case of directory). It also contains stats of the Root, which currently only have count of all directories and files in the Root. Going further we can ustilize Stat to include few more details like space used etc.

func TraverseDir

func TraverseDir(root string, opt Options, level int16) (Tree, error)

TraverseDir utility method to recursively traverse through the dir

func (Tree) AsJSONString

func (tree Tree) AsJSONString(opt Options) ([]byte, error)

AsJSONString ...

func (Tree) AsJSONTree

func (tree Tree) AsJSONTree(opt Options) JSONTree

AsJSONTree Utility function to return tree as json string

func (Tree) NodeName

func (tree Tree) NodeName(opt Options) (string, error)

NodeName Get NodeName of the tree

func (Tree) Print

func (tree Tree) Print(w io.Writer, opt Options)

Print is a utility method on the tree to print tree structure on console. Later we will few more mthods on tree which will allow to output tree result to other means like file or socket etc.

func (Tree) String

func (tree Tree) String() string

String Implements String method of Stringer interface, helpful in debugging. For Printing the tree structure like linux command use Print method

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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