From 1056bca9e918006533da0b2694e51848e6aaa47f Mon Sep 17 00:00:00 2001 From: Till Kuhn Date: Tue, 12 Nov 2024 18:41:48 +0100 Subject: [PATCH] introduce global flags --- Makefile | 6 ++-- README.adoc | 80 ++++++++++++++++++++++++++++++++++------------------ cmd/root.go | 11 ++++++-- cmd/track.go | 7 +++-- 4 files changed, 68 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 55d567d..0d509ea 100644 --- a/Makefile +++ b/Makefile @@ -99,15 +99,15 @@ vulncheck: ## Run govulncheck scanner .PHONY: run run: ## Run app in tracker mode (dev env), add -drop-create to recreate db - go run main.go track --env dev --idle 10s --interval 5s --debug + go run main.go --debug track --env dev --idle 10s --interval 5s .PHONY: report-dev report-dev: ## Show report for dev env db - go run main.go report --env dev + go run main.go --debug report --env dev .PHONY: report report: ## Show report for default db - go run main.go report --debug + go run main.go --debug report .PHONY: run-help run-help: ## Run app in help mode diff --git a/README.adoc b/README.adoc index 6f8f813..d41009c 100644 --- a/README.adoc +++ b/README.adoc @@ -78,41 +78,65 @@ $ launchctl unload ~/Library/LaunchAgents/com.github.tillkuhn.billy-idle.plist [source,shell] ---- -$ billy help +$ billy -Usage: billy [command] +Usage: + billy-idle [command] -Available Commands (more coming soon): - track Starts the tracker +Available Commands: + busy Enter busy time + completion Generate the autocompletion script for the specified shell + help Help about any command + report Generate a report + track Track idle time + +Flags: + -d, --debug Debug checkpoints + -h, --help help for billy-idle -Use "billy [command] -h" for more information about a command. ---- [source,shell] ---- -$ billy track -help - -Usage of track: - -app-dir string - App Directory e.g. for SQLite DB (defaults to $HOME/.billy-idle/ - -cmd string - Command to retrieve HIDIdleTime (default "ioreg") - -debug - Debug checkpoints - -drop-create - Drop and re-create db schema on startup - -env string - Environment (default "default") - -idle duration - Max tolerated idle time before client enters idle state (default 10s) - -interval duration - Interval to check for idle time (default 2s) - -max-busy duration - Max allowed time busy period per day (w/o breaks), report only (default 10h0m0s) - -min-busy duration - Minimum time for a busy record to count for the report (default 5m0s) - -reg-busy duration - Regular busy period per day (w/o breaks), report only (default 7h48m0s) +$ billy track -h + +Starts the tracker in daemon mode to record busy and idle times. + +Usage: + billy-idle track [flags] + +Flags: + -a, --app-dir string App Directory e.g. for SQLite DB (defaults to $HOME/.billy-idle/ + -c, --cmd string Command to retrieve HIDIdleTime (default "ioreg") + --drop-create Drop and re-create db schema on startup + -e, --env string Environment (default "default") + -h, --help help for track + -m, --idle duration Max tolerated idle time before client enters idle state (default 10s) + -i, --interval duration Interval to check for idle time (default 2s) + +Global Flags: + -d, --debug Debug checkpoints + +---- + +[source,shell] +---- +$ billy report -h + +Generates a report based on the recorded idle and busy times. + +Usage: + billy-idle report [flags] + +Flags: + -a, --app-dir string App Directory e.g. for SQLite DB (defaults to $HOME/.billy-idle/ + -e, --env string Environment (default "default") + -h, --help help for report + --max-busy duration Max allowed time busy period per day (w/o breaks), report only (default 10h0m0s) + --min-busy duration Minimum time for a busy record to count for the report (default 5m0s) + --reg-busy duration Regular busy period per day (w/o breaks), report only (default 7h48m0s) + + ---- == Database Support diff --git a/cmd/root.go b/cmd/root.go index bc4131d..b4b23ce 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ var ( var rootCmd = &cobra.Command{ Use: "billy-idle", Short: "Simple busy / idle time tracker inspired by the ancient article 'Inactivity and Idle Time on OS X'.", - Long: `OS X has a timer called HIDIdleTime that tracks the last time you interacted with the computer, e.g. moved the mouse, typed a key, or interacted with the computer. + Long: `Simple busy / idle time tracker based on the macOS timer called HIDIdleTime that tracks the last time you interacted with the computer, e.g. moved the mouse, typed a key, or interacted with the computer. billy-idle simply queries this value periodically using the ioreg utility that ships with macOS, and matches it against a pre-defined threshold. If exceeded, it will create a record for the busy time period in database. This data can later be used as input for time tracking tools or statistics.`, @@ -44,5 +44,12 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + // A flag can be 'persistent', meaning that this flag will be available to the command it's assigned to as well + // as every command under that command. + + // For global flags, assign a flag as a persistent flag on the root. + rootCmd.PersistentFlags().BoolVarP(&opts.Debug, "debug", "d", false, "Debug checkpoints") + + // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/track.go b/cmd/track.go index 3fa4bce..470670f 100644 --- a/cmd/track.go +++ b/cmd/track.go @@ -21,8 +21,10 @@ var opts tracker.Options var trackCmd = &cobra.Command{ Use: "track", Short: "Track idle time", - Long: `Starts the tracker daemon mode to record busy and idle times.`, - Run: func(_ *cobra.Command, _ []string) { + Long: `Starts the tracker in daemon mode to record busy and idle times.`, + Run: func(cmd *cobra.Command, _ []string) { + dbg, _ := cmd.Flags().GetBool("debug") + opts.Debug = dbg track() }, } @@ -34,7 +36,6 @@ func init() { rootCmd.AddCommand(trackCmd) trackCmd.PersistentFlags().StringVarP(&opts.Env, "env", "e", "default", "Environment") trackCmd.PersistentFlags().StringVarP(&opts.AppDir, "app-dir", "a", "", "App Directory e.g. for SQLite DB (defaults to $HOME/.billy-idle/") - trackCmd.PersistentFlags().BoolVarP(&opts.Debug, "debug", "d", false, "Debug checkpoints") trackCmd.PersistentFlags().StringVarP(&opts.Cmd, "cmd", "c", "ioreg", "Command to retrieve HIDIdleTime") trackCmd.PersistentFlags().BoolVar(&opts.DropCreate, "drop-create", false, "Drop and re-create db schema on startup") trackCmd.PersistentFlags().DurationVarP(&opts.CheckInterval, "interval", "i", 2*time.Second, "Interval to check for idle time")