From 67675f38f1d9d0d1599e16614b808d68e6fd83f9 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sun, 22 Nov 2026 08:18:23 +0100 Subject: feat(open): support AT "URIs" AT URIs are not valid URIs.[1] [1]: https://bnewbold.leaflet.pub/3mph4hzvbdc2v --- programs/open/main.go | 10 ++++++---- programs/open/main_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/programs/open/main.go b/programs/open/main.go index f5a9204..ed8a920 100644 --- a/programs/open/main.go +++ b/programs/open/main.go @@ -5,11 +5,11 @@ import ( "log" "maps" "mime" - "net/url" "os" "os/exec" "path" "path/filepath" + "regexp" "slices" "strings" @@ -21,6 +21,8 @@ type Config struct { UriSchemes map[string][]string `toml:"uri-schemes"` } +var uriSchemePattern = regexp.MustCompile(`^([A-Za-z][A-Za-z0-9+.-]*):`) + func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "usage: %s ", os.Args[0]) @@ -60,9 +62,9 @@ func main() { } func (c Config) findCommand(arg string) ([]string, error) { - u, err := url.Parse(arg) - if err == nil && u.Scheme != "" { - cmd, ok := c.UriSchemes[u.Scheme] + // Not using url.Parse since it expects a valid port after a colon which isn't the case for AT "URIs". + if match := uriSchemePattern.FindStringSubmatch(arg); match != nil { + cmd, ok := c.UriSchemes[strings.ToLower(match[1])] if ok { return cmd, nil } diff --git a/programs/open/main_test.go b/programs/open/main_test.go index 8f01766..7a30ec3 100644 --- a/programs/open/main_test.go +++ b/programs/open/main_test.go @@ -38,6 +38,23 @@ func TestFindCommand_Uri(t *testing.T) { } } +func TestFindCommand_ATUri(t *testing.T) { + cfg := Config{ + UriSchemes: map[string][]string{ + "at": {"open-at"}, + }, + } + + arg := "at://did:plc:example/app.bsky.feed.post/example" + cmd, err := cfg.findCommand(arg) + if err != nil { + t.Fatal(err) + } + if diff := cmp.Diff(cmd, cfg.UriSchemes["at"]); diff != "" { + t.Error("unexpected command", diff) + } +} + func TestFindCommand_MimeFile(t *testing.T) { err := os.Chdir(t.TempDir()) if err != nil { -- cgit v1.3.1