internal/control/difflayout.go

v1.40.1
gitbay/internal/control/difflayout.go history · blame · raw

49 lines · 1256 bytes

 1package control
 2
 3import (
 4	"fmt"
 5	"io"
 6
 7	"gitbay.org/gitbay/internal/protocol"
 8)
 9
10func init() {
11	register(Command{Path: []string{"web", "diff", "show"},
12		Summary:  "the diff layout the web UI uses for you",
13		Usage:    "web diff show",
14		Examples: []string{"web diff show"},
15		ReadOnly: true, Run: runWebDiffShow})
16	register(Command{Path: []string{"web", "diff", "set"},
17		Summary:  "draw web diffs unified or side by side",
18		Usage:    "web diff set unified|split",
19		Examples: []string{"web diff set split"}, Run: runWebDiffSet})
20}
21
22var diffLayouts = map[string]bool{"unified": true, "split": true}
23
24func runWebDiffShow(c *Ctx, args []string) int {
25	if len(args) != 0 {
26		return c.usage()
27	}
28	return emitDiffLayout(c)
29}
30
31func runWebDiffSet(c *Ctx, args []string) int {
32	if len(args) != 1 || !diffLayouts[args[0]] {
33		return c.usage()
34	}
35	if err := c.Store.SetDiffLayout(c.User.ID, args[0]); err != nil {
36		return c.fail(protocol.ExitFailure, "%v", err)
37	}
38	return emitDiffLayout(c)
39}
40
41func emitDiffLayout(c *Ctx) int {
42	layout, err := c.Store.DiffLayout(c.User.ID)
43	if err != nil {
44		return c.fail(protocol.ExitFailure, "%v", err)
45	}
46	return c.emit(map[string]string{"layout": layout}, func(w io.Writer) {
47		fmt.Fprintf(w, "layout: %s\n", layout)
48	})
49}