forked from zaquestion/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippetCreate.go
175 lines (160 loc) · 4.35 KB
/
snippetCreate.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"strconv"
"strings"
"text/template"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
var (
name string
file string
private bool
public bool
)
// mrCmd represents the mr command
var snippetCreateCmd = &cobra.Command{
Use: "create [remote]",
Short: "Create a personal or project snippet",
Long: `
Source snippets from stdin, file, or in editor from scratch
Optionally add a title & description with -m`,
Run: func(cmd *cobra.Command, args []string) {
msgs, err := cmd.Flags().GetStringSlice("message")
if err != nil {
log.Fatal(err)
}
remote := forkedFromRemote
if len(args) > 0 {
ok, err := git.IsRemote(args[0])
if err != nil {
log.Fatal(err)
}
if ok {
remote = args[0]
} else {
file = args[0]
}
if ok && len(args) > 1 {
file = args[1]
}
}
code, err := snipCode(file)
if err != nil {
log.Fatal(err)
}
if strings.TrimSpace(code) == "" {
log.Fatal("aborting snippet due to empty contents")
}
title, body := snipMsg(msgs)
if title == "" {
log.Fatal("aborting snippet due to empty msg")
}
visibility := gitlab.InternalVisibility
switch {
case private:
visibility = gitlab.PrivateVisibility
case public:
visibility = gitlab.PublicVisibility
}
// 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 == "" {
opts := gitlab.CreateSnippetOptions{
Title: gitlab.String(title),
Description: gitlab.String(body),
Content: gitlab.String(code),
FileName: gitlab.String(name),
Visibility: &visibility,
}
snip, err := lab.SnippetCreate(&opts)
if err != nil || snip == nil {
log.Fatal(errors.Wrap(err, "failed to create snippet"))
}
fmt.Println(snip.WebURL)
return
}
project, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}
opts := gitlab.CreateProjectSnippetOptions{
Title: gitlab.String(title),
Description: gitlab.String(body),
Code: gitlab.String(code),
FileName: gitlab.String(name),
Visibility: &visibility,
}
snip, err := lab.ProjectSnippetCreate(project.ID, &opts)
if err != nil || snip == nil {
log.Fatal(errors.Wrap(err, "failed to create snippet"))
}
fmt.Println(snip.WebURL)
},
}
func snipMsg(msgs []string) (string, string) {
return msgs[0], strings.Join(msgs[1:], "\n\n")
}
func snipCode(path string) (string, error) {
b, err := ioutil.ReadFile(path)
if !os.IsNotExist(err) && err != nil {
return "", err
}
if len(b) > 0 {
return string(b), nil
}
if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0 {
b, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return "", err
}
if len(b) > 0 {
return string(b), nil
}
}
var tmpl = `
{{.CommentChar}} In this mode you are writing a snippet from scratch
{{.CommentChar}} The first block is the title and the rest is the contents.`
text, err := snipText(tmpl)
if err != nil {
log.Fatal(err)
}
title, body, err := git.Edit("SNIPCODE", text)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
}
return fmt.Sprintf("%s\n\n%s", title, body), nil
}
func snipText(tmpl string) (string, error) {
t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return "", err
}
cc := git.CommentChar()
msg := &struct{ CommentChar string }{CommentChar: cc}
var b bytes.Buffer
err = t.Execute(&b, msg)
if err != nil {
return "", err
}
return b.String(), nil
}
func init() {
snippetCreateCmd.Flags().BoolVarP(&private, "private", "p", false, "Make snippet private; visible only to project members (default: internal)")
snippetCreateCmd.Flags().BoolVar(&public, "public", false, "Make snippet public; can be accessed without any authentication (default: internal)")
snippetCreateCmd.Flags().StringVarP(&name, "name", "n", "", "(optional) Name snippet to add code highlighting, e.g. potato.go for GoLang")
snippetCreateCmd.Flags().StringSliceP("message", "m", []string{"-"}, "Use the given <msg>; multiple -m are concatenated as seperate paragraphs")
snippetCmd.Flags().AddFlagSet(snippetCreateCmd.Flags())
snippetCmd.AddCommand(snippetCreateCmd)
}