diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9004798 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +Copyright (c) 2013, Vincent Batts +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. Redistributions in binary +form must reproduce the above copyright notice, this list of conditions and the +following disclaimer in the documentation and/or other materials provided with +the distribution. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..7cf4239 --- /dev/null +++ b/config/config.go @@ -0,0 +1,73 @@ +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 +} diff --git a/lockout/main.go b/lockout/main.go new file mode 100644 index 0000000..ce7cdb0 --- /dev/null +++ b/lockout/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "github.com/vbatts/go-lockout/config" + "os" +) + +func main() { + var ( + c *config.Config + err error + ) + + c, err = config.ReadConfig(DefaultConfig) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + fmt.Fprintf(os.Stderr, UsageMessage) + os.Exit(1) + } + //fmt.Printf("%#v\n", c) + + err = c.Lock() + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + os.Exit(1) + } + err = c.Suspend() + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + os.Exit(1) + } +} + +var ( + DefaultConfig = os.Getenv("HOME") + "/.config/lockout.conf" + UsageMessage = ` +Please configure this file to look something like + + suspendcmd: sudo /usr/sbin/pm-suspend + lockcmd: xscreensaver-command -lock + +` +)