diff options
| author | Martin Fischer <martin@push-f.com> | 2026-11-22 08:18:23 +0100 |
|---|---|---|
| committer | Martin Fischer <martin@push-f.com> | 2026-12-02 07:43:14 +0100 |
| commit | 67675f38f1d9d0d1599e16614b808d68e6fd83f9 (patch) | |
| tree | 20abde2aa7e8c55d184e52c52ecc58ced134de06 | |
| parent | dc6b8f5ba8298f3ad1f04eee1f2c82183b9ed28c (diff) | |
feat(open): support AT "URIs"main
AT URIs are not valid URIs.[1]
[1]: https://bnewbold.leaflet.pub/3mph4hzvbdc2v
| -rw-r--r-- | programs/open/main.go | 10 | ||||
| -rw-r--r-- | 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 <file-or-uri>", 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 { |
