func createCommand(args []string, options cmds.Options) (cmds.Command, error) { if len(args) < 2 { return nil, errors.New("No bmp command specified") } currentUser, err := user.Current() if err != nil { return nil, err } configPath := filepath.Join(currentUser.HomeDir, config.CONFIG_FILE_NAME) _, err = os.Stat(configPath) if os.IsNotExist(err) { if args[1] != "target" { return nil, errors.New("Please set bmp target firstly.") } else { err = ioutil.WriteFile(configPath, []byte("{}"), 0666) if err != nil { return nil, err } } } bmpClient, err := common.CreateBmpClient() if err != nil { return nil, errors.New(fmt.Sprintf("Could not create BMP client: %s", err.Error())) } cmd := createCommands(options, bmpClient)[args[1]] if cmd == nil { return nil, errors.New(fmt.Sprintf("Invalid command: %s", args[1])) } return cmd, nil }
Context("when current user has a .bmp_config", func() { BeforeEach(func() { _, err = ioutil.ReadFile(configFileName) if err != nil { configContents := []byte(`{ "Username": "", "Password": "", "TargetUrl": "" }`) err = ioutil.WriteFile(configFileName, configContents, 0666) Expect(err).NotTo(HaveOccurred()) } }) It("creates a BMP client", func() { bmpClient, err := common.CreateBmpClient() Expect(err).NotTo(HaveOccurred()) Expect(bmpClient).ToNot(BeNil()) }) }) Context("when current user does not have a .bmp_config", func() { var ( tmpFileName string err error ) BeforeEach(func() { _, err = os.Stat(configFileName) if os.IsNotExist(err) == false { tmpFile, err := ioutil.TempFile("", ".bmp_config")