Skip to content

Commit

Permalink
feat: group cli commands for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie0129 committed Sep 26, 2023
1 parent d175a2c commit 5691814
Showing 1 changed file with 50 additions and 22 deletions.
72 changes: 50 additions & 22 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ var (
logLevel = "info"
)

var (
gBasic = "Basic"
gAdvanced = "Advanced"
gInstallation = "Installation"
commandGroups = []string{
gBasic,
gAdvanced,
gInstallation,
}
)

// NewCommand .
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -32,6 +43,13 @@ func NewCommand() *cobra.Command {
globalFlags := cmd.PersistentFlags()
globalFlags.StringVarP(&logLevel, "log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal, panic)")

for _, i := range commandGroups {
cmd.AddGroup(&cobra.Group{
ID: i,
Title: i,
})
}

cmd.AddCommand(
NewDaemonCommand(),
NewVersionCommand(),
Expand Down Expand Up @@ -63,9 +81,10 @@ func NewVersionCommand() *cobra.Command {
// NewDaemonCommand .
func NewDaemonCommand() *cobra.Command {
return &cobra.Command{
Use: "daemon",
Hidden: true,
Short: "Run batt daemon in the foreground",
Use: "daemon",
Hidden: true,
Short: "Run batt daemon in the foreground",
GroupID: gAdvanced,
Run: func(cmd *cobra.Command, args []string) {
logrus.Infof("batt version %s commit %s", version.Version, version.GitCommit)
runDaemon()
Expand All @@ -76,8 +95,9 @@ func NewDaemonCommand() *cobra.Command {
// NewInstallCommand .
func NewInstallCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Short: "Install batt (system-wide)",
Use: "install",
Short: "Install batt (system-wide)",
GroupID: gInstallation,
Long: `Install batt daemon to launchd (system-wide).
This makes batt run in the background and automatically start on boot. You must run this command as root.
Expand Down Expand Up @@ -142,8 +162,9 @@ By default, only root user is allowed to access the batt daemon for security rea
// NewUninstallCommand .
func NewUninstallCommand() *cobra.Command {
return &cobra.Command{
Use: "uninstall",
Short: "Uninstall batt (system-wide)",
Use: "uninstall",
Short: "Uninstall batt (system-wide)",
GroupID: gInstallation,
Long: `Uninstall batt daemon from launchd (system-wide).
This stops batt and removes it from launchd.
Expand Down Expand Up @@ -192,8 +213,9 @@ You must run this command as root.`,
// NewLimitCommand .
func NewLimitCommand() *cobra.Command {
return &cobra.Command{
Use: "limit [percentage]",
Short: "Set upper charge limit",
Use: "limit [percentage]",
Short: "Set upper charge limit",
GroupID: gBasic,
Long: `Set upper charge limit.
This is a percentage from 10 to 100.
Expand Down Expand Up @@ -222,8 +244,9 @@ Setting the limit to 10-99 will enable the battery charge limit. However, settin
// NewSetPreventIdleSleepCommand .
func NewSetPreventIdleSleepCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "prevent-idle-sleep",
Short: "Set whether to prevent idle sleep during a charging session",
Use: "prevent-idle-sleep",
Short: "Set whether to prevent idle sleep during a charging session",
GroupID: gAdvanced,
Long: `Set whether to prevent idle sleep during a charging session.
Due to macOS limitations, batt will pause when your computer goes to sleep. As a result, when you are in a charging session and your computer goes to sleep, the battery charge limit will no longer function and the battery will charge to 100%. If you want the battery to stay below the charge limit, this behavior is probably not what you want. This option, together with disable-charging-pre-sleep, will prevent this from happening.
Expand Down Expand Up @@ -278,8 +301,9 @@ However, this does not prevent manual sleep. For example, if you manually put yo
// NewSetDisableChargingPreSleepCommand .
func NewSetDisableChargingPreSleepCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "disable-charging-pre-sleep",
Short: "Set whether to disable charging before sleep if charge limit is enabled",
Use: "disable-charging-pre-sleep",
Short: "Set whether to disable charging before sleep if charge limit is enabled",
GroupID: gAdvanced,
Long: `Set whether to disable charging before sleep if charge limit is enabled.
Due to macOS limitations, batt will pause when your computer goes to sleep. As a result, when you are in a charging session and your computer goes to sleep, the battery charge limit will no longer function and the battery will charge to 100%. If you want the battery to stay below the charge limit, this behavior is probably not what you want. This option, together with prevent-idle-sleep, will prevent this from happening. prevent-idle-sleep can prevent idle sleep to keep the battery charge limit active. However, this does not prevent manual sleep. For example, if you manually put your computer to sleep or close the lid, batt will not prevent your computer from sleeping. This is a limitation of macOS.
Expand Down Expand Up @@ -332,11 +356,12 @@ To prevent such cases, you can use disable-charging-pre-sleep. This will disable
// NewAdapterCommand .
func NewAdapterCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "adapter",
Short: "Enable or disable power adapter",
Use: "adapter",
Short: "Enable or disable power input",
GroupID: gBasic,
Long: `Enable or disable power adapter, i.e, power input.
When you disable power adapter, power input from the wall will be disabled. This is useful when you are plugged in and you still want to consume battery instead of power input.`,
When you disable power adapter, power input from the wall will be disabled. Your computer will not use any power from the wall even if it is plugged in. This is useful when you are plugged in and you still want to consume battery instead of power input.`,
}

cmd.AddCommand(
Expand Down Expand Up @@ -405,8 +430,9 @@ When you disable power adapter, power input from the wall will be disabled. This
// NewStatusCommand .
func NewStatusCommand() *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Get the current status of batt",
Use: "status",
GroupID: gBasic,
Short: "Get the current status of batt",
RunE: func(cmd *cobra.Command, args []string) error {
// Get various info first.
ret, err := get("/charging")
Expand Down Expand Up @@ -548,8 +574,9 @@ func NewStatusCommand() *cobra.Command {
// NewLowerLimitDeltaCommand .
func NewLowerLimitDeltaCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "lower-limit-delta",
Short: "Set the delta between lower and upper charge limit",
Use: "lower-limit-delta",
Short: "Set the delta between lower and upper charge limit",
GroupID: gAdvanced,
Long: `Set the delta between lower and upper charge limit.
When you set a charge limit, for example, on a Lenovo ThinkPad, you can set two percentages. The first one is the upper limit, and the second one is the lower limit. When the battery charge is above the upper limit, the computer will stop charging. When the battery charge is below the lower limit, the computer will start charging. If the battery charge is between the two limits, the computer will keep whatever charging state it is in.
Expand Down Expand Up @@ -588,8 +615,9 @@ For example, if you want to set the lower limit to be 5% less than the upper lim
// NewSetControlMagSafeLEDCommand .
func NewSetControlMagSafeLEDCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "magsafe-led",
Short: "Control MagSafe LED according to battery charging status",
Use: "magsafe-led",
Short: "Control MagSafe LED according to battery charging status",
GroupID: gAdvanced,
Long: `Control MagSafe LED according to battery charging status.
This setting can make the MagSafe LED behave like a normal device, i.e., it will turn green when charge limit is reached (not charging). By default, on a MagSafe-compatible device, the MagSafe LED will always be orange (charging) even if charge limit is reached and charging is disabled by batt, due to Apple's limitations. You cannot enable this feature on a non-MagSafe-compatible device.
Expand Down

0 comments on commit 5691814

Please sign in to comment.