browser-ruler/main.go

62 lines
1.3 KiB
Go
Raw Permalink Normal View History

2020-11-26 16:03:23 +00:00
package main
import (
"flag"
2021-09-28 23:21:56 +00:00
"fmt"
2020-11-26 16:03:23 +00:00
"net/url"
)
func handleURL(browserRules []BrowserRule, urlString string) error {
2020-11-26 16:03:23 +00:00
dest, err := url.Parse(urlString)
if err != nil {
2021-09-28 23:21:56 +00:00
return fmt.Errorf("failed to parse url: %s, %w", urlString, err)
2020-11-26 16:03:23 +00:00
}
var matched bool
for _, rule := range browserRules {
2020-11-26 16:03:23 +00:00
matched, err = rule.MaybeLaunch(*dest)
2021-09-28 23:21:56 +00:00
if err != nil {
return fmt.Errorf("failed launching browser: %w", err)
}
2020-11-26 16:03:23 +00:00
if matched {
break
}
}
return nil
}
func main() {
configPath := flag.String("config", "", "Path to config file, otherwise it will default to $XDG_CONFIG_HOME/browser-ruler/config.hcl or $HOME/.browser-ruler.hcl")
2020-11-26 16:03:23 +00:00
flag.Parse()
// Read config path from option or find default
if *configPath == "" {
defaultConfigPath, err := DefaultConfigPath()
if err != nil {
fmt.Println("Failed to get a path to any config files")
panic(err)
}
configPath = &defaultConfigPath
}
// Read rules from config path
browserRules, err := LoadConfig(*configPath)
if err != nil {
fmt.Printf("Could not read config from path %s\n", *configPath)
panic(err)
}
// Get urls from args
2020-11-26 16:03:23 +00:00
urls := flag.Args()
// For each url, run matcher
2020-11-26 16:03:23 +00:00
for _, urlString := range urls {
err := handleURL(browserRules, urlString)
2020-11-26 16:03:23 +00:00
if err != nil {
panic(err)
}
}
}