go-lockout/config/config.go

85 lines
1.4 KiB
Go

package config
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"launchpad.net/goyaml"
)
type Config struct {
SuspendCmd, LockCmd string
NoXSuspendCmd, NoXLockCmd string
}
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, " ")
}
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, " ")
}
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
}