Skip to content

Commit

Permalink
Change OS functions to value receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
kke committed Jan 19, 2021
1 parent 83ca417 commit 8a7cc6c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 83 deletions.
16 changes: 8 additions & 8 deletions os/initsystem/openrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ type OpenRC struct {
}

// StartService starts a a service
func (i *OpenRC) StartService(s string) error {
func (i OpenRC) StartService(s string) error {
return i.Host.Execf("sudo rc-service %s start", s)
}

// StopService stops a a service
func (i *OpenRC) StopService(s string) error {
func (i OpenRC) StopService(s string) error {
return i.Host.Execf("sudo rc-service %s stop", s)
}

// ServiceScriptPath returns the path to a service configuration file
func (i *OpenRC) ServiceScriptPath(s string) (string, error) {
func (i OpenRC) ServiceScriptPath(s string) (string, error) {
return i.Host.ExecOutputf("sudo rc-service -r %s 2> /dev/null", s)
}

// RestartService restarts a a service
func (i *OpenRC) RestartService(s string) error {
func (i OpenRC) RestartService(s string) error {
return i.Host.Execf("sudo rc-service %s restart", s)
}

// DaemonReload reloads init system configuration
func (i *OpenRC) DaemonReload() error {
func (i OpenRC) DaemonReload() error {
return nil
}

// EnableService enables a a service
func (i *OpenRC) EnableService(s string) error {
func (i OpenRC) EnableService(s string) error {
return i.Host.Execf("sudo rc-update add %s", s)
}

// DisableService disables a a service
func (i *OpenRC) DisableService(s string) error {
func (i OpenRC) DisableService(s string) error {
return i.Host.Execf("sudo rc-update del %s", s)
}

// ServiceIsRunning returns true if a service is running
func (i *OpenRC) ServiceIsRunning(s string) bool {
func (i OpenRC) ServiceIsRunning(s string) bool {
return i.Host.Execf(`sudo rc-service %s status | grep -q "status: started"`, s) == nil
}
16 changes: 8 additions & 8 deletions os/initsystem/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@ type Systemd struct {
}

// StartService starts a a service
func (i *Systemd) StartService(s string) error {
func (i Systemd) StartService(s string) error {
return i.Host.Execf("sudo systemctl start %s", s)
}

// EnableService enables a a service
func (i *Systemd) EnableService(s string) error {
func (i Systemd) EnableService(s string) error {
return i.Host.Execf("sudo systemctl enable %s", s)
}

// DisableService disables a a service
func (i *Systemd) DisableService(s string) error {
func (i Systemd) DisableService(s string) error {
return i.Host.Execf("sudo systemctl disable %s", s)
}

// StopService stops a a service
func (i *Systemd) StopService(s string) error {
func (i Systemd) StopService(s string) error {
return i.Host.Execf("sudo systemctl stop %s", s)
}

// RestartService restarts a a service
func (i *Systemd) RestartService(s string) error {
func (i Systemd) RestartService(s string) error {
return i.Host.Execf("sudo systemctl restart %s", s)
}

// DaemonReload reloads init system configuration
func (i *Systemd) DaemonReload() error {
func (i Systemd) DaemonReload() error {
return i.Host.Execf("sudo systemctl daemon-reload")
}

// ServiceIsRunning returns true if a service is running
func (i *Systemd) ServiceIsRunning(s string) bool {
func (i Systemd) ServiceIsRunning(s string) bool {
return i.Host.Execf(`sudo systemctl status %s | grep -q "(running)"`, s) == nil
}

// ServiceScriptPath returns the path to a service configuration file
func (i *Systemd) ServiceScriptPath(s string) (string, error) {
func (i Systemd) ServiceScriptPath(s string) (string, error) {
return i.Host.ExecOutputf(`systemctl show -p FragmentPath %s.service 2> /dev/null | cut -d"=" -f2`, s)
}
54 changes: 27 additions & 27 deletions os/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type initSystem interface {
}

// Kind returns "linux"
func (c *Linux) Kind() string {
func (c Linux) Kind() string {
return "linux"
}

// memoizing accessor to the init system (systemd, openrc)
func (c *Linux) is() initSystem {
func (c Linux) is() initSystem {
if c.isys == nil {
initctl, err := c.Host.ExecOutput("basename $(command -v rc-service systemd)")
if err != nil {
Expand All @@ -52,47 +52,47 @@ func (c *Linux) is() initSystem {
}

// StartService starts a service on the host
func (c *Linux) StartService(s string) error {
func (c Linux) StartService(s string) error {
return c.is().StartService(s)
}

// StopService stops a service on the host
func (c *Linux) StopService(s string) error {
func (c Linux) StopService(s string) error {
return c.is().StopService(s)
}

// RestartService restarts a service on the host
func (c *Linux) RestartService(s string) error {
func (c Linux) RestartService(s string) error {
return c.is().RestartService(s)
}

// DisableService disables a service on the host
func (c *Linux) DisableService(s string) error {
func (c Linux) DisableService(s string) error {
return c.is().DisableService(s)
}

// EnableService enables a service on the host
func (c *Linux) EnableService(s string) error {
func (c Linux) EnableService(s string) error {
return c.is().EnableService(s)
}

// ServiceIsRunning returns true if the service is running on the host
func (c *Linux) ServiceIsRunning(s string) bool {
func (c Linux) ServiceIsRunning(s string) bool {
return c.is().ServiceIsRunning(s)
}

// ServiceScriptPath returns the service definition file path on the host
func (c *Linux) ServiceScriptPath(s string) (string, error) {
func (c Linux) ServiceScriptPath(s string) (string, error) {
return c.is().ServiceScriptPath(s)
}

// DaemonReload performs an init system config reload
func (c *Linux) DaemonReload() error {
func (c Linux) DaemonReload() error {
return c.is().DaemonReload()
}

// CheckPrivilege returns an error if the user does not have passwordless sudo enabled
func (c *Linux) CheckPrivilege() error {
func (c Linux) CheckPrivilege() error {
if c.Host.Exec("sudo -n true") != nil {
return fmt.Errorf("user does not have passwordless sudo access")
}
Expand All @@ -101,7 +101,7 @@ func (c *Linux) CheckPrivilege() error {
}

// Pwd returns the current working directory of the session
func (c *Linux) Pwd() string {
func (c Linux) Pwd() string {
pwd, err := c.Host.ExecOutput("pwd")
if err != nil {
return ""
Expand All @@ -110,39 +110,39 @@ func (c *Linux) Pwd() string {
}

// JoinPath joins a path
func (c *Linux) JoinPath(parts ...string) string {
func (c Linux) JoinPath(parts ...string) string {
return strings.Join(parts, "/")
}

// Hostname resolves the short hostname
func (c *Linux) Hostname() string {
func (c Linux) Hostname() string {
hostname, _ := c.Host.ExecOutput("hostname -s")

return hostname
}

// LongHostname resolves the FQDN (long) hostname
func (c *Linux) LongHostname() string {
func (c Linux) LongHostname() string {
longHostname, _ := c.Host.ExecOutput("hostname")

return longHostname
}

// IsContainer returns true if the host is actually a container
func (c *Linux) IsContainer() bool {
func (c Linux) IsContainer() bool {
return c.Host.Exec("grep 'container=docker' /proc/1/environ") == nil
}

// FixContainer makes a container work like a real host
func (c *Linux) FixContainer() error {
func (c Linux) FixContainer() error {
if c.IsContainer() {
return c.Host.Exec("sudo mount --make-rshared /")
}
return nil
}

// SELinuxEnabled is true when SELinux is enabled
func (c *Linux) SELinuxEnabled() bool {
func (c Linux) SELinuxEnabled() bool {
if output, err := c.Host.ExecOutput("sudo getenforce"); err == nil {
return strings.ToLower(strings.TrimSpace(output)) == "enforcing"
}
Expand All @@ -151,7 +151,7 @@ func (c *Linux) SELinuxEnabled() bool {
}

// WriteFile writes file to host with given contents. Do not use for large files.
func (c *Linux) WriteFile(path string, data string, permissions string) error {
func (c Linux) WriteFile(path string, data string, permissions string) error {
if data == "" {
return fmt.Errorf("empty content in WriteFile to %s", path)
}
Expand All @@ -174,23 +174,23 @@ func (c *Linux) WriteFile(path string, data string, permissions string) error {
}

// ReadFile reads a files contents from the host.
func (c *Linux) ReadFile(path string) (string, error) {
func (c Linux) ReadFile(path string) (string, error) {
return c.Host.ExecOutput(fmt.Sprintf("sudo cat %s", escape.Quote(path)), exec.HideOutput())
}

// DeleteFile deletes a file from the host.
func (c *Linux) DeleteFile(path string) error {
func (c Linux) DeleteFile(path string) error {
return c.Host.Exec(fmt.Sprintf(`sudo rm -f %s`, escape.Quote(path)))
}

// FileExist checks if a file exists on the host
func (c *Linux) FileExist(path string) bool {
func (c Linux) FileExist(path string) bool {
return c.Host.Exec(fmt.Sprintf(`sudo test -e %s`, escape.Quote(path))) == nil
}

// LineIntoFile tries to find a matching line in a file and replace it with a new entry
// TODO refactor this into go because it's too magical.
func (c *Linux) LineIntoFile(path, matcher, newLine string) error {
func (c Linux) LineIntoFile(path, matcher, newLine string) error {
if c.FileExist(path) {
err := c.Host.Exec(fmt.Sprintf(`file=%s; match=%s; line=%s; sudo grep -q "${match}" "$file" && sudo sed -i "/${match}/c ${line}" "$file" || (echo "$line" | sudo tee -a "$file" > /dev/null)`, escape.Quote(path), escape.Quote(matcher), escape.Quote(newLine)))
if err != nil {
Expand All @@ -202,7 +202,7 @@ func (c *Linux) LineIntoFile(path, matcher, newLine string) error {
}

// UpdateEnvironment updates the hosts's environment variables
func (c *Linux) UpdateEnvironment(env map[string]string) error {
func (c Linux) UpdateEnvironment(env map[string]string) error {
for k, v := range env {
err := c.LineIntoFile("/etc/environment", fmt.Sprintf("^%s=", k), fmt.Sprintf("%s=%s", k, v))
if err != nil {
Expand All @@ -215,7 +215,7 @@ func (c *Linux) UpdateEnvironment(env map[string]string) error {
}

// CleanupEnvironment removes environment variable configuration
func (c *Linux) CleanupEnvironment(env map[string]string) error {
func (c Linux) CleanupEnvironment(env map[string]string) error {
for k := range env {
err := c.LineIntoFile("/etc/environment", fmt.Sprintf("^%s=", k), "")
if err != nil {
Expand All @@ -227,11 +227,11 @@ func (c *Linux) CleanupEnvironment(env map[string]string) error {
}

// CommandExist returns true if the command exists
func (c *Linux) CommandExist(cmd string) bool {
func (c Linux) CommandExist(cmd string) bool {
return c.Host.Execf("sudo command -v %s", cmd) == nil
}

// Reboot executes the reboot command
func (c *Linux) Reboot() error {
func (c Linux) Reboot() error {
return c.Host.Exec("sudo reboot")
}
2 changes: 1 addition & 1 deletion os/linux/enterpriselinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ type EnterpriseLinux struct {
}

// InstallPackage installs packages via yum
func (c *EnterpriseLinux) InstallPackage(s ...string) error {
func (c EnterpriseLinux) InstallPackage(s ...string) error {
return c.Host.Execf("sudo yum install -y %s", strings.Join(s, " "))
}
2 changes: 1 addition & 1 deletion os/linux/sles.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type SLES struct {
}

// InstallPackage installs packages via zypper
func (c *SLES) InstallPackage(s ...string) error {
func (c SLES) InstallPackage(s ...string) error {
return c.Host.Execf("sudo zypper refresh && sudo zypper -n install -y %s", strings.Join(s, " "))
}

Expand Down
2 changes: 1 addition & 1 deletion os/linux/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func init() {
}

// InstallPackage installs packages via apt-get
func (c *Ubuntu) InstallPackage(s ...string) error {
func (c Ubuntu) InstallPackage(s ...string) error {
return c.Host.Execf("sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -q %s", strings.Join(s, " "))
}
20 changes: 10 additions & 10 deletions os/mac/darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,52 @@ type Darwin struct {
}

// Kind returns "darwin"
func (c *Darwin) Kind() string {
func (c Darwin) Kind() string {
return "darwin"
}

// StartService starts a a service
func (c *Darwin) StartService(s string) error {
func (c Darwin) StartService(s string) error {
return c.Host.Execf(`sudo launchctl start %s`, s)
}

// StopService stops a a service
func (c *Darwin) StopService(s string) error {
func (c Darwin) StopService(s string) error {
return c.Host.Execf(`sudo launchctl stop %s`, s)
}

// ServiceScriptPath returns the path to a service configuration file
func (c *Darwin) ServiceScriptPath(s string) (string, error) {
func (c Darwin) ServiceScriptPath(s string) (string, error) {
return "", fmt.Errorf("not available on mac")
}

// RestartService restarts a a service
func (c *Darwin) RestartService(s string) error {
func (c Darwin) RestartService(s string) error {
return c.Host.Execf(`sudo launchctl kickstart -k %s`, s)
}

// DaemonReload reloads init system configuration
func (c *Darwin) DaemonReload() error {
func (c Darwin) DaemonReload() error {
return nil
}

// EnableService enables a a service
func (c *Darwin) EnableService(s string) error {
func (c Darwin) EnableService(s string) error {
return c.Host.Execf(`sudo launchctl enable %s`, s)
}

// DisableService disables a a service
func (c *Darwin) DisableService(s string) error {
func (c Darwin) DisableService(s string) error {
return c.Host.Execf(`sudo launchctl disable %s`, s)
}

// ServiceIsRunning returns true if a service is running
func (c *Darwin) ServiceIsRunning(s string) bool {
func (c Darwin) ServiceIsRunning(s string) bool {
return c.Host.Execf(`sudo launchctl list %s | grep -q '"PID"'`, s) == nil
}

// InstallPackage installs a package using brew
func (c *Darwin) InstallPackage(s ...string) error {
func (c Darwin) InstallPackage(s ...string) error {
return c.Host.Execf("brew install %s", strings.Join(s, " "))
}

Expand Down
Loading

0 comments on commit 8a7cc6c

Please sign in to comment.