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

     1  package cmd
     2  
     3  import (
     4  	"github.com/MakeNowJust/heredoc/v2"
     5  	lab "github.com/zaquestion/lab/internal/gitlab"
     6  	"net/url"
     7  	"path"
     8  	"strconv"
     9  
    10  	"github.com/rsteube/carapace"
    11  	"github.com/spf13/cobra"
    12  	"github.com/zaquestion/lab/internal/action"
    13  )
    14  
    15  var snippetBrowseCmd = &cobra.Command{
    16  	Use:   "browse [remote] <id>",
    17  	Short: "View personal or project snippet in a browser",
    18  	Example: heredoc.Doc(`
    19  		lab snippet browse
    20  		lab snippet browse upstream`),
    21  	PersistentPreRun: labPersistentPreRun,
    22  	Run: func(cmd *cobra.Command, args []string) {
    23  		rn, id, err := parseArgsRemoteAndID(args)
    24  		if err != nil {
    25  			log.Fatal(err)
    26  		}
    27  
    28  		host := lab.Host()
    29  		hostURL, err := url.Parse(host)
    30  		if err != nil {
    31  			log.Fatal(err)
    32  		}
    33  
    34  		// See if we're in a git repo or if global is set to determine
    35  		// if this should be a personal snippet
    36  		if global || rn == "" {
    37  			hostURL.Path = path.Join(hostURL.Path, "dashboard", "snippets")
    38  		} else {
    39  			hostURL.Path = path.Join(hostURL.Path, rn, "snippets")
    40  		}
    41  
    42  		if id > 0 {
    43  			hostURL.Path = path.Join(hostURL.Path, strconv.FormatInt(id, 10))
    44  		}
    45  
    46  		err = browse(hostURL.String())
    47  		if err != nil {
    48  			log.Fatal(err)
    49  		}
    50  	},
    51  }
    52  
    53  func init() {
    54  	snippetCmd.AddCommand(snippetBrowseCmd)
    55  	carapace.Gen(snippetBrowseCmd).PositionalCompletion(
    56  		action.Remotes(),
    57  		action.Snippets(snippetList),
    58  	)
    59  }