Skip to content

Commit

Permalink
Merge pull request #3 from caarlos0/improvements
Browse files Browse the repository at this point in the history
WIP: start, stop et al
  • Loading branch information
caarlos0 committed Aug 21, 2016
2 parents 9842abf + 5cea254 commit 93c0387
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
am-i-working
43 changes: 7 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,16 @@ VPN, a line with `domain mycompany.com` appears in my `/etc/resolv.conf`.
What I want here is to log those changes so I can easily get my extra working
hours later (and automate the sending of the report too).

To run it, you can simply `./am-i-working -d mycompany > work.log`, or
create a service in the OS level to keep it running forever.

### OSX Service

Create a file like this:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>am-i-working</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/am-i-working</string>
<string>--domain</string>
<string>mycompany</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/path/to/am-i-working-out.log</string>
<key>StandardErrorPath</key>
<string>/path/to/am-i-working-err.log</string>
</dict>
</plist>
```

Changing the path to the `am-i-working` binary, domain argument and where to
save the logs (usually `/Users/USER/Library/Logs/`).
To run it, you can simply `./am-i-working watch --domain mycompany > work.log`,
or create a service in the OS level to keep it running forever.

Save the file in `~/Library/LaunchAgents/am-i-working.plist`.
### macOS Service

Then run:
To set it up as a macOS service, just tun:

```console
launchctl load ~/Library/LaunchAgents/am-i-working.plist
$ am-i-working create --domain mycompany
$ am-i-working start
```

And you can check that the `am-i-working` process will be running on background.
There are also the `stop` and `restart` commands.
28 changes: 0 additions & 28 deletions cmd/actions/main.go

This file was deleted.

15 changes: 15 additions & 0 deletions cmd/commands/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package commands

import "github.com/urfave/cli"

var flags = []cli.Flag{
cli.StringFlag{
Name: "domain, d",
Usage: "Domain name that appears in domain section of /etc/resolv.conf when you're connected to company networks",
},
cli.StringFlag{
Name: "file, f",
Usage: "File to watch for domain regexes",
Value: "/etc/resolv.conf",
},
}
43 changes: 43 additions & 0 deletions cmd/commands/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package commands

import "github.com/urfave/cli"

// CreateService creates the service on the OS
var CreateService = cli.Command{
Name: "create",
Usage: "creates the service on the OS",
Flags: flags,
Action: func(c *cli.Context) error {
return create(c)
},
}

// StartService starts the service on the OS
var StartService = cli.Command{
Name: "start",
Usage: "starts the service on the OS",
Action: func(c *cli.Context) error {
return start(c)
},
}

// StopService stops the service on the OS
var StopService = cli.Command{
Name: "stop",
Usage: "stops the service on the OS",
Action: func(c *cli.Context) error {
return stop(c)
},
}

// RestartService stops and starts the service
var RestartService = cli.Command{
Name: "restart",
Usage: "restarts the service",
Action: func(c *cli.Context) error {
if err := stop(c); err != nil {
return err
}
return start(c)
},
}
59 changes: 59 additions & 0 deletions cmd/commands/service_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package commands

import (
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/kardianos/osext"
"github.com/urfave/cli"
)

const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>am-i-working</string>
<key>ProgramArguments</key>
<array>
<string>BINARY</string>
<string>watch</string>
<string>--domain</string>
<string>DOMAIN</string>
<string>--file</string>
<string>FILE</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>HOME/Library/Logs/am-i-working-out.log</string>
<key>StandardErrorPath</key>
<string>HOME/Library/Logs/am-i-working-out.log</string>
</dict>
</plist>
`

var plist = os.Getenv("HOME") + "/Library/LaunchAgents/am-i-working.plist"

func create(c *cli.Context) error {
executable, err := osext.Executable()
if err != nil {
return err
}
svc := strings.Replace(xml, "FILE", c.String("file"), -1)
svc = strings.Replace(svc, "HOME", os.Getenv("HOME"), -1)
svc = strings.Replace(svc, "DOMAIN", c.String("domain"), -1)
svc = strings.Replace(svc, "BINARY", executable, -1)
return ioutil.WriteFile(plist, []byte(svc), 0644)
}

func start(c *cli.Context) error {
return exec.Command("launchctl", "load", plist).Run()
}

func stop(c *cli.Context) error {
return exec.Command("launchctl", "unload", plist).Run()
}
19 changes: 19 additions & 0 deletions cmd/commands/service_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package commands

import (
"errors"

"github.com/urfave/cli"
)

func create(c *cli.Context) error {
return errors.New("not implemented")
}

func start(c *cli.Context) error {
return errors.New("not implemented")
}

func stop(c *cli.Context) error {
return errors.New("not implemented")
}
33 changes: 33 additions & 0 deletions cmd/commands/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"log"

working "github.com/caarlos0/am-i-working"
"github.com/urfave/cli"
)

// Watch action of the app
var Watch = cli.Command{
Name: "watch",
Usage: "Watch for domain changes",
Flags: flags,
Action: func(c *cli.Context) error {
events := make(chan bool)
domain := c.String("domain")
resolv := c.String("file")
log.Println("Watching", resolv, "for domain", domain)
go func() {
if err := working.Watch(resolv, domain, events); err != nil {
log.Fatal(err)
}
}()
for {
if <-events {
log.Println("Working")
} else {
log.Println("Not working")
}
}
},
}
23 changes: 10 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"os"

"github.com/caarlos0/am-i-working/cmd/actions"
"github.com/caarlos0/am-i-working/cmd/commands"
"github.com/urfave/cli"
)

Expand All @@ -17,17 +17,14 @@ func main() {
app.Version = version
app.Author = "Carlos Alexandro Becker <@caarlos0>"
app.Copyright = "MIT"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "domain, d",
Usage: "Domain name that appears in domain section of /etc/resolv.conf when you're connected to company networks",
},
cli.StringFlag{
Name: "file, f",
Usage: "File to watch for domain regexes",
Value: "/etc/resolv.conf",
},
app.Commands = []cli.Command{
commands.Watch,
commands.CreateService,
commands.RestartService,
commands.StartService,
commands.StopService,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
app.Action = actions.Main
log.Fatal(app.Run(os.Args))
}
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import:
version: ^1.18.0
- package: gopkg.in/fsnotify.v1
version: ^1.3.1
- package: github.com/kardianos/osext
testImport:
- package: github.com/stretchr/testify
version: ^1.1.3

0 comments on commit 93c0387

Please sign in to comment.