/
schemas.go
88 lines (75 loc) · 2.05 KB
/
schemas.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
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/stevedomin/termtable"
"strings"
)
func CmdSchemas(c *Config) *cobra.Command {
return &cobra.Command{
Use: "schemas",
Short: "Manage post schemas.",
Long: `
List, set or remove post schemas.
Schemas keep you from typing post types over and over by mapping them to short names.
This simple feature might get expanded when strict post schemas get introduced with Tent 0.4.
Post types can optionally be saved with fragments (e.g. "schemas add status https://tent.io/types/status/v0#reply"). These extensions can be overwritten when used: "create status#different_fragment".
`,
Run: func(cmd *cobra.Command, args []string) {
t := termtable.NewTable(nil, nil)
t.SetHeader([]string{"NAME", "POSTTYPE"})
for _, s := range c.Schemas {
t.AddRow([]string{s.Name, s.PostType})
}
fmt.Println(t.Render())
},
}
}
func CmdSchemasSet(c *Config) *cobra.Command {
return &cobra.Command{
Use: "set <schema_name> <post_type>",
Short: "Define a post schema.",
Long: "Add a new or overwrite an existing post schema.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
showHelpAndExit(cmd)
}
name := args[0]
postType := args[1]
if strings.Contains(name, "#") {
exitWithError(`Schema names can't contain "#".`)
}
if i, s := c.SchemaByName(name); i > -1 {
// schema already exists
s.Name = name
s.PostType = postType
} else {
c.Schemas = append(c.Schemas, SchemaConfig{
Name: name,
PostType: postType,
})
}
err := c.Write()
maybeExit(err)
},
}
}
func CmdSchemasRemove(c *Config) *cobra.Command {
return &cobra.Command{
Use: "remove <schema_name>",
Short: "Remove a post schema.",
Long: "Remove a schema by its name.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
showHelpAndExit(cmd)
}
i, _ := c.SchemaByName(args[0])
if i == -1 {
return
}
c.Schemas = append(c.Schemas[:i], c.Schemas[i+1:]...)
err := c.Write()
maybeExit(err)
},
}
}