From 0d9b44c54b8be0248218326550205c56b9813c1c Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Tue, 8 Oct 2024 13:54:51 +0800 Subject: [PATCH] chore: add various convenience options for brew formulae #9 --- cmd.go | 11 ++++++++++- daemon.go | 9 +++++++++ hack/cc.chlc.batt.plist | 19 +++++++++++-------- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/cmd.go b/cmd.go index aa20aa6..f9871f9 100644 --- a/cmd.go +++ b/cmd.go @@ -42,6 +42,8 @@ func NewCommand() *cobra.Command { globalFlags := cmd.PersistentFlags() globalFlags.StringVarP(&logLevel, "log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal, panic)") + globalFlags.StringVar(&configPath, "config", configPath, "config file path") + globalFlags.StringVar(&unixSocketPath, "daemon-socket", unixSocketPath, "batt daemon unix socket path") for _, i := range commandGroups { cmd.AddGroup(&cobra.Group{ @@ -81,7 +83,7 @@ func NewVersionCommand() *cobra.Command { // NewDaemonCommand . func NewDaemonCommand() *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "daemon", Hidden: true, Short: "Run batt daemon in the foreground", @@ -91,6 +93,13 @@ func NewDaemonCommand() *cobra.Command { runDaemon() }, } + + f := cmd.Flags() + + f.BoolVar(&alwaysAllowNonRootAccess, "always-allow-non-root-access", false, + "Always allow non-root users to access the daemon.") + + return cmd } // NewInstallCommand . diff --git a/daemon.go b/daemon.go index d0d0e91..4d97a28 100644 --- a/daemon.go +++ b/daemon.go @@ -45,6 +45,10 @@ func setupRoutes() *gin.Engine { return router } +var ( + alwaysAllowNonRootAccess = false +) + func runDaemon() { router := setupRoutes() @@ -54,6 +58,11 @@ func runDaemon() { } logrus.Infof("config loaded: %#v", config) + if alwaysAllowNonRootAccess { + config.AllowNonRootAccess = true + logrus.Info("alwaysAllowNonRootAccess is set to true, allowing non-root access") + } + // Receive SIGHUP to reload config go func() { sigc := make(chan os.Signal, 1) diff --git a/hack/cc.chlc.batt.plist b/hack/cc.chlc.batt.plist index ffbc4a8..6366915 100644 --- a/hack/cc.chlc.batt.plist +++ b/hack/cc.chlc.batt.plist @@ -1,22 +1,25 @@ - + + KeepAlive + Label cc.chlc.batt + + ProcessType + Interactive ProgramArguments /path/to/batt daemon - -l=debug + --log-level=debug - StandardOutPath - /tmp/batt.log - StandardErrorPath - /tmp/batt.log RunAtLoad - KeepAlive - + StandardErrorPath + /tmp/batt.log + StandardOutPath + /tmp/batt.log