Update and refactor

This commit is contained in:
IamTheFij 2021-09-28 16:21:56 -07:00
parent 655ecf7f6d
commit 1e9b9d9685
5 changed files with 128 additions and 58 deletions

View File

@ -8,7 +8,9 @@ This is tested on an Ubuntu system but should work on anything that supports xdg
There is no configuration, persay. Intead, the code must be updated.
Edit `main.go` and edit the list of `BrowserRules` in a similar fashion as the example and then run `make install` again.
Edit `rules.go` and edit the list of `BrowserRules` in a similar fashion as the example and then run `make install` again.
You can use any rule functions found in `main.go` and any browsers found in `browsers.go`.
There is also a Python version as `browser_ruler.py`. To use that instead, you can edit that and then update the Makefile to make the `BIN` variable `BIN = browser_ruler.py`.

60
browsers.go Normal file
View File

@ -0,0 +1,60 @@
package main
func Firefox(matcher MatchFunc) BrowserRule {
return BrowserRule{
command: "firefox",
args: nil,
matcher: matcher,
}
}
func FirefoxWindow(matcher MatchFunc) BrowserRule {
return BrowserRule{
matcher: matcher,
command: "firefox",
args: []string{"--new-window"},
}
}
func FirefoxPrivate(matcher MatchFunc) BrowserRule {
return BrowserRule{
matcher: matcher,
command: "firefox",
args: []string{"--private-window"},
}
}
func Chromium(matcher MatchFunc) BrowserRule {
return BrowserRule{
command: "chromium-browser",
args: nil,
matcher: matcher,
}
}
func ChromiumWindow(matcher MatchFunc) BrowserRule {
return BrowserRule{
command: "chromium-browser",
args: []string{"--new-window"},
matcher: matcher,
}
}
func ChromiumIncognito(matcher MatchFunc) BrowserRule {
return BrowserRule{
command: "chromium-browser",
args: []string{"--incognito"},
matcher: matcher,
}
}
func ChromiumFlatpak(matcher MatchFunc) BrowserRule {
return BrowserRule{
command: "flatpak",
args: []string{
"run",
"org.chromium.Chromium",
},
matcher: matcher,
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.iamthefij.com/iamthefij/browser-ruler
go 1.16

66
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"net/url"
"os/exec"
"regexp"
@ -99,68 +100,19 @@ func noop(dest url.URL) bool {
return false
}
// Rules to evaluate
var rules = []BrowserRule{
// Personal domains
BrowserRule{
matcher: matchHostRegexp(
`.*\.iamthefij\.com$`,
`.*\.thefij\.rocks$`,
`.*\.thefij$`,
),
command: "firefox",
},
// Work domains
BrowserRule{
matcher: matchAny(
matchHostname(
"app.signalfx.com",
"lever.co",
"work.grubhub.com",
"y",
"yelp.rimeto.io",
"yelp.slack.com",
"yelplove.appspot.com",
),
matchHostRegexp(
`.*\.lifesize\.com$`,
`.*\.myworkday\.com$`,
`.*\.salesforce\.com$`,
`.*\.yelpcorp\.com$`,
),
),
command: "chromium-browser",
},
// Googly domains
BrowserRule{
matcher: matchAny(
matchHostname(
"google.com",
"youtube.com",
),
matchHostRegexp(
`.*\.google\.com$`,
`.*\.youtube\.com$`,
),
),
command: "chromium-browser",
},
// Default fallback browser
BrowserRule{
matcher: defaultBrowser,
command: "firefox",
},
}
func handleUrl(urlString string) error {
func handleURL(urlString string) error {
dest, err := url.Parse(urlString)
if err != nil {
return err
return fmt.Errorf("failed to parse url: %s, %w", urlString, err)
}
var matched bool
for _, rule := range rules {
for _, rule := range rules() {
matched, err = rule.MaybeLaunch(*dest)
if err != nil {
return fmt.Errorf("failed launching browser: %w", err)
}
if matched {
break
}
@ -174,7 +126,7 @@ func main() {
urls := flag.Args()
for _, urlString := range urls {
err := handleUrl(urlString)
err := handleURL(urlString)
if err != nil {
panic(err)
}

53
rules.go Normal file
View File

@ -0,0 +1,53 @@
package main
// Update this file with your rules
// You can either define a BrowserRule directly, or using one of the
// pre-defined browsers in browsers.go
func rules() []BrowserRule {
return []BrowserRule{
// Personal domains
Firefox(
matchHostRegexp(
`.*\.iamthefij\.com$`,
`.*\.thefij\.rocks$`,
`.*\.thefij$`,
),
),
// Work domains
ChromiumFlatpak(
matchAny(
matchHostname(
"app.signalfx.com",
"lever.co",
"work.grubhub.com",
"y",
"yelp.rimeto.io",
"yelp.slack.com",
"yelplove.appspot.com",
),
matchHostRegexp(
`.*\.lifesize\.com$`,
`.*\.myworkday\.com$`,
`.*\.salesforce\.com$`,
`.*\.yelpcorp\.com$`,
),
),
),
// Googly domains
ChromiumFlatpak(
matchAny(
matchHostname(
"google.com",
"youtube.com",
),
matchHostRegexp(
`.*\.google\.com$`,
`.*\.youtube\.com$`,
),
),
),
// Default fallback browser
Firefox(defaultBrowser),
}
}