Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add git appraise web subcommand #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ var CommandMap = map[string]*Command{
"request": requestCmd,
"show": showCmd,
"submit": submitCmd,
"web": webCmd,
}
74 changes: 74 additions & 0 deletions commands/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package commands

import (
"flag"
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"sync"

"github.com/google/git-appraise/repository"
)

var webFlagSet = flag.NewFlagSet("web", flag.ExitOnError)

var (
webPort = webFlagSet.Int("port", 12345, "Port to run git-appraise-web.")
)

func openBrowser(url string) error {
var err error

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err
}

func openWeb(args []string) error {
webFlagSet.Parse(args)

bin, err := exec.LookPath("git-appraise-web")
if err != nil {
return err
}

cmd := exec.Command(bin, "-port", strconv.Itoa(*webPort))
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

var wg sync.WaitGroup
wg.Add(1)
go func() {
fmt.Printf("starting %s on http://localhost:%d\n", bin, *webPort)
err = cmd.Run()
wg.Done()
}()

if err = openBrowser(fmt.Sprintf("http://localhost:%d", *webPort)); err != nil {
return err
}

wg.Wait()
return err
}

// rejectCmd defines the "reject" subcommand.
var webCmd = &Command{
Usage: func(arg0 string) {
fmt.Printf("Usage: %s web [<option>...]\n\nOptions:\n", arg0)
webFlagSet.PrintDefaults()
},
RunMethod: func(_ repository.Repo, args []string) error {
return openWeb(args)
},
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/google/git-appraise

go 1.14