|
| 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 | +} |
0 commit comments