-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: open Signed-off-by: Carlos Alexandro Becker <[email protected]> * chore: go 1.18 Signed-off-by: Carlos Alexandro Becker <[email protected]> * build: fix go.work --------- Signed-off-by: Carlos Alexandro Becker <[email protected]>
- Loading branch information
Showing
13 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# auto-generated by scripts/dependabot. DO NOT EDIT. | ||
name: open | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
paths: | ||
- exp/open/** | ||
- .github/workflows/open.yml | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
runs-on: ${{ matrix.os }} | ||
defaults: | ||
run: | ||
working-directory: ./exp/open | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: ./exp/open/go.mod | ||
cache: true | ||
cache-dependency-path: ./exp/open.sum | ||
- run: go build -v ./... | ||
- run: go test -race -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
!go.work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/charmbracelet/x/exp/open" | ||
) | ||
|
||
func main() { | ||
fmt.Println(open.Open(context.Background(), "https://charm.sh")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/charmbracelet/x/exp/open | ||
|
||
go 1.18 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package open | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// ErrNotSupported occurs when no ways to open a file are found. | ||
var ErrNotSupported = errors.New("not supported") | ||
|
||
// Open the given input. | ||
func Open(ctx context.Context, input string) error { | ||
return With(ctx, "", input) | ||
} | ||
|
||
// With opens the given input using the given app. | ||
func With(ctx context.Context, app, input string) error { | ||
cmd := buildCmd(ctx, app, input) | ||
if cmd == nil { | ||
return ErrNotSupported | ||
} | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("open: %w: %s", err, string(out)) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package open | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
) | ||
|
||
func buildCmd(ctx context.Context, app, path string) *exec.Cmd { | ||
if _, err := exec.LookPath("open"); err == nil { | ||
var arg []string | ||
if app != "" { | ||
arg = append(arg, "-a", app) | ||
} | ||
arg = append(arg, path) | ||
return exec.CommandContext(ctx, "open", arg...) | ||
} | ||
|
||
if _, err := exec.LookPath("xdg-open"); err == nil { | ||
if app == "" { | ||
return exec.CommandContext(ctx, app, path) | ||
} | ||
return exec.CommandContext(ctx, "xdg-open", path) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package open | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
) | ||
|
||
func buildCmd(ctx context.Context, app, path string) *exec.Cmd { | ||
if app != "" { | ||
return exec.Command("cmd", "/C", "start", "", app, path) | ||
} | ||
return exec.CommandContext(ctx, "rundll32", "url.dll,FileProtocolHandler", path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters