-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathnonbrew.go
138 lines (107 loc) · 3.95 KB
/
nonbrew.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//go:build !brew
package main
import (
"fmt"
"os"
"github.com/charlie0129/batt/pkg/smc"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
commandGroups = append(commandGroups, gInstallation)
}
// NewInstallCommand .
func NewInstallCommand() *cobra.Command {
cmd := &cobra.Command{
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.
By default, only root user is allowed to access the batt daemon for security reasons. As a result, you will need to run batt client as root to control battery charging, e.g. setting charge limit. If you want to allow non-root users, i.e., you, to access the daemon, you can use the --allow-non-root-access flag, so you don't have to use sudo every time.`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
err := loadConfig()
if err != nil {
return err
}
flags := cmd.Flags()
b, err := flags.GetBool("allow-non-root-access")
if err != nil {
return err
}
if config.AllowNonRootAccess && !b {
logrus.Warnf("Previously, non-root users were allowed to access the batt daemon. However, this will be disabled at every installation unless you provide the --allow-non-root-access flag. Consider using the flag if you want to allow non-root users to access the daemon.")
}
// Before installation, always reset config.AllowNonRootAccess to flag value
// instead of the one in config file.
config.AllowNonRootAccess = b
return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
if config.AllowNonRootAccess {
logrus.Info("non-root users are allowed to access the batt daemon.")
} else {
logrus.Info("only root user is allowed to access the batt daemon.")
}
err := installDaemon()
if err != nil {
// check if current user is root
if os.Geteuid() != 0 {
logrus.Errorf("you must run this command as root")
}
return fmt.Errorf("failed to install daemon: %v. Are you root?", err)
}
err = saveConfig()
if err != nil {
return err
}
logrus.Infof("installation succeeded")
exePath, _ := os.Executable()
cmd.Printf("`launchd' will use current binary (%s) at startup so please make sure you do not move this binary. Once this binary is moved or deleted, you will need to run ``batt install'' again.\n", exePath)
return nil
},
}
cmd.Flags().Bool("allow-non-root-access", false, "Allow non-root users to access batt daemon.")
return cmd
}
// NewUninstallCommand .
func NewUninstallCommand() *cobra.Command {
return &cobra.Command{
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.
You must run this command as root.`,
RunE: func(cmd *cobra.Command, _ []string) error {
err := uninstallDaemon()
if err != nil {
// check if current user is root
if os.Geteuid() != 0 {
logrus.Errorf("you must run this command as root")
}
return fmt.Errorf("failed to uninstall daemon: %v", err)
}
logrus.Infof("resetting charge limits")
// Open Apple SMC for read/writing
smcC := smc.New()
if err := smcC.Open(); err != nil {
return fmt.Errorf("failed to open SMC: %v", err)
}
err = smcC.EnableCharging()
if err != nil {
return fmt.Errorf("failed to enable charging: %v", err)
}
err = smcC.EnableAdapter()
if err != nil {
return fmt.Errorf("failed to enable adapter: %v", err)
}
if err := smcC.Close(); err != nil {
return fmt.Errorf("failed to close SMC: %v", err)
}
fmt.Println("successfully uninstalled")
cmd.Printf("Your config is kept in %s, in case you want to use `batt' again. If you want a complete uninstall, you can remove both config file and batt itself manually.\n", configPath)
return nil
},
}
}