From e7959e9ab2663f652fbb018a1ccb73f9568a8d67 Mon Sep 17 00:00:00 2001 From: Mark Gardner Date: Sun, 5 May 2019 22:57:49 -0700 Subject: [PATCH 1/2] Updated storage cache. SHA-256 style. --- main.go | 61 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/main.go b/main.go index cb2e644..8b6aba5 100644 --- a/main.go +++ b/main.go @@ -9,9 +9,8 @@ package main import ( "crypto/sha256" "fmt" - "io" + "hash" "io/ioutil" - "net/url" "os" "path/filepath" @@ -24,7 +23,7 @@ import ( const ( AppDesc AppMetaData = "File change detection tool" AppName AppMetaData = "FileDelta" - AppVersion AppMetaData = "0.3.0" + AppVersion AppMetaData = "0.4.0" CLIName AppMetaData = "filedelta" CommandCheck = "check" CommandStore = "store" @@ -62,12 +61,13 @@ type ( * VARIABLES */ var ( - cacheFile string - homePath string - cachePath string - cmd string - debug = false - hashFile string + cacheFile string + cachePath string + cmd string + debug = false + hashFile string + hashSHA256 hash.Hash + homePath string ) /* @@ -78,7 +78,7 @@ func cacheFilePath(file string) string { if err == nil { file = fileAbs } - return fmt.Sprintf("%s/%s", cachePath, url.QueryEscape(file)) + return fmt.Sprintf("%s/%s", cachePath, hashSHA256String(file)) } func cacheHashGet(file string) (string, error) { @@ -93,25 +93,38 @@ func cacheHashPut(file, hash string) error { func fileHashGet(file string) (string, error) { var ( - err error - f *os.File - hash string + err error + f *os.File + fileBytes []byte + hashStr string ) - // fmt.Printf("fileHashGet() | file = %q\n", file) + f, err = os.Open(file) if err != nil { - // fmt.Printf("fileHashGet() | err = %q\n", err) - return hash, err + return hashStr, err } + fileBytes, err = ioutil.ReadAll(f) + hashStr = hashSHA256ByteString(fileBytes) + f.Close() + + return hashStr, err +} +func hashSHA256Bytes(bytes []byte) []byte { h := sha256.New() - _, err = io.Copy(h, f) - f.Close() + h.Write(bytes) + return h.Sum(nil) +} - hash = fmt.Sprintf("%x", h.Sum(nil)) - // fmt.Printf("fileHashGet() | hash = %q\n", hash) +func hashSHA256ByteString(bytes []byte) string { + hashBytes := hashSHA256Bytes(bytes) + return fmt.Sprintf("%x", hashBytes) +} - return hash, err +func hashSHA256String(value string) string { + // fmt.Printf("hashSHA256String() | nil | hashStr = %q\n", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") + hashBytes := hashSHA256Bytes([]byte(value)) + return fmt.Sprintf("%x", hashBytes) } func init() { @@ -144,6 +157,7 @@ func init() { } } + hashSHA256 = sha256.New() homePath, _ = homedir.Dir() cachePath = fmt.Sprintf("%s/.local/filedelta/cache", homePath) @@ -151,10 +165,6 @@ func init() { if err != nil { os.MkdirAll(cachePath, 0700) } - - if len(hashFile) > 0 { - cacheFile = url.QueryEscape(hashFile) - } } /* @@ -163,7 +173,6 @@ func init() { func main() { if len(hashFile) > 0 { hexHash, _ := fileHashGet(hashFile) - switch cmd { case CommandStore: if debug { From 5f54d3c58de10d6bff34ff559380e999e3898175 Mon Sep 17 00:00:00 2001 From: Mark Gardner Date: Sun, 5 May 2019 23:22:28 -0700 Subject: [PATCH 2/2] Updated help info --- Justfile | 8 ++------ README.md | 24 +++++++++++++++++++++++- main.go | 2 ++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Justfile b/Justfile index 2193e28..a0d5323 100644 --- a/Justfile +++ b/Justfile @@ -207,13 +207,9 @@ _list-dir path='.': fi -# Run Helper -@run id='app' +args='': - just _term-lw "{{PROJECT_NAME}}" - just --highlight run-{{id}} {{args}} - # Run the app -@run-app +args='': +@run +args='': + just _term-lw "{{PROJECT_NAME}}" echo "$ {{BINARY_NAME}} {{args}}" go run {{SOURCE_NAME}} {{args}} diff --git a/README.md b/README.md index d07a008..76419db 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -FileDelta v0.3.0 +FileDelta v0.4.0 ================ 1. Calculates the SHA-256 hash of a file @@ -21,6 +21,28 @@ test/touched.txt: ERROR Exit Code: 1 ``` +Help Example +------------ + +```bash +$ filedelta --help +FileDelta v0.4.0 + +File change detection tool + +USAGE: filedelta [OPTIONS] COMMAND FILENAME + +COMMANDS + check Compares the provided files hash against the one stored + store Stores the provided files hash for later comparison + +OPTIONS + -d | --debug Output debugging info + -h | --help Output this help info + -v | --version Output app version info + +``` + Hashes are now stored in `$HOME/.local/filedelta/cache/` in plain text files. This was inspired by a short discussion at diff --git a/main.go b/main.go index 8b6aba5..ba64f99 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,8 @@ var ( AppLabel = AppMetaData(fmt.Sprintf("%s v%s", string(AppName), string(AppVersion))) AppHelp = AppMetaData(fmt.Sprintf("%s\n\n%s", string(AppLabel), string(AppDesc)) + ` +USAGE: filedelta [OPTIONS] COMMAND FILENAME + COMMANDS check Compares the provided files hash against the one stored store Stores the provided files hash for later comparison