Skip to content

Commit

Permalink
introduce global flags
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkuhn committed Nov 12, 2024
1 parent 8dc74eb commit 1056bca
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 52 additions & 28 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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/<env>
-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/<env>
-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/<env>
-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
Expand Down
11 changes: 9 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
Expand All @@ -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")
}
7 changes: 4 additions & 3 deletions cmd/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
}
Expand All @@ -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/<env>")
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")
Expand Down

0 comments on commit 1056bca

Please sign in to comment.