From 1e9b9d96851f8d61828fd029c718e1617df334dd Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 28 Sep 2021 16:21:56 -0700 Subject: [PATCH] Update and refactor --- README.md | 4 +++- browsers.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ main.go | 66 ++++++++--------------------------------------------- rules.go | 53 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 58 deletions(-) create mode 100644 browsers.go create mode 100644 go.mod create mode 100644 rules.go diff --git a/README.md b/README.md index 6b098f7..a1fffa1 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/browsers.go b/browsers.go new file mode 100644 index 0000000..4c84a7b --- /dev/null +++ b/browsers.go @@ -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, + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e2cd197 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.iamthefij.com/iamthefij/browser-ruler + +go 1.16 diff --git a/main.go b/main.go index 111f77a..9f24385 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/rules.go b/rules.go new file mode 100644 index 0000000..0417c1a --- /dev/null +++ b/rules.go @@ -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), + } +}