implement browse feature

This commit is contained in:
daveberrys committed 2026-07-15 20:14:50 +08:00
1 parent ed41c19bbf
commit 0c405b495f
5 files changed
+48 -8

No files matched your search

+5 -1
View File
@@ -2,9 +2,13 @@ module github.com/daveberrys/guh
go 1.26.3
require github.com/spf13/cobra v1.10.2
require (
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/spf13/cobra v1.10.2
)
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
golang.org/x/sys v0.1.0 // indirect
)
+4
View File
@@ -1,10 +1,14 @@
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+1
View File
@@ -4,6 +4,7 @@ import (
"github.com/daveberrys/guh/src/cmd"
_ "github.com/daveberrys/guh/src/cmd/create"
_ "github.com/daveberrys/guh/src/cmd/switch"
_ "github.com/daveberrys/guh/src/cmd/browse"
)
func main() { cmd.Execute() }
+25
View File
@@ -0,0 +1,25 @@
package browse
import (
"strings"
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
"github.com/pkg/browser"
)
func init() {
cmd.RootCmd.AddCommand(createCmd)
}
var createCmd = &cobra.Command{
Use: "browse",
Short: "Opens a browser to the repository.",
RunE: func(cmd *cobra.Command, args []string) error {
url, err := utils.RunGit(true, "remote", "get-url", "origin")
if err != nil { return err }
url = strings.TrimSuffix(url, ".git")
return browser.OpenURL(url)
},
}
+13 -7
View File
@@ -1,6 +1,7 @@
package utils
import (
"bytes"
"fmt"
"io"
"os"
@@ -8,23 +9,28 @@ import (
"strings"
)
func RunGit(hideOutput bool, args ...string) error {
func RunGit(hideOutput bool, args ...string) (string, error) {
gitCmd := exec.Command("git", args...)
gitCmd.Stdin = os.Stdin
var stdout bytes.Buffer
var stderr bytes.Buffer
if hideOutput {
gitCmd.Stdout = io.Discard
gitCmd.Stderr = io.Discard
gitCmd.Stdout = &stdout
gitCmd.Stderr = &stderr
} else {
gitCmd.Stdout = os.Stdout
gitCmd.Stderr = os.Stderr
gitCmd.Stdout = io.MultiWriter(os.Stdout, &stdout)
gitCmd.Stderr = io.MultiWriter(os.Stderr, &stderr)
}
return gitCmd.Run()
err := gitCmd.Run()
return strings.TrimRight(stdout.String(), "\n"), err
}
func RunGitSequence(hideOutput bool, commands ...[]string) error {
for _, commandArgs := range commands {
if err := RunGit(hideOutput, commandArgs...); err != nil {
if _, err := RunGit(hideOutput, commandArgs...); err != nil {
return fmt.Errorf("git %s failed: %w", strings.Join(commandArgs, " "), err)
}
}