Skip to content

Commit feaadea

Browse files
Create omctl command-line interface for openmock
Use omctl to post templates from a local directory at a remote openmock instance. Have post templates job use actual openmock to load templates, to take advantage of all features When loading a payload / body from file, delete the 'FromFile' field so that only the loaded body is saved Add some info to the README about the admin interface and om_post_templatse use log.Fatalf, remove Exit() Update README.md Co-Authored-By: Vlad Zelmanov <51313481+vzvzv@users.noreply.github.com> Update README.md Co-Authored-By: Vlad Zelmanov <51313481+vzvzv@users.noreply.github.com> EPLT-866: create 'omctl' interface with cobra library instead of stand-alone post-templates script Remove post_templates, move url flag to directory, change default templates to demo_templates Update README Remove noun from CLI - just 'omctl update' Update readme Code review comments
1 parent 49edbe6 commit feaadea

File tree

243 files changed

+59299
-9827
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

243 files changed

+59299
-9827
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
1313
/om
14-
/cmd/om/om
14+
/omctl

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FROM alpine:3.6
77
WORKDIR /go/src/github.com/checkr/openmock
88
RUN apk add --no-cache ca-certificates libc6-compat
99
COPY --from=builder /go/src/github.com/checkr/openmock/om ./om
10+
COPY --from=builder /go/src/github.com/checkr/openmock/omctl ./omctl
1011
ENV OPENMOCK_HTTP_HOST=0.0.0.0
1112
ENV OPENMOCK_TEMPLATES_DIR=/data/templates
1213
CMD ./om

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ vendor:
55
@GO111MODULE=on go mod tidy
66
@GO111MODULE=on go mod vendor
77

8-
build:
8+
build: build_om build_omctl
9+
10+
build_om:
911
@GO111MODULE=on go build -mod=vendor -o $(PWD)/om github.com/checkr/openmock/cmd/om
1012

13+
build_omctl:
14+
@GO111MODULE=on go build -mod=vendor -o $(PWD)/omctl github.com/checkr/openmock/cmd/omctl
15+
1116
test: lint
1217
@GO111MODULE=on go test -mod=vendor -race -covermode=atomic .
1318

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ OpenMock leverages [https://golang.org/pkg/text/template/](https://golang.org/pk
288288
{{.HTTPBody | xmlPath "node1/node2/node3"}}
289289
```
290290

291+
## Command Line Interface
292+
Openmock has a command-line interface to help with certain tasks interacting with openmock instances. This is
293+
invoked with the `omctl` command. This uses the [cobra](https://github.com/spf13/cobra) library to provide a discoverable CLI; run `omctl` for a list of commands / flags.
294+
295+
### CLI: Directory
296+
#### Push
297+
Pushes a local openmock model from the file system to a remote instance.
298+
```
299+
# Adds templates from the ./demo_templates directory to the instance running on localhost.
300+
omctl push --directory ./demo_templates --url http://localhost:9998
301+
```
302+
291303
### Example: Mock HTTP
292304
```yaml
293305
# demo_templates/http.yaml

cmd/omctl/cmd/root.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cmd
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
// RootCmd represents the base command when called without any subcommands
9+
var RootCmd = &cobra.Command{
10+
Use: "omctl",
11+
Short: "CLI for openmock",
12+
SilenceUsage: true,
13+
Long: `
14+
A simple CLI for interacting with openmock.
15+
`,
16+
}
17+
18+
// Execute adds all child commands to the root command and sets flags appropriately.
19+
// This is called by main.main(). It only needs to happen once to the rootCmd.
20+
func Execute() {
21+
if err := RootCmd.Execute(); err != nil {
22+
logrus.Fatal(err)
23+
}
24+
}
25+
26+
// local openmock directory
27+
var localDirectory string
28+
29+
// URL of remote openmock instance to control
30+
var openMockURL string
31+
32+
func init() {
33+
RootCmd.PersistentFlags().StringVarP(&localDirectory, "directory", "d", "./demo_templates", "Local directory in filesystem to upload to remote openmock")
34+
RootCmd.PersistentFlags().StringVarP(&openMockURL, "url", "u", "http://localhost:9998", "URL for remote openmock instance to control")
35+
}

cmd/omctl/cmd/update.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/checkr/openmock"
9+
"github.com/sirupsen/logrus"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var pushCmd = &cobra.Command{
14+
Use: "push",
15+
Short: "push local openmock model to remote instance",
16+
Long: "push local openmock model to remote instance",
17+
Args: cobra.MaximumNArgs(1),
18+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
19+
// check if remote instance reachable
20+
response, httpErr := http.Get(openMockURL + "/api/v1/templates")
21+
if httpErr != nil || response == nil {
22+
return fmt.Errorf("Remote openmock instance not reachable %s: [%s]", openMockURL, httpErr)
23+
}
24+
if response.StatusCode != 200 {
25+
return fmt.Errorf("Remote openmock instance not reachable %s: [%d]", openMockURL, response.StatusCode)
26+
}
27+
response.Body.Close()
28+
return nil
29+
},
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
// load local templates
32+
localOpenMock := openmock.OpenMock{}
33+
localOpenMock.ParseEnv()
34+
localOpenMock.TemplatesDir = localDirectory
35+
36+
localOpenMock.SetupLogrus()
37+
localOpenMock.SetupRepo()
38+
39+
err := localOpenMock.Load()
40+
if err != nil {
41+
logrus.Errorf("%s: %s", "failed to load yaml templates for mocks", err)
42+
return err
43+
}
44+
45+
// post the loaded templates at the openmock instance
46+
templatesYaml := localOpenMock.ToYAML()
47+
response, httpErr := http.Post(openMockURL+"/api/v1/templates", "application/yaml", bytes.NewReader(templatesYaml))
48+
if httpErr != nil {
49+
logrus.Errorf("Error posting templates at %s: [%s]", openMockURL, httpErr)
50+
return httpErr
51+
}
52+
response.Body.Close()
53+
54+
logrus.Info("Posted templates!")
55+
return nil
56+
},
57+
}
58+
59+
func init() {
60+
RootCmd.AddCommand(pushCmd)
61+
}

cmd/omctl/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"github.com/checkr/openmock/cmd/omctl/cmd"
5+
)
6+
7+
func main() {
8+
cmd.Execute()
9+
}

go.mod

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ require (
1616
github.com/bsm/sarama-cluster v2.1.15+incompatible
1717
github.com/caarlos0/env v3.3.0+incompatible
1818
github.com/dafiti/echo-middleware v0.0.0-20180423194757-e57a87d075ea
19-
github.com/davecgh/go-spew v1.1.1 // indirect
20-
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
2119
github.com/eapache/go-resiliency v1.1.0 // indirect
2220
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
2321
github.com/eapache/queue v1.1.0 // indirect
@@ -39,19 +37,18 @@ require (
3937
github.com/onsi/gomega v1.5.0 // indirect
4038
github.com/parnurzeal/gorequest v0.0.0-20171015110455-8e3aed27fe49
4139
github.com/pierrec/lz4 v2.0.3+incompatible // indirect
42-
github.com/pmezard/go-difflib v1.0.0 // indirect
4340
github.com/prashantv/gostub v1.0.0
4441
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 // indirect
45-
github.com/sirupsen/logrus v1.0.6
42+
github.com/sirupsen/logrus v1.2.0
4643
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
47-
github.com/spf13/cast v1.2.0
44+
github.com/spf13/cast v1.3.0
45+
github.com/spf13/cobra v0.0.5
46+
github.com/spf13/viper v1.5.0
4847
github.com/streadway/amqp v0.0.0-20180806233856-70e15c650864
4948
github.com/stretchr/testify v1.2.2
5049
github.com/teamwork/reload v0.0.0-20190319183701-e8d47ccac39e
5150
github.com/valyala/bytebufferpool v1.0.0 // indirect
5251
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect
5352
github.com/yuin/gopher-lua v0.0.0-20180827083657-b942cacc89fe // indirect
54-
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
55-
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
56-
gopkg.in/yaml.v2 v2.2.1
53+
gopkg.in/yaml.v2 v2.2.4
5754
)

0 commit comments

Comments
 (0)