From 2755d0e1739eeab459ff47b0c742f349f879986d Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Fri, 29 Nov 2024 20:22:23 +0000 Subject: [PATCH] add json config support --- README.md | 44 +++++++++++++++++++++++++++++--------------- cmd/chkbit/help.go | 6 ++++++ cmd/chkbit/main.go | 36 ++++++++++++++++++++++-------------- scripts/tests | 13 +++++++++++-- 4 files changed, 68 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 33b5afa..62a07e6 100644 --- a/README.md +++ b/README.md @@ -83,27 +83,27 @@ Run `chkbit PATH` to verify only. ``` Usage: chkbit [ ...] [flags] +Ensures the safety of your files by verifying that their data integrity remains +intact over time, especially during transfers and backups. + + For help tips run "chkbit -H" or go to + https://github.com/laktak/chkbit + Arguments: [ ...] directories to check Flags: -h, --help Show context-sensitive help. -H, --tips Show tips. - -c, --check check mode: chkbit will verify files in readonly - mode (default mode) - -u, --update update mode: add and update indices - -a, --add-only add mode: only add new and modified files, - do not check existing (quicker) - -i, --show-ignored-only show-ignored mode: only show ignored files - -m, --show-missing show missing files/directories - -d, --include-dot include dot files + -m, --[no-]show-missing show missing files/directories + -d, --[no-]include-dot include dot files + -S, --[no-]skip-symlinks do not follow symlinks + -R, --[no-]no-recurse do not recurse into subdirectories + -D, --[no-]no-dir-in-index do not track directories in the index --force force update of damaged items (advanced usage only) - -S, --skip-symlinks do not follow symlinks - -R, --no-recurse do not recurse into subdirectories - -D, --no-dir-in-index do not track directories in the index -l, --log-file=STRING write to a logfile if specified - --log-verbose verbose logging + --[no-]log-verbose verbose logging --algo="blake3" hash algorithm: md5, sha512, blake3 (default: blake3) --index-name=".chkbit" filename where chkbit stores its hashes, @@ -112,10 +112,18 @@ Flags: filename that chkbit reads its ignore list from, needs to start with '.' (default: .chkbitignore) -w, --workers=5 number of workers to use (default: 5) - --plain show plain status instead of being fancy - -q, --quiet quiet, don't show progress/information - -v, --verbose verbose output + --[no-]plain show plain status instead of being fancy + -q, --[no-]quiet quiet, don't show progress/information + -v, --[no-]verbose verbose output -V, --version show version information + +mode + -c, --check check mode: chkbit will verify files in readonly + mode (default mode) + -u, --update update mode: add and update indices + -a, --add-only add mode: only add new and modified files, do not + check existing (quicker) + -i, --show-ignored-only show-ignored mode: only show ignored files ``` ``` @@ -142,6 +150,12 @@ Status codes: del: file/directory removed ign: ignored (see .chkbitignore) EXC: exception/panic + +Configuration file (json): +- location /home/spark/.config/chkbit/config.json +- key names are the option names with '-' replaced by '_' +- for example --include-dot is written as: + { "include_dot": true } ``` chkbit is set to use only 5 workers by default so it will not slow your system to a crawl. You can specify a higher number to make it a lot faster if the IO throughput can also keep up. diff --git a/cmd/chkbit/help.go b/cmd/chkbit/help.go index 348cec3..a1c3147 100644 --- a/cmd/chkbit/help.go +++ b/cmd/chkbit/help.go @@ -28,4 +28,10 @@ Status codes: del: file/directory removed ign: ignored (see .chkbitignore) EXC: exception/panic + +Configuration file (json): +- location +- key names are the option names with '-' replaced by '_' +- for example --include-dot is written as: + { "include_dot": true } ` diff --git a/cmd/chkbit/main.go b/cmd/chkbit/main.go index 671f1c6..49797c8 100644 --- a/cmd/chkbit/main.go +++ b/cmd/chkbit/main.go @@ -5,6 +5,7 @@ import ( "io" "log" "os" + "path/filepath" "strings" "sync" "time" @@ -44,25 +45,25 @@ var ( var cli struct { Paths []string `arg:"" optional:"" name:"paths" help:"directories to check"` Tips bool `short:"H" help:"Show tips."` - Check bool `short:"c" help:"check mode: chkbit will verify files in readonly mode (default mode)"` - Update bool `short:"u" help:"update mode: add and update indices"` - AddOnly bool `short:"a" help:"add mode: only add new and modified files, do not check existing (quicker)"` - ShowIgnoredOnly bool `short:"i" help:"show-ignored mode: only show ignored files"` - ShowMissing bool `short:"m" help:"show missing files/directories"` - IncludeDot bool `short:"d" help:"include dot files"` + Check bool `short:"c" help:"check mode: chkbit will verify files in readonly mode (default mode)" xor:"mode" group:"mode"` + Update bool `short:"u" help:"update mode: add and update indices" xor:"mode" group:"mode"` + AddOnly bool `short:"a" help:"add mode: only add new and modified files, do not check existing (quicker)" xor:"mode" group:"mode"` + ShowIgnoredOnly bool `short:"i" help:"show-ignored mode: only show ignored files" xor:"mode" group:"mode"` + ShowMissing bool `short:"m" help:"show missing files/directories" negatable:""` + IncludeDot bool `short:"d" help:"include dot files" negatable:""` + SkipSymlinks bool `short:"S" help:"do not follow symlinks" negatable:""` + NoRecurse bool `short:"R" help:"do not recurse into subdirectories" negatable:""` + NoDirInIndex bool `short:"D" help:"do not track directories in the index" negatable:""` Force bool `help:"force update of damaged items (advanced usage only)"` - SkipSymlinks bool `short:"S" help:"do not follow symlinks"` - NoRecurse bool `short:"R" help:"do not recurse into subdirectories"` - NoDirInIndex bool `short:"D" help:"do not track directories in the index"` LogFile string `short:"l" help:"write to a logfile if specified"` - LogVerbose bool `help:"verbose logging"` + LogVerbose bool `help:"verbose logging" negatable:""` Algo string `default:"blake3" help:"hash algorithm: md5, sha512, blake3 (default: blake3)"` IndexName string `default:".chkbit" help:"filename where chkbit stores its hashes, needs to start with '.' (default: .chkbit)"` IgnoreName string `default:".chkbitignore" help:"filename that chkbit reads its ignore list from, needs to start with '.' (default: .chkbitignore)"` Workers int `short:"w" default:"5" help:"number of workers to use (default: 5)"` - Plain bool `help:"show plain status instead of being fancy"` - Quiet bool `short:"q" help:"quiet, don't show progress/information"` - Verbose bool `short:"v" help:"verbose output"` + Plain bool `help:"show plain status instead of being fancy" negatable:""` + Quiet bool `short:"q" help:"quiet, don't show progress/information" negatable:""` + Verbose bool `short:"v" help:"verbose output" negatable:""` Version bool `short:"V" help:"show version information"` } @@ -285,14 +286,21 @@ func (m *Main) run() { os.Args = append(os.Args, "--help") } + var configPath = "chkbit-config.json" + configRoot, err := os.UserConfigDir() + if err == nil { + configPath = filepath.Join(configRoot, "chkbit/config.json") + } + kong.Parse(&cli, kong.Name("chkbit"), kong.Description(headerHelp), kong.UsageOnError(), + kong.Configuration(kong.JSON, configPath), ) if cli.Tips { - fmt.Println(helpTips) + fmt.Println(strings.ReplaceAll(helpTips, "", configPath)) os.Exit(0) } diff --git a/scripts/tests b/scripts/tests index 5a1b690..ad8c9ba 100755 --- a/scripts/tests +++ b/scripts/tests @@ -4,8 +4,17 @@ set -e script_dir=$(dirname "$(realpath "$0")") cd $script_dir/.. -# prep +echo "# test module" +go test -v . +echo "# test util" +go test -v ./cmd/chkbit/util -count=1 + +echo "# prep files" $script_dir/build -go test -v ./cmd/chkbit/util -count=1 +echo "# test files" +if [[ -f ~/.config/chkbit/config.json ]]; then + echo 'error: unable to test with config file preset' + exit 1 +fi go test -v ./scripts -count=1