adding the sources

This commit is contained in:
Vincent Batts 2013-07-05 10:46:45 -04:00
parent 1caa7b987f
commit c7eb842ec7
3 changed files with 138 additions and 0 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
Copyright (c) 2013, Vincent Batts <vbatts@hashbangbash.com>
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.

73
config/config.go Normal file
View file

@ -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
}

44
lockout/main.go Normal file
View file

@ -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
`
)