github.com/ianfoo/lab@v0.9.5-0.20180123060006-5ed79f2ccfc7/cmd/snippetBrowse.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 "github.com/zaquestion/lab/internal/git" 12 ) 13 14 var snippetBrowseCmd = &cobra.Command{ 15 Use: "browse [remote] <id>", 16 Short: "View personal or project snippet in a browser", 17 Long: ``, 18 Run: func(cmd *cobra.Command, args []string) { 19 remote, id, err := parseArgsRemote(args) 20 if err != nil { 21 log.Fatal(err) 22 } 23 if remote == "" { 24 remote = forkedFromRemote 25 } 26 27 c := viper.AllSettings()["core"] 28 config := c.([]map[string]interface{})[0] 29 host := config["host"].(string) 30 hostURL, err := url.Parse(host) 31 if err != nil { 32 log.Fatal(err) 33 } 34 35 // See if we're in a git repo or if global is set to determine 36 // if this should be a personal snippet 37 rn, _ := git.PathWithNameSpace(remote) 38 if global || rn == "" { 39 hostURL.Path = path.Join(hostURL.Path, "dashboard", "snippets") 40 } else { 41 hostURL.Path = path.Join(hostURL.Path, rn, "snippets") 42 } 43 44 if id > 0 { 45 hostURL.Path = path.Join(hostURL.Path, strconv.FormatInt(id, 10)) 46 } 47 48 err = browse(hostURL.String()) 49 if err != nil { 50 log.Fatal(err) 51 } 52 }, 53 } 54 55 func init() { 56 snippetCmd.AddCommand(snippetBrowseCmd) 57 }