Created
April 20, 2015 20:53
-
-
Save dallasmarlow/8d228807ccacd40b4fd9 to your computer and use it in GitHub Desktop.
mysql service wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"errors" | |
"fmt" | |
"os/exec" | |
"strings" | |
"time" | |
) | |
const ( | |
bashPath = `/bin/bash` | |
) | |
var ( | |
ErrStatusUnknown = errors.New(`Unknown response from command 'service mysql status'`) | |
ErrStderrNotEmpty = errors.New(`Expected stderr from mysql service command to be empty`) | |
) | |
type StatusVal int | |
const ( | |
StatusUnknown StatusVal = iota | |
StatusStopped | |
StatusRunning | |
) | |
func (v StatusVal) String() string { | |
switch v { | |
case StatusStopped: | |
return `Status: Stopped` | |
case StatusRunning: | |
return `Status: Running` | |
} | |
return `Status: Unknown` | |
} | |
type ServiceStatus struct { | |
Status StatusVal | |
CheckedAt time.Time | |
} | |
func (s ServiceStatus) IsRunning() bool { | |
return s.Status == StatusRunning | |
} | |
func ServiceStart() (ServiceStatus, error) { | |
return serviceAction(`start`) | |
} | |
func ServiceStop() (ServiceStatus, error) { | |
return serviceAction(`stop`) | |
} | |
func ServiceGetStatus() (ServiceStatus, error) { | |
return serviceAction(`status`) | |
} | |
func serviceAction(action string) (ServiceStatus, error) { | |
stdout, stderr, err := BashExec(`service mysql ` + action) | |
fmt.Println(`stdout:`, stdout) | |
fmt.Println(`stderr:`, stderr) | |
switch { | |
case err != nil: | |
return ServiceStatus{}, err | |
case stderr != ``: | |
return ServiceStatus{}, ErrStderrNotEmpty | |
} | |
return parseServiceStatusMsg(stdout) | |
} | |
func parseServiceStatusMsg(msg string) (ServiceStatus, error) { | |
switch { | |
case strings.HasPrefix(msg, `mysql start/running`): | |
return ServiceStatus{StatusRunning, time.Now()}, nil | |
case strings.HasPrefix(msg, `mysql stop/waiting`): | |
return ServiceStatus{StatusStopped, time.Now()}, nil | |
} | |
return ServiceStatus{}, ErrStatusUnknown | |
} | |
func BashExec(bashCmd string) (string, string, error) { | |
cmd := exec.Command(bashPath, `-c`, bashCmd) | |
var stdout, stderr bytes.Buffer | |
cmd.Stdout = &stdout | |
cmd.Stderr = &stderr | |
if err := cmd.Start(); err != nil { | |
return ``, ``, err | |
} | |
if err := cmd.Wait(); err != nil { | |
return ``, ``, err | |
} | |
return stdout.String(), stderr.String(), nil | |
} | |
func main() { | |
serviceStatus, err := ServiceGetStatus() | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println(serviceStatus) | |
startStatus, err := ServiceStart() | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println(startStatus) | |
stopStatus, err := ServiceStop() | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println(stopStatus) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment