2013-07-05 14:46:45 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2014-10-09 12:55:20 +00:00
|
|
|
|
|
|
|
"launchpad.net/goyaml"
|
2013-07-05 14:46:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2014-10-09 12:55:20 +00:00
|
|
|
SuspendCmd, LockCmd string
|
|
|
|
NoXSuspendCmd, NoXLockCmd string
|
2013-07-05 14:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Suspend() (err error) {
|
2014-10-09 12:55:20 +00:00
|
|
|
var args []string
|
|
|
|
if len(os.Getenv("DISPLAY")) > 0 {
|
|
|
|
args = strings.Split(c.SuspendCmd, " ")
|
|
|
|
} else {
|
|
|
|
args = strings.Split(c.NoXSuspendCmd, " ")
|
|
|
|
}
|
2013-07-05 14:46:45 +00:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go io.Copy(os.Stdout, stdout)
|
|
|
|
go io.Copy(os.Stderr, stderr)
|
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Lock() (err error) {
|
2014-10-09 12:55:20 +00:00
|
|
|
var args []string
|
|
|
|
if len(os.Getenv("DISPLAY")) > 0 {
|
|
|
|
args = strings.Split(c.LockCmd, " ")
|
|
|
|
} else {
|
|
|
|
args = strings.Split(c.NoXLockCmd, " ")
|
|
|
|
}
|
2013-07-05 14:46:45 +00:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go io.Copy(os.Stdout, stdout)
|
|
|
|
go io.Copy(os.Stderr, stderr)
|
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig(filename string) (c *Config, err error) {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c = &Config{}
|
|
|
|
err = goyaml.Unmarshal(b, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|