github.com/zaquestion/lab@v0.25.1/cmd/issue_browse.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/MakeNowJust/heredoc/v2"
     5  	"strconv"
     6  
     7  	"github.com/rsteube/carapace"
     8  	"github.com/spf13/cobra"
     9  	"github.com/zaquestion/lab/internal/action"
    10  	"github.com/zaquestion/lab/internal/gitlab"
    11  )
    12  
    13  var issueBrowseCmd = &cobra.Command{
    14  	Use:     "browse [remote] <id>",
    15  	Aliases: []string{"b"},
    16  	Short:   "View issue in a browser",
    17  	Example: heredoc.Doc(`
    18  		lab issue browse
    19  		lab issue browse upstream 22`),
    20  	PersistentPreRun: labPersistentPreRun,
    21  	Run: func(cmd *cobra.Command, args []string) {
    22  		rn, num, err := parseArgsRemoteAndID(args)
    23  		if err != nil {
    24  			log.Fatal(err)
    25  		}
    26  
    27  		project, err := gitlab.FindProject(rn)
    28  		if err != nil {
    29  			log.Fatal(err)
    30  		}
    31  
    32  		// path.Join will remove 1 "/" from "http://" as it's consider that's
    33  		// file system path. So we better use normal string concat
    34  		issueURL := project.WebURL + "/issues"
    35  		if num > 0 {
    36  			issueURL = issueURL + "/" + strconv.FormatInt(num, 10)
    37  		}
    38  
    39  		err = browse(issueURL)
    40  		if err != nil {
    41  			log.Fatal(err)
    42  		}
    43  	},
    44  }
    45  
    46  func init() {
    47  	issueCmd.AddCommand(issueBrowseCmd)
    48  	carapace.Gen(issueBrowseCmd).PositionalCompletion(
    49  		action.Remotes(),
    50  		action.Issues(issueList),
    51  	)
    52  }