dirreport

package module
v0.0.0-...-562cd78 Latest Latest
Warning

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

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

README

go-dirreport

Generate a report for a directory that can be e.g. used for synchronization with another directory

Build Status Go Report Card GoDoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Debugf = func(f string, a ...interface{}) {}

Debugf can be used to add a debug output writer

Functions

This section is empty.

Types

type DebugFunc

type DebugFunc func(f string, a ...interface{})

DebugFunc can be used to overwrite the Debugf function

type DirectoryReport

type DirectoryReport struct {
	Files map[string]string   `json:"files"`
	Dirs  map[string]struct{} `json:"dirs"`
	// contains filtered or unexported fields
}

DirectoryReport holds a report of a directory. This includes subdirectories and hashes of all files.

func NewDirectoryReport

func NewDirectoryReport(path string) (*DirectoryReport, error)

NewDirectoryReport walks through the specified path and generates a DirectoryReport.

func (*DirectoryReport) Diff

func (r *DirectoryReport) Diff(other *DirectoryReport) (newFiles, removedFiles, modifiedFiles, newDirs, removedDirs []string)

Diff calculates the difference between two directory reports. It generates a list of new, removed and modified files and new dirs and removed dirs.

Example
defer reset()
// before state
mustCreateFile(rootOld, "file1.txt", "content a")
mustCreateFile(rootOld, "file2.txt", "content b")
before, _ := NewDirectoryReport(rootOld)
// after state
mustCreateFile(rootNew, "file1.txt", "content c") // changed content of file1.txt
mustCreateFile(rootNew, "file3.txt", "content b") // renamed file2.txt to file3.txt
after, _ := NewDirectoryReport(rootNew)
newFiles, removedFiles, modifiedFiles, _, _ := before.Diff(after)
fmt.Printf("New Files: %+v\n", newFiles)
fmt.Printf("Removed Files: %+v\n", removedFiles)
fmt.Printf("Modified Files: %+v\n", modifiedFiles)
Output:

New Files: [file3.txt]
Removed Files: [file2.txt]
Modified Files: [file1.txt]
Example (Second)
defer reset()
// before state
mustCreateDirectory(rootOld, "dir1")
mustCreateDirectory(rootOld, "dir1/subdir1")
mustCreateDirectory(rootOld, "dir1/subdir2")
before, _ := NewDirectoryReport(rootOld)
// after state
mustCreateDirectory(rootNew, "dir1")
mustCreateDirectory(rootNew, "dir1/subdir1")
mustCreateDirectory(rootNew, "dir1/subdir3") // renamed subdir2 to subdir 3
after, _ := NewDirectoryReport(rootNew)
_, _, _, newDirectories, removedDirectories := before.Diff(after)
fmt.Printf("New Directories: %+v\n", newDirectories)
fmt.Printf("Removed Directies: %+v\n", removedDirectories)
Output:

New Directories: [dir1/subdir3]
Removed Directies: [dir1/subdir2]
Example (Third)
defer reset()
// before state
mustCreateDirectory(rootOld, "dir1")
mustCreateDirectory(rootOld, "dir1/subdir1")
mustCreateDirectory(rootOld, "dir1/subdir2")
mustCreateFile(rootOld, "file1.txt", "content a")
mustCreateFile(rootOld, "dir1/file2.txt", "content b")
mustCreateFile(rootOld, "dir1/subdir1/file3.txt", "content c")
mustCreateFile(rootOld, "dir1/subdir2/file4.txt", "content d")
before, err := NewDirectoryReport(rootOld)
if err != nil {
	panic(err)
}
// after state
mustCreateDirectory(rootNew, "dir1")
mustCreateDirectory(rootNew, "dir1/subdir1")
/* mustCreateDirectory(rootNew, "dir1/subdir2") */ // this directory is removed
mustCreateDirectory(rootNew, "dir1/subdir3")       // this directory is new
mustCreateFile(rootNew, "file1.txt", "content a")
mustCreateFile(rootNew, "dir1/file2.txt", "content b")
mustCreateFile(rootNew, "dir1/subdir1/file3.txt", "modcontent c") // the content of this file has changed
mustCreateFile(rootNew, "dir1/subdir3/file4.txt", "content d")    // this files has moved from subdir2 to subdir3
mustCreateFile(rootNew, "dir1/subdir3/file5.txt", "content e")    // this files is new
after, err := NewDirectoryReport(rootNew)
if err != nil {
	panic(err)
}
newFiles, removedFiles, modifiedFiles, newDirectories, removedDirectories := before.Diff(after)
fmt.Printf("New Files: %+v\n", newFiles)
fmt.Printf("Removed Files: %+v\n", removedFiles)
fmt.Printf("Modified Files: %+v\n", modifiedFiles)
fmt.Printf("New Directories: %+v\n", newDirectories)
fmt.Printf("Removed Directies: %+v\n", removedDirectories)
Output:

New Files: [dir1/subdir3/file4.txt dir1/subdir3/file5.txt]
Removed Files: [dir1/subdir2/file4.txt]
Modified Files: [dir1/subdir1/file3.txt]
New Directories: [dir1/subdir3]
Removed Directies: [dir1/subdir2]

Jump to

Keyboard shortcuts

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