go-lockout/config/config.go

86 lines
1.4 KiB
Go
Raw Permalink Normal View History

2013-07-05 14:46:45 +00:00
package config
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"launchpad.net/goyaml"
2013-07-05 14:46:45 +00:00
)
type Config struct {
SuspendCmd, LockCmd string
NoXSuspendCmd, NoXLockCmd string
2013-07-05 14:46:45 +00:00
}
func (c *Config) Suspend() (err error) {
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) {
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
}