internal/control/snippet.go

aa03e8c8e02357cfe5f94188fdab3c79846cebae
gitbay/internal/control/snippet.go history · blame · raw

373 lines · 12267 bytes

  1package control
  2
  3import (
  4	"crypto/rand"
  5	"encoding/hex"
  6	"errors"
  7	"fmt"
  8	"io"
  9	"strconv"
 10	"unicode/utf8"
 11
 12	"gitbay.org/gitbay/internal/policy"
 13	"gitbay.org/gitbay/internal/protocol"
 14	"gitbay.org/gitbay/internal/store"
 15)
 16
 17// A snippet keeps at most this many files; a paste is not a repository.
 18const maxSnippetFiles = 64
 19
 20func init() {
 21	register(Command{Path: []string{"snippet", "create"},
 22		Summary:    "create a snippet from one file on stdin",
 23		Usage:      "snippet create <filename> [--description <d>] [--visibility public|unlisted|private] < file",
 24		ReadsStdin: true, Run: runSnippetCreate})
 25	register(Command{Path: []string{"snippet", "show"},
 26		Summary: "show a snippet's metadata and files",
 27		Usage:   "snippet show <id>", ReadOnly: true, Run: runSnippetShow})
 28	register(Command{Path: []string{"snippet", "list"},
 29		Summary: "list your snippets, or an owner's public ones",
 30		Usage:   "snippet list [<owner>] [--limit n] [--cursor c]", ReadOnly: true, Run: runSnippetList})
 31	register(Command{Path: []string{"snippet", "edit"},
 32		Summary: "change a snippet's description or visibility",
 33		Usage:   "snippet edit <id> [--description <d>] [--visibility public|unlisted|private]", Run: runSnippetEdit})
 34	register(Command{Path: []string{"snippet", "delete"},
 35		Summary: "delete a snippet and its files",
 36		Usage:   "snippet delete <id>", Run: runSnippetDelete})
 37	register(Command{Path: []string{"snippet", "file", "set"},
 38		Summary:    "add a file to a snippet, or replace one, from stdin",
 39		Usage:      "snippet file set <id> <filename> < file",
 40		ReadsStdin: true, Run: runSnippetFileSet})
 41	register(Command{Path: []string{"snippet", "file", "get"},
 42		Summary: "write a snippet file to stdout",
 43		Usage:   "snippet file get <id> <filename> > file", ReadOnly: true, Run: runSnippetFileGet})
 44	register(Command{Path: []string{"snippet", "file", "remove"},
 45		Summary: "remove a file from a snippet",
 46		Usage:   "snippet file remove <id> <filename>", Run: runSnippetFileRemove})
 47}
 48
 49type SnippetFileOut struct {
 50	Name    string `json:"name"`
 51	Size    int64  `json:"size"`
 52	Content string `json:"content,omitempty"`
 53}
 54
 55type SnippetOut struct {
 56	ID          string           `json:"id"`
 57	URL         string           `json:"url"`
 58	Owner       string           `json:"owner"`
 59	Description string           `json:"description"`
 60	Visibility  string           `json:"visibility"`
 61	CreatedAt   string           `json:"created_at"`
 62	UpdatedAt   string           `json:"updated_at"`
 63	Files       []SnippetFileOut `json:"files"`
 64}
 65
 66func snippetURL(c *Ctx, sn store.Snippet) string {
 67	return c.Cfg.Server.SiteURL + "/" + sn.OwnerName + "/-/snippets/" + sn.PublicID
 68}
 69
 70func snippetOut(c *Ctx, sn store.Snippet) SnippetOut {
 71	o := SnippetOut{ID: sn.PublicID, URL: snippetURL(c, sn), Owner: sn.OwnerName,
 72		Description: sn.Description, Visibility: sn.Visibility,
 73		CreatedAt: sn.CreatedAt, UpdatedAt: sn.UpdatedAt, Files: []SnippetFileOut{}}
 74	for _, f := range sn.Files {
 75		o.Files = append(o.Files, SnippetFileOut{Name: f.Name, Size: f.Size, Content: string(f.Content)})
 76	}
 77	return o
 78}
 79
 80func validSnippetVisibility(v string) bool {
 81	return v == "public" || v == "unlisted" || v == "private"
 82}
 83
 84// snippetRef loads a snippet the caller may read; with write, one they
 85// may change. Unreadable and missing are the same not-found, so a
 86// private id cannot be confirmed by probing.
 87func snippetRef(c *Ctx, id string, write bool) (store.Snippet, int) {
 88	sn, err := c.Store.SnippetByPublicID(id)
 89	if err != nil && !errors.Is(err, store.ErrNotFound) {
 90		return sn, c.fail(protocol.ExitFailure, "%v", err)
 91	}
 92	if err != nil || !policy.CanReadSnippet(c.User, sn) {
 93		return sn, c.fail(protocol.ExitNotFound, "no snippet %q", id)
 94	}
 95	if write && !policy.CanWriteSnippet(c.User, sn) {
 96		return sn, c.fail(protocol.ExitDenied, "snippet %s belongs to %s", id, sn.OwnerName)
 97	}
 98	return sn, -1
 99}
100
101// readSnippetBody reads one file from stdin under the limit, and insists
102// on text: the page highlights it and the raw route serves text/plain.
103func readSnippetBody(c *Ctx) ([]byte, int) {
104	limit := c.Cfg.Limits.MaxSnippetBytes
105	data, err := io.ReadAll(io.LimitReader(c.Stdin, limit+1))
106	if err != nil {
107		return nil, c.fail(protocol.ExitFailure, "reading stdin: %v", err)
108	}
109	if int64(len(data)) > limit {
110		return nil, c.fail(protocol.ExitUsage, "file exceeds max_snippet_bytes (%d)", limit)
111	}
112	if len(data) == 0 {
113		return nil, c.fail(protocol.ExitUsage, "empty file: pipe it on stdin")
114	}
115	if !utf8.Valid(data) {
116		return nil, c.fail(protocol.ExitUsage, "snippets hold text: the file is not valid UTF-8")
117	}
118	return data, -1
119}
120
121func checkSnippetFileName(c *Ctx, name string) int {
122	if !assetNamePat.MatchString(name) {
123		return c.fail(protocol.ExitUsage, "invalid file name %q: letters, digits, '._+-'; must not start with '.'", name)
124	}
125	return -1
126}
127
128func newSnippetID() string {
129	buf := make([]byte, 6)
130	rand.Read(buf)
131	return hex.EncodeToString(buf)
132}
133
134func runSnippetCreate(c *Ctx, args []string) int {
135	const usage = "usage: snippet create <filename> [--description <d>] [--visibility public|unlisted|private] < file"
136	f, err := parseFlags(args, flagSpec{Values: []string{"--description", "--visibility"}, MaxPos: 1, Usage: usage})
137	if err != nil {
138		return c.fail(protocol.ExitUsage, "%v", err)
139	}
140	name := f.pos(0)
141	if name == "" {
142		return c.fail(protocol.ExitUsage, usage)
143	}
144	if code := checkSnippetFileName(c, name); code >= 0 {
145		return code
146	}
147	visibility := f.Value("--visibility")
148	if visibility == "" {
149		visibility = "unlisted"
150	}
151	if !validSnippetVisibility(visibility) {
152		return c.fail(protocol.ExitUsage, "visibility is public, unlisted or private")
153	}
154	data, code := readSnippetBody(c)
155	if code >= 0 {
156		return code
157	}
158	var pid string
159	for try := 0; ; try++ {
160		pid = newSnippetID()
161		_, err = c.Store.CreateSnippet(c.User.ID, pid, f.Value("--description"), visibility, name, data)
162		if !errors.Is(err, store.ErrExists) || try == 4 {
163			break
164		}
165	}
166	if err != nil {
167		return c.failErr(err)
168	}
169	sn, err := c.Store.SnippetByPublicID(pid)
170	if err != nil {
171		return c.fail(protocol.ExitFailure, "%v", err)
172	}
173	return c.emit(snippetOut(c, sn), func(w io.Writer) {
174		fmt.Fprintf(w, "created snippet %s\n%s\n", sn.PublicID, snippetURL(c, sn))
175	})
176}
177
178func runSnippetShow(c *Ctx, args []string) int {
179	if len(args) != 1 {
180		return c.fail(protocol.ExitUsage, "usage: snippet show <id>")
181	}
182	sn, code := snippetRef(c, args[0], false)
183	if code >= 0 {
184		return code
185	}
186	files, err := c.Store.SnippetFiles(sn.ID)
187	if err != nil {
188		return c.fail(protocol.ExitFailure, "%v", err)
189	}
190	sn.Files = files
191	return c.emit(snippetOut(c, sn), func(w io.Writer) {
192		fmt.Fprintf(w, "snippet %s by %s (%s)\n", sn.PublicID, sn.OwnerName, sn.Visibility)
193		if sn.Description != "" {
194			fmt.Fprintf(w, "%s\n", sn.Description)
195		}
196		fmt.Fprintf(w, "%s\nupdated %s\n", snippetURL(c, sn), sn.UpdatedAt)
197		for _, f := range files {
198			fmt.Fprintf(w, "  %s\t%d bytes\n", f.Name, f.Size)
199		}
200	})
201}
202
203func runSnippetList(c *Ctx, args []string) int {
204	rest, p, code := parsePageFlags(c, args, "snippet", true)
205	if code >= 0 {
206		return code
207	}
208	if len(rest) > 1 {
209		return c.fail(protocol.ExitUsage, "usage: snippet list [<owner>] [--limit n] [--cursor c]")
210	}
211	owner := c.User
212	if len(rest) == 1 {
213		u, err := c.Store.UserByUsername(rest[0])
214		if errors.Is(err, store.ErrNotFound) {
215			return c.fail(protocol.ExitNotFound, "no user %q", rest[0])
216		}
217		if err != nil {
218			return c.fail(protocol.ExitFailure, "%v", err)
219		}
220		owner = u
221	}
222	all := owner.ID == c.User.ID || c.User.IsAdmin
223	rows, err := c.Store.ListSnippets(owner.ID, all, p.queryLimit(), p.keyInt())
224	if err != nil {
225		return c.fail(protocol.ExitFailure, "%v", err)
226	}
227	rows, next := trimPage(p, rows, "snippet", func(sn store.Snippet) string { return strconv.FormatInt(sn.ID, 10) })
228	items := make([]SnippetOut, 0, len(rows))
229	for _, sn := range rows {
230		items = append(items, snippetOut(c, sn))
231	}
232	return c.emitPage(p, items, next, func(w io.Writer) {
233		for _, sn := range rows {
234			names := ""
235			for i, f := range sn.Files {
236				if i > 0 {
237					names += ", "
238				}
239				names += f.Name
240			}
241			fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", sn.PublicID, sn.Visibility, names, sn.Description)
242		}
243	})
244}
245
246func runSnippetEdit(c *Ctx, args []string) int {
247	const usage = "usage: snippet edit <id> [--description <d>] [--visibility public|unlisted|private]"
248	f, err := parseFlags(args, flagSpec{Values: []string{"--description", "--visibility"}, MaxPos: 1, Usage: usage})
249	if err != nil {
250		return c.fail(protocol.ExitUsage, "%v", err)
251	}
252	if f.pos(0) == "" || (!f.Has("--description") && !f.Has("--visibility")) {
253		return c.fail(protocol.ExitUsage, usage)
254	}
255	sn, code := snippetRef(c, f.pos(0), true)
256	if code >= 0 {
257		return code
258	}
259	description, visibility := sn.Description, sn.Visibility
260	if f.Has("--description") {
261		description = f.Value("--description")
262	}
263	if f.Has("--visibility") {
264		visibility = f.Value("--visibility")
265		if !validSnippetVisibility(visibility) {
266			return c.fail(protocol.ExitUsage, "visibility is public, unlisted or private")
267		}
268	}
269	if err := c.Store.UpdateSnippet(sn.ID, description, visibility); err != nil {
270		return c.failErr(err)
271	}
272	sn, err = c.Store.SnippetByPublicID(sn.PublicID)
273	if err != nil {
274		return c.fail(protocol.ExitFailure, "%v", err)
275	}
276	return c.emit(snippetOut(c, sn), func(w io.Writer) {
277		fmt.Fprintf(w, "updated snippet %s (%s)\n", sn.PublicID, sn.Visibility)
278	})
279}
280
281func runSnippetDelete(c *Ctx, args []string) int {
282	if len(args) != 1 {
283		return c.fail(protocol.ExitUsage, "usage: snippet delete <id>")
284	}
285	sn, code := snippetRef(c, args[0], true)
286	if code >= 0 {
287		return code
288	}
289	if err := c.Store.DeleteSnippet(sn.ID); err != nil {
290		return c.failErr(err)
291	}
292	return c.emit(map[string]string{"id": sn.PublicID}, func(w io.Writer) {
293		fmt.Fprintf(w, "deleted snippet %s\n", sn.PublicID)
294	})
295}
296
297func runSnippetFileSet(c *Ctx, args []string) int {
298	if len(args) != 2 {
299		return c.fail(protocol.ExitUsage, "usage: snippet file set <id> <filename> < file")
300	}
301	sn, code := snippetRef(c, args[0], true)
302	if code >= 0 {
303		return code
304	}
305	name := args[1]
306	if code := checkSnippetFileName(c, name); code >= 0 {
307		return code
308	}
309	exists := false
310	for _, f := range sn.Files {
311		exists = exists || f.Name == name
312	}
313	if !exists && len(sn.Files) >= maxSnippetFiles {
314		return c.fail(protocol.ExitUsage, "a snippet holds at most %d files", maxSnippetFiles)
315	}
316	data, code := readSnippetBody(c)
317	if code >= 0 {
318		return code
319	}
320	if err := c.Store.SetSnippetFile(sn.ID, name, data); err != nil {
321		return c.failErr(err)
322	}
323	return c.emit(SnippetFileOut{Name: name, Size: int64(len(data))}, func(w io.Writer) {
324		fmt.Fprintf(w, "set %s (%d bytes) on snippet %s\n", name, len(data), sn.PublicID)
325	})
326}
327
328func runSnippetFileGet(c *Ctx, args []string) int {
329	if len(args) != 2 {
330		return c.fail(protocol.ExitUsage, "usage: snippet file get <id> <filename> > file")
331	}
332	sn, code := snippetRef(c, args[0], false)
333	if code >= 0 {
334		return code
335	}
336	f, err := c.Store.SnippetFile(sn.ID, args[1])
337	if errors.Is(err, store.ErrNotFound) {
338		return c.fail(protocol.ExitNotFound, "no file %q in snippet %s", args[1], sn.PublicID)
339	}
340	if err != nil {
341		return c.fail(protocol.ExitFailure, "%v", err)
342	}
343	if c.JSON {
344		return c.emit(SnippetFileOut{Name: f.Name, Size: f.Size, Content: string(f.Content)}, nil)
345	}
346	if _, err := c.Stdout.Write(f.Content); err != nil {
347		return protocol.ExitFailure
348	}
349	return protocol.ExitOK
350}
351
352func runSnippetFileRemove(c *Ctx, args []string) int {
353	if len(args) != 2 {
354		return c.fail(protocol.ExitUsage, "usage: snippet file remove <id> <filename>")
355	}
356	sn, code := snippetRef(c, args[0], true)
357	if code >= 0 {
358		return code
359	}
360	if len(sn.Files) == 1 && sn.Files[0].Name == args[1] {
361		return c.fail(protocol.ExitUsage, "a snippet keeps at least one file; delete the snippet instead")
362	}
363	err := c.Store.RemoveSnippetFile(sn.ID, args[1])
364	if errors.Is(err, store.ErrNotFound) {
365		return c.fail(protocol.ExitNotFound, "no file %q in snippet %s", args[1], sn.PublicID)
366	}
367	if err != nil {
368		return c.failErr(err)
369	}
370	return c.emit(map[string]string{"id": sn.PublicID, "name": args[1]}, func(w io.Writer) {
371		fmt.Fprintf(w, "removed %s from snippet %s\n", args[1], sn.PublicID)
372	})
373}