go-lockout/config/config.go
2013-07-05 10:46:45 -04:00

73 lines
1.1 KiB
Go

package config
import (
"fmt"
"io"
"io/ioutil"
"launchpad.net/goyaml"
"os"
"os/exec"
"strings"
)
type Config struct {
SuspendCmd, LockCmd string
}
func (c *Config) Suspend() (err error) {
args := strings.Split(c.SuspendCmd, " ")
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) {
args := strings.Split(c.LockCmd, " ")
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
}