-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from caarlos0/improvements
WIP: start, stop et al
- Loading branch information
Showing
11 changed files
with
192 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
vendor | ||
am-i-working |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters