-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathjson.go
87 lines (69 loc) · 1.71 KB
/
json.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
package cmd
import (
"errors"
"github.com/caalberts/localroast/http"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strings"
)
type jsonCommand struct {
*cobra.Command
fileHandler fileHandler
parser parser
serverFunc http.ServerFunc
}
func (c *jsonCommand) getCommand() *cobra.Command {
return c.Command
}
func newJSONCmd(fileHandler fileHandler, parser parser, serverFunc http.ServerFunc) commander {
jsonCmd := &jsonCommand{
fileHandler: fileHandler,
parser: parser,
serverFunc: serverFunc,
}
command := &cobra.Command{
Use: "json",
Short: "Use localroast with json file (default)",
Long: `A tool to help developers stub external HTTP services quickly.
See https://github.com/caalberts/localroast/examples/stubs.json
for examples.`,
Args: validateJSONArgs,
Example: "localroast json examples/stubs.json",
RunE: jsonCmd.execute,
}
jsonCmd.Command = command
return jsonCmd
}
func (c *jsonCommand) execute(cmd *cobra.Command, args []string) error {
port, err := cmd.Flags().GetString("port")
if err != nil {
return err
}
filepath := args[0]
err = c.fileHandler.Open(filepath)
if err != nil {
return err
}
err = c.fileHandler.Watch()
if err != nil {
return err
}
server := c.serverFunc(port)
c.parser.Watch(c.fileHandler.Output())
server.Watch(c.parser.Output())
log.Info("brewing on port " + port)
return server.ListenAndServe()
}
func validateJSONArgs(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("a file is required")
}
if len(args) > 1 {
return errors.New("expected 1 argument")
}
file := args[0]
if !strings.HasSuffix(file, ".json") {
return errors.New("input must be a JSON file")
}
return nil
}