internal/control/snippet.go

9d96712a6988b5d376162e4f31c9ee3525e1ca9a
gitbay/internal/control/snippet.go history · blame · raw

380 lines · 11973 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; only they can change it", 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	f, err := parseFlags(args, flagSpec{Values: []string{"--description", "--visibility"}, MaxPos: 1, Usage: c.Cmd.Usage})
136	if err != nil {
137		return c.fail(protocol.ExitUsage, "%v", err)
138	}
139	name := f.pos(0)
140	if name == "" {
141		return c.usage()
142	}
143	if code := checkSnippetFileName(c, name); code >= 0 {
144		return code
145	}
146	visibility := f.Value("--visibility")
147	if visibility == "" {
148		visibility = "unlisted"
149	}
150	if !validSnippetVisibility(visibility) {
151		return c.fail(protocol.ExitUsage, "visibility is public, unlisted or private")
152	}
153	if limit := c.Cfg.Limits.MaxSnippetsPerUser; limit > 0 {
154		n, err := c.Store.CountSnippets(c.User.ID, true)
155		if err != nil {
156			return c.fail(protocol.ExitFailure, "%v", err)
157		}
158		if n >= limit {
159			return c.fail(protocol.ExitUsage, "snippet limit reached (%d); delete one first", limit)
160		}
161	}
162	data, code := readSnippetBody(c)
163	if code >= 0 {
164		return code
165	}
166	var pid string
167	for try := 0; ; try++ {
168		pid = newSnippetID()
169		_, err = c.Store.CreateSnippet(c.User.ID, pid, f.Value("--description"), visibility, name, data)
170		if !errors.Is(err, store.ErrExists) || try == 4 {
171			break
172		}
173	}
174	if err != nil {
175		return c.failErr(err)
176	}
177	sn, err := c.Store.SnippetByPublicID(pid)
178	if err != nil {
179		return c.fail(protocol.ExitFailure, "%v", err)
180	}
181	return c.emit(snippetOut(c, sn), func(w io.Writer) {
182		fmt.Fprintf(w, "created snippet %s\n%s\n", sn.PublicID, snippetURL(c, sn))
183	})
184}
185
186func runSnippetShow(c *Ctx, args []string) int {
187	if len(args) != 1 {
188		return c.usage()
189	}
190	sn, code := snippetRef(c, args[0], false)
191	if code >= 0 {
192		return code
193	}
194	files, err := c.Store.SnippetFiles(sn.ID)
195	if err != nil {
196		return c.fail(protocol.ExitFailure, "%v", err)
197	}
198	sn.Files = files
199	return c.emit(snippetOut(c, sn), func(w io.Writer) {
200		fmt.Fprintf(w, "snippet %s by %s (%s)\n", sn.PublicID, sn.OwnerName, sn.Visibility)
201		if sn.Description != "" {
202			fmt.Fprintf(w, "%s\n", sn.Description)
203		}
204		fmt.Fprintf(w, "%s\nupdated %s\n", snippetURL(c, sn), sn.UpdatedAt)
205		for _, f := range files {
206			fmt.Fprintf(w, "  %s\t%d bytes\n", f.Name, f.Size)
207		}
208	})
209}
210
211func runSnippetList(c *Ctx, args []string) int {
212	rest, p, code := parsePageFlags(c, args, "snippet", true)
213	if code >= 0 {
214		return code
215	}
216	if len(rest) > 1 {
217		return c.usage()
218	}
219	owner := c.User
220	if len(rest) == 1 {
221		u, err := c.Store.UserByUsername(rest[0])
222		if errors.Is(err, store.ErrNotFound) {
223			return c.fail(protocol.ExitNotFound, "no user %q", rest[0])
224		}
225		if err != nil {
226			return c.fail(protocol.ExitFailure, "%v", err)
227		}
228		owner = u
229	}
230	all := owner.ID == c.User.ID || c.User.IsAdmin
231	rows, err := c.Store.ListSnippets(owner.ID, all, p.queryLimit(), p.keyInt())
232	if err != nil {
233		return c.fail(protocol.ExitFailure, "%v", err)
234	}
235	rows, next := trimPage(p, rows, "snippet", func(sn store.Snippet) string { return strconv.FormatInt(sn.ID, 10) })
236	items := make([]SnippetOut, 0, len(rows))
237	for _, sn := range rows {
238		items = append(items, snippetOut(c, sn))
239	}
240	return c.emitPage(p, items, next, func(w io.Writer) {
241		for _, sn := range rows {
242			names := ""
243			for i, f := range sn.Files {
244				if i > 0 {
245					names += ", "
246				}
247				names += f.Name
248			}
249			fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", sn.PublicID, sn.Visibility, names, sn.Description)
250		}
251	})
252}
253
254func runSnippetEdit(c *Ctx, args []string) int {
255	f, err := parseFlags(args, flagSpec{Values: []string{"--description", "--visibility"}, MaxPos: 1, Usage: c.Cmd.Usage})
256	if err != nil {
257		return c.fail(protocol.ExitUsage, "%v", err)
258	}
259	if f.pos(0) == "" || (!f.Has("--description") && !f.Has("--visibility")) {
260		return c.usage()
261	}
262	sn, code := snippetRef(c, f.pos(0), true)
263	if code >= 0 {
264		return code
265	}
266	description, visibility := sn.Description, sn.Visibility
267	if f.Has("--description") {
268		description = f.Value("--description")
269	}
270	if f.Has("--visibility") {
271		visibility = f.Value("--visibility")
272		if !validSnippetVisibility(visibility) {
273			return c.fail(protocol.ExitUsage, "visibility is public, unlisted or private")
274		}
275	}
276	if err := c.Store.UpdateSnippet(sn.ID, description, visibility); err != nil {
277		return c.failErr(err)
278	}
279	sn, err = c.Store.SnippetByPublicID(sn.PublicID)
280	if err != nil {
281		return c.fail(protocol.ExitFailure, "%v", err)
282	}
283	return c.emit(snippetOut(c, sn), func(w io.Writer) {
284		fmt.Fprintf(w, "updated snippet %s (%s)\n", sn.PublicID, sn.Visibility)
285	})
286}
287
288func runSnippetDelete(c *Ctx, args []string) int {
289	if len(args) != 1 {
290		return c.usage()
291	}
292	sn, code := snippetRef(c, args[0], true)
293	if code >= 0 {
294		return code
295	}
296	if err := c.Store.DeleteSnippet(sn.ID); err != nil {
297		return c.failErr(err)
298	}
299	return c.emit(map[string]string{"id": sn.PublicID}, func(w io.Writer) {
300		fmt.Fprintf(w, "deleted snippet %s\n", sn.PublicID)
301	})
302}
303
304func runSnippetFileSet(c *Ctx, args []string) int {
305	if len(args) != 2 {
306		return c.usage()
307	}
308	sn, code := snippetRef(c, args[0], true)
309	if code >= 0 {
310		return code
311	}
312	name := args[1]
313	if code := checkSnippetFileName(c, name); code >= 0 {
314		return code
315	}
316	exists := false
317	for _, f := range sn.Files {
318		exists = exists || f.Name == name
319	}
320	if !exists && len(sn.Files) >= maxSnippetFiles {
321		return c.fail(protocol.ExitUsage, "a snippet holds at most %d files", maxSnippetFiles)
322	}
323	data, code := readSnippetBody(c)
324	if code >= 0 {
325		return code
326	}
327	if err := c.Store.SetSnippetFile(sn.ID, name, data); err != nil {
328		return c.failErr(err)
329	}
330	return c.emit(SnippetFileOut{Name: name, Size: int64(len(data))}, func(w io.Writer) {
331		fmt.Fprintf(w, "set %s (%d bytes) on snippet %s\n", name, len(data), sn.PublicID)
332	})
333}
334
335func runSnippetFileGet(c *Ctx, args []string) int {
336	if len(args) != 2 {
337		return c.usage()
338	}
339	sn, code := snippetRef(c, args[0], false)
340	if code >= 0 {
341		return code
342	}
343	f, err := c.Store.SnippetFile(sn.ID, args[1])
344	if errors.Is(err, store.ErrNotFound) {
345		return c.fail(protocol.ExitNotFound, "no file %q in snippet %s", args[1], sn.PublicID)
346	}
347	if err != nil {
348		return c.fail(protocol.ExitFailure, "%v", err)
349	}
350	if c.JSON {
351		return c.emit(SnippetFileOut{Name: f.Name, Size: f.Size, Content: string(f.Content)}, nil)
352	}
353	if _, err := c.Stdout.Write(f.Content); err != nil {
354		return protocol.ExitFailure
355	}
356	return protocol.ExitOK
357}
358
359func runSnippetFileRemove(c *Ctx, args []string) int {
360	if len(args) != 2 {
361		return c.usage()
362	}
363	sn, code := snippetRef(c, args[0], true)
364	if code >= 0 {
365		return code
366	}
367	if len(sn.Files) == 1 && sn.Files[0].Name == args[1] {
368		return c.fail(protocol.ExitUsage, "a snippet keeps at least one file; delete the snippet instead")
369	}
370	err := c.Store.RemoveSnippetFile(sn.ID, args[1])
371	if errors.Is(err, store.ErrNotFound) {
372		return c.fail(protocol.ExitNotFound, "no file %q in snippet %s", args[1], sn.PublicID)
373	}
374	if err != nil {
375		return c.failErr(err)
376	}
377	return c.emit(map[string]string{"id": sn.PublicID, "name": args[1]}, func(w io.Writer) {
378		fmt.Fprintf(w, "removed %s from snippet %s\n", args[1], sn.PublicID)
379	})
380}