Skip to content

Commit

Permalink
avoid const
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkuhn committed Sep 27, 2024
1 parent 5ad83fb commit a0eced8
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import (
_ "modernc.org/sqlite"
)

const (
recreateSchema = true // CAUTION !!!!
)

var (
c = make(chan os.Signal, 1)
clientID = "default"
Expand All @@ -36,6 +32,7 @@ type Options struct {
checkInterval time.Duration
idleAfter time.Duration
cmd string
dropCreate bool
}

// main runs the tracker
Expand All @@ -45,6 +42,7 @@ func main() {
flag.DurationVar(&opts.checkInterval, "interval", 2*time.Second, "Interval to check for idle time")
flag.DurationVar(&opts.idleAfter, "idle", 10*time.Second, "Max time before client is considered idle")
flag.StringVar(&opts.cmd, "cmd", "ioreg", "Command to retrieve HIDIdleTime")
flag.BoolVar(&opts.dropCreate, "drop-create", false, "Drop and re-create db schema (CAUTION!)")
if len(os.Args) > 1 && os.Args[1] == "help" {
flag.PrintDefaults()
return
Expand All @@ -54,7 +52,7 @@ func main() {
ctx, ctxCancel := context.WithCancel(context.Background())
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)

db, err := initDB()
db, err := initDB(&opts)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -128,7 +126,7 @@ func currentIdleTime(ctx context.Context, cmd string) (int64, error) {
}

// initDB initializes SQLite DB in local filesystem
func initDB() (*sql.DB, error) {
func initDB(opts *Options) (*sql.DB, error) {
fn := filepath.Join(dbDirectory, "db")

db, err := sql.Open("sqlite", fn)
Expand All @@ -143,7 +141,7 @@ func initDB() (*sql.DB, error) {

// drop table if exists t; insert into t values(42), (314);
var dropStmt string
if recreateSchema {
if opts.dropCreate {
dropStmt = "DROP TABLE IF EXISTS track;\n"
}
if _, err = db.Exec(dropStmt + `
Expand Down

0 comments on commit a0eced8

Please sign in to comment.