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