Skip to content

Commit b8436e9

Browse files
committed
'$ bob www' for opening project homepage / GitHub
1 parent 3239c3f commit b8436e9

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

cmd/bob/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func main() {
5151
app.AddCommand(infoEntry())
5252
app.AddCommand(workspaceEntry())
5353

54+
app.AddCommand(openProjectHomepageEntrypoint())
55+
5456
app.AddCommand(toolsEntry()) // namespace for less often needed tools
5557
} else {
5658
app.AddCommand(buildInsideEntry())

cmd/bob/projecthomepage.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
"os"
8+
"regexp"
9+
10+
"github.com/function61/gokit/os/osutil"
11+
"github.com/pkg/browser"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func openProjectHomepageEntrypoint() *cobra.Command {
16+
return &cobra.Command{
17+
Use: "www",
18+
Aliases: []string{"gh"},
19+
Short: "Open project homepage / GitHub repo in browser",
20+
Hidden: true,
21+
Args: cobra.NoArgs,
22+
Run: func(_ *cobra.Command, _ []string) {
23+
osutil.ExitIfError(func() error {
24+
repo, err := gitHubRepoRefFromGit()
25+
if err != nil {
26+
return err
27+
}
28+
29+
// not interested in browser output
30+
browser.Stdout = io.Discard
31+
browser.Stderr = io.Discard
32+
33+
return browser.OpenURL(githubURL(*repo))
34+
}())
35+
},
36+
}
37+
}
38+
39+
func gitHubRepoRefFromGit() (*githubRepoRef, error) {
40+
conf, err := os.ReadFile(".git/config")
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
// dirty
46+
originParseRe := regexp.MustCompile(`url = [email protected]:(.+)/(.+).git`)
47+
48+
matches := originParseRe.FindStringSubmatch(string(conf))
49+
if matches == nil {
50+
return nil, errors.New("unable to resolve GitHub organization/repo name")
51+
}
52+
53+
org, repo := matches[1], matches[2]
54+
55+
return &githubRepoRef{org, repo}, nil
56+
}
57+
58+
type githubRepoRef struct {
59+
org string
60+
repo string
61+
}
62+
63+
func githubURL(repo githubRepoRef) string {
64+
return fmt.Sprintf("https://github.com/%s/%s", repo.org, repo.repo)
65+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.13
55
require (
66
github.com/function61/gokit v0.0.0-20220129122644-a8596b4bad8c
77
github.com/mattn/go-runewidth v0.0.13 // indirect
8+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
89
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4
910
github.com/spf13/cobra v1.3.0
1011
go.i3wm.org/i3/v4 v4.18.0

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
292292
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
293293
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
294294
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
295+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
296+
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
295297
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
296298
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
297299
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
@@ -546,6 +548,7 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
546548
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
547549
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
548550
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
551+
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
549552
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
550553
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
551554
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)