commands: add flags/config to control the automatic opening in the default browser

Michael Muré created

Change summary

commands/webui.go            | 55 ++++++++++++++++++++++++++++---------
doc/man/git-bug-webui.1      | 14 ++++++++
doc/md/git-bug.md            |  2 
doc/md/git-bug_ls-id.md      |  4 +-
doc/md/git-bug_webui.md      |  8 ++++
misc/bash_completion/git-bug | 33 +++++++++++++++++++++-
misc/zsh_completion/git-bug  |  2 
7 files changed, 96 insertions(+), 22 deletions(-)

Detailed changes

commands/webui.go 🔗

@@ -13,28 +13,35 @@ import (
 	"time"
 
 	"github.com/99designs/gqlgen/handler"
-	"github.com/MichaelMure/git-bug/graphql"
-	"github.com/MichaelMure/git-bug/repository"
-	"github.com/MichaelMure/git-bug/util/git"
-	"github.com/MichaelMure/git-bug/webui"
 	"github.com/gorilla/mux"
 	"github.com/phayes/freeport"
 	"github.com/skratchdot/open-golang/open"
 	"github.com/spf13/cobra"
+
+	"github.com/MichaelMure/git-bug/graphql"
+	"github.com/MichaelMure/git-bug/repository"
+	"github.com/MichaelMure/git-bug/util/git"
+	"github.com/MichaelMure/git-bug/webui"
+)
+
+var (
+	webUIPort   int
+	webUIOpen   bool
+	webUINoOpen bool
 )
 
-var port int
+const webUIOpenConfigKey = "git-bug.webui.open"
 
 func runWebUI(cmd *cobra.Command, args []string) error {
-	if port == 0 {
+	if webUIPort == 0 {
 		var err error
-		port, err = freeport.GetFreePort()
+		webUIPort, err = freeport.GetFreePort()
 		if err != nil {
 			return err
 		}
 	}
 
-	addr := fmt.Sprintf("127.0.0.1:%d", port)
+	addr := fmt.Sprintf("127.0.0.1:%d", webUIPort)
 	webUiAddr := fmt.Sprintf("http://%s", addr)
 
 	router := mux.NewRouter()
@@ -93,9 +100,21 @@ func runWebUI(cmd *cobra.Command, args []string) error {
 	fmt.Printf("Graphql Playground: http://%s/playground\n", addr)
 	fmt.Println("Press Ctrl+c to quit")
 
-	err = open.Run(webUiAddr)
-	if err != nil {
-		fmt.Println(err)
+	configOpen, err := repo.ReadConfigBool(webUIOpenConfigKey)
+	if err == repository.ErrNoConfigEntry {
+		// default to true
+		configOpen = true
+	} else if err != nil {
+		return err
+	}
+
+	shouldOpen := (configOpen && !webUINoOpen) || webUIOpen
+
+	if shouldOpen {
+		err = open.Run(webUiAddr)
+		if err != nil {
+			fmt.Println(err)
+		}
 	}
 
 	err = srv.ListenAndServe()
@@ -223,8 +242,13 @@ func (gufh *gitUploadFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Requ
 }
 
 var webUICmd = &cobra.Command{
-	Use:     "webui",
-	Short:   "Launch the web UI.",
+	Use:   "webui",
+	Short: "Launch the web UI.",
+	Long: `Launch the web UI.
+
+Available git config:
+  git-bug.webui.open [bool]: control the automatic opening of the web UI in the default browser
+`,
 	PreRunE: loadRepo,
 	RunE:    runWebUI,
 }
@@ -234,5 +258,8 @@ func init() {
 
 	webUICmd.Flags().SortFlags = false
 
-	webUICmd.Flags().IntVarP(&port, "port", "p", 0, "Port to listen to")
+	webUICmd.Flags().BoolVar(&webUIOpen, "open", false, "Automatically open the web UI in the default browser")
+	webUICmd.Flags().BoolVar(&webUINoOpen, "no-open", false, "Prevent the automatic opening of the web UI in the default browser")
+	webUICmd.Flags().IntVarP(&webUIPort, "port", "p", 0, "Port to listen to (default is random)")
+
 }

doc/man/git-bug-webui.1 🔗

@@ -17,11 +17,23 @@ git\-bug\-webui \- Launch the web UI.
 .PP
 Launch the web UI.
 
+.PP
+Available git config:
+  git\-bug.webui.open [bool]: control the automatic opening of the web UI in the default browser
+
 
 .SH OPTIONS
+.PP
+\fB\-\-open\fP[=false]
+    Automatically open the web UI in the default browser
+
+.PP
+\fB\-\-no\-open\fP[=false]
+    Prevent the automatic opening of the web UI in the default browser
+
 .PP
 \fB\-p\fP, \fB\-\-port\fP=0
-    Port to listen to
+    Port to listen to (default is random)
 
 .PP
 \fB\-h\fP, \fB\-\-help\fP[=false]

doc/md/git-bug.md 🔗

@@ -31,7 +31,7 @@ git-bug [flags]
 * [git-bug deselect](git-bug_deselect.md)	 - Clear the implicitly selected bug.
 * [git-bug label](git-bug_label.md)	 - Display, add or remove labels to/from a bug.
 * [git-bug ls](git-bug_ls.md)	 - List bugs.
-* [git-bug ls-id](git-bug_ls-id.md)	 - List Bug Id
+* [git-bug ls-id](git-bug_ls-id.md)	 - List bug identifiers.
 * [git-bug ls-label](git-bug_ls-label.md)	 - List valid labels.
 * [git-bug pull](git-bug_pull.md)	 - Pull bugs update from a git remote.
 * [git-bug push](git-bug_push.md)	 - Push bugs update to a git remote.

doc/md/git-bug_ls-id.md 🔗

@@ -1,10 +1,10 @@
 ## git-bug ls-id
 
-List Bug Id
+List bug identifiers.
 
 ### Synopsis
 
-List Bug Id
+List bug identifiers.
 
 ```
 git-bug ls-id [<prefix>] [flags]

doc/md/git-bug_webui.md 🔗

@@ -6,6 +6,10 @@ Launch the web UI.
 
 Launch the web UI.
 
+Available git config:
+  git-bug.webui.open [bool]: control the automatic opening of the web UI in the default browser
+
+
 ```
 git-bug webui [flags]
 ```
@@ -13,7 +17,9 @@ git-bug webui [flags]
 ### Options
 
 ```
-  -p, --port int   Port to listen to
+      --open       Automatically open the web UI in the default browser
+      --no-open    Prevent the automatic opening of the web UI in the default browser
+  -p, --port int   Port to listen to (default is random)
   -h, --help       help for webui
 ```
 

misc/bash_completion/git-bug 🔗

@@ -107,7 +107,13 @@ __git-bug_handle_reply()
     fi
 
     if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
-        declare -F __custom_func >/dev/null && __custom_func
+		if declare -F __git-bug_custom_func >/dev/null; then
+			# try command name qualified custom func
+			__git-bug_custom_func
+		else
+			# otherwise fall back to unqualified for compatibility
+			declare -F __custom_func >/dev/null && __custom_func
+		fi
     fi
 
     # available in bash-completion >= 2, not always present on macOS
@@ -171,7 +177,8 @@ __git-bug_handle_flag()
     fi
 
     # skip the argument to a two word flag
-    if __git-bug_contains_word "${words[c]}" "${two_word_flags[@]}"; then
+    if [[ ${words[c]} != *"="* ]] && __git-bug_contains_word "${words[c]}" "${two_word_flags[@]}"; then
+			  __git-bug_debug "${FUNCNAME[0]}: found a flag ${words[c]}, skip the next argument"
         c=$((c+1))
         # if we are looking for a flags value, don't show commands
         if [[ $c -eq $cword ]]; then
@@ -263,12 +270,15 @@ _git-bug_add()
     flags_completion=()
 
     flags+=("--title=")
+    two_word_flags+=("--title")
     two_word_flags+=("-t")
     local_nonpersistent_flags+=("--title=")
     flags+=("--message=")
+    two_word_flags+=("--message")
     two_word_flags+=("-m")
     local_nonpersistent_flags+=("--message=")
     flags+=("--file=")
+    two_word_flags+=("--file")
     two_word_flags+=("-F")
     local_nonpersistent_flags+=("--file=")
 
@@ -398,9 +408,11 @@ _git-bug_comment_add()
     flags_completion=()
 
     flags+=("--file=")
+    two_word_flags+=("--file")
     two_word_flags+=("-F")
     local_nonpersistent_flags+=("--file=")
     flags+=("--message=")
+    two_word_flags+=("--message")
     two_word_flags+=("-m")
     local_nonpersistent_flags+=("--message=")
 
@@ -527,30 +539,39 @@ _git-bug_ls()
     flags_completion=()
 
     flags+=("--status=")
+    two_word_flags+=("--status")
     two_word_flags+=("-s")
     local_nonpersistent_flags+=("--status=")
     flags+=("--author=")
+    two_word_flags+=("--author")
     two_word_flags+=("-a")
     local_nonpersistent_flags+=("--author=")
     flags+=("--participant=")
+    two_word_flags+=("--participant")
     two_word_flags+=("-p")
     local_nonpersistent_flags+=("--participant=")
     flags+=("--actor=")
+    two_word_flags+=("--actor")
     two_word_flags+=("-A")
     local_nonpersistent_flags+=("--actor=")
     flags+=("--label=")
+    two_word_flags+=("--label")
     two_word_flags+=("-l")
     local_nonpersistent_flags+=("--label=")
     flags+=("--title=")
+    two_word_flags+=("--title")
     two_word_flags+=("-t")
     local_nonpersistent_flags+=("--title=")
     flags+=("--no=")
+    two_word_flags+=("--no")
     two_word_flags+=("-n")
     local_nonpersistent_flags+=("--no=")
     flags+=("--by=")
+    two_word_flags+=("--by")
     two_word_flags+=("-b")
     local_nonpersistent_flags+=("--by=")
     flags+=("--direction=")
+    two_word_flags+=("--direction")
     two_word_flags+=("-d")
     local_nonpersistent_flags+=("--direction=")
 
@@ -674,6 +695,7 @@ _git-bug_show()
     flags_completion=()
 
     flags+=("--field=")
+    two_word_flags+=("--field")
     two_word_flags+=("-f")
     local_nonpersistent_flags+=("--field=")
 
@@ -779,6 +801,7 @@ _git-bug_title_edit()
     flags_completion=()
 
     flags+=("--title=")
+    two_word_flags+=("--title")
     two_word_flags+=("-t")
     local_nonpersistent_flags+=("--title=")
 
@@ -886,6 +909,7 @@ _git-bug_user()
     flags_completion=()
 
     flags+=("--field=")
+    two_word_flags+=("--field")
     two_word_flags+=("-f")
     local_nonpersistent_flags+=("--field=")
 
@@ -937,7 +961,12 @@ _git-bug_webui()
     flags_with_completion=()
     flags_completion=()
 
+    flags+=("--open")
+    local_nonpersistent_flags+=("--open")
+    flags+=("--no-open")
+    local_nonpersistent_flags+=("--no-open")
     flags+=("--port=")
+    two_word_flags+=("--port")
     two_word_flags+=("-p")
     local_nonpersistent_flags+=("--port=")
 

misc/zsh_completion/git-bug 🔗

@@ -8,7 +8,7 @@ case $state in
   level1)
     case $words[1] in
       git-bug)
-        _arguments '1: :(add bridge commands comment deselect label ls ls-id ls-label pull push select show status termui title user version webui)'
+        _arguments '1: :(add bridge commands comment deselect export label ls ls-id ls-label pull push select show status termui title user version webui)'
       ;;
       *)
         _arguments '*: :_files'