func (cmd *CreateServiceKey) Execute(c flags.FlagContext) error { serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance() serviceKeyName := c.Args()[1] params := c.String("c") paramsMap, err := json.ParseJSONFromFileOrString(params) if err != nil { return errors.New(T("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object.")) } cmd.ui.Say(T("Creating service key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...", map[string]interface{}{ "ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name), "ServiceKeyName": terminal.EntityNameColor(serviceKeyName), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) err = cmd.serviceKeyRepo.CreateServiceKey(serviceInstance.GUID, serviceKeyName, paramsMap) switch err.(type) { case nil: cmd.ui.Ok() case *errors.ModelAlreadyExistsError: cmd.ui.Ok() cmd.ui.Warn(err.Error()) default: return err } return nil }
func (cmd *CreateService) Execute(c flags.FlagContext) error { serviceName := c.Args()[0] planName := c.Args()[1] serviceInstanceName := c.Args()[2] params := c.String("c") tags := c.String("t") tagsList := uihelpers.ParseTags(tags) paramsMap, err := json.ParseJSONFromFileOrString(params) if err != nil { return errors.New(T("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object.")) } cmd.ui.Say(T("Creating service instance {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "ServiceName": terminal.EntityNameColor(serviceInstanceName), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) plan, err := cmd.CreateService(serviceName, planName, serviceInstanceName, paramsMap, tagsList) switch err.(type) { case nil: err := printSuccessMessageForServiceInstance(serviceInstanceName, cmd.serviceRepo, cmd.ui) if err != nil { return err } if !plan.Free { cmd.ui.Say("") cmd.ui.Say(T("Attention: The plan `{{.PlanName}}` of service `{{.ServiceName}}` is not free. The instance `{{.ServiceInstanceName}}` will incur a cost. Contact your administrator if you think this is in error.", map[string]interface{}{ "PlanName": terminal.EntityNameColor(plan.Name), "ServiceName": terminal.EntityNameColor(serviceName), "ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName), })) cmd.ui.Say("") } case *errors.ModelAlreadyExistsError: cmd.ui.Ok() cmd.ui.Warn(err.Error()) default: return err } return nil }
func (cmd *BindService) Execute(c flags.FlagContext) error { app := cmd.appReq.GetApplication() serviceInstance := cmd.serviceInstanceReq.GetServiceInstance() params := c.String("c") paramsMap, err := json.ParseJSONFromFileOrString(params) if err != nil { return errors.New(T("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object.")) } cmd.ui.Say(T("Binding service {{.ServiceInstanceName}} to app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name), "AppName": terminal.EntityNameColor(app.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) err = cmd.BindApplication(app, serviceInstance, paramsMap) if err != nil { if httperr, ok := err.(errors.HTTPError); ok && httperr.ErrorCode() == errors.ServiceBindingAppServiceTaken { cmd.ui.Ok() cmd.ui.Warn(T("App {{.AppName}} is already bound to {{.ServiceName}}.", map[string]interface{}{ "AppName": app.Name, "ServiceName": serviceInstance.Name, })) return nil } return err } cmd.ui.Ok() cmd.ui.Say(T("TIP: Use '{{.CFCommand}} {{.AppName}}' to ensure your env variable changes take effect", map[string]interface{}{"CFCommand": terminal.CommandColor(cf.Name + " restage"), "AppName": app.Name})) return nil }
AfterEach(func() { tmpFile.Close() os.Remove(tmpFile.Name()) }) It("tries to convert the json file but fails because it was given something it didn't like", func() { _, err := json.ParseJSONArray(filename) Expect(err).To(MatchError("Incorrect json format: invalid character 'S' looking for beginning of value")) }) }) }) Describe("ParseJSONFromFileOrString", func() { Context("when the input is empty", func() { It("returns nil", func() { result, err := json.ParseJSONFromFileOrString("") Expect(result).To(BeNil()) Expect(err).To(BeNil()) }) }) Context("when the input is a file", func() { var jsonFile *os.File var fileContent string AfterEach(func() { if jsonFile != nil { jsonFile.Close() os.Remove(jsonFile.Name()) }