github.com/matthewdale/lab@v0.14.0/cmd/snippet_browse.go (about)

     1  package cmd
     2  
     3  import (
     4  	"log"
     5  	"net/url"
     6  	"path"
     7  	"strconv"
     8  
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/viper"
    11  )
    12  
    13  var snippetBrowseCmd = &cobra.Command{
    14  	Use:   "browse [remote] <id>",
    15  	Short: "View personal or project snippet in a browser",
    16  	Long:  ``,
    17  	Run: func(cmd *cobra.Command, args []string) {
    18  		rn, id, err := parseArgs(args)
    19  		if err != nil {
    20  			log.Fatal(err)
    21  		}
    22  
    23  		c := viper.AllSettings()["core"]
    24  		config := c.([]map[string]interface{})[0]
    25  		host := config["host"].(string)
    26  		hostURL, err := url.Parse(host)
    27  		if err != nil {
    28  			log.Fatal(err)
    29  		}
    30  
    31  		// See if we're in a git repo or if global is set to determine
    32  		// if this should be a personal snippet
    33  		if global || rn == "" {
    34  			hostURL.Path = path.Join(hostURL.Path, "dashboard", "snippets")
    35  		} else {
    36  			hostURL.Path = path.Join(hostURL.Path, rn, "snippets")
    37  		}
    38  
    39  		if id > 0 {
    40  			hostURL.Path = path.Join(hostURL.Path, strconv.FormatInt(id, 10))
    41  		}
    42  
    43  		err = browse(hostURL.String())
    44  		if err != nil {
    45  			log.Fatal(err)
    46  		}
    47  	},
    48  }
    49  
    50  func init() {
    51  	snippetCmd.AddCommand(snippetBrowseCmd)
    52  }