Skip to content

Commit

Permalink
Merge branch 'release/v0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Gardner committed May 6, 2019
2 parents 3ee2b98 + 5f54d3c commit 2429253
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
8 changes: 2 additions & 6 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FileDelta v0.3.0
FileDelta v0.4.0
================

1. Calculates the SHA-256 hash of a file
Expand All @@ -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 <https://github.com/casey/just/issues/424>
Expand Down
63 changes: 37 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ package main
import (
"crypto/sha256"
"fmt"
"io"
"hash"
"io/ioutil"
"net/url"
"os"
"path/filepath"

Expand All @@ -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"
Expand All @@ -39,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
Expand All @@ -62,12 +63,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
)

/*
Expand All @@ -78,7 +80,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) {
Expand All @@ -93,25 +95,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() {
Expand Down Expand Up @@ -144,17 +159,14 @@ func init() {
}
}

hashSHA256 = sha256.New()
homePath, _ = homedir.Dir()
cachePath = fmt.Sprintf("%s/.local/filedelta/cache", homePath)

_, err := os.Stat(cachePath)
if err != nil {
os.MkdirAll(cachePath, 0700)
}

if len(hashFile) > 0 {
cacheFile = url.QueryEscape(hashFile)
}
}

/*
Expand All @@ -163,7 +175,6 @@ func init() {
func main() {
if len(hashFile) > 0 {
hexHash, _ := fileHashGet(hashFile)

switch cmd {
case CommandStore:
if debug {
Expand Down

0 comments on commit 2429253

Please sign in to comment.