Exemplo n.º 1
0
func (l Login) Run(c *cli.Context) {
	l.ui.Say("target: %s", term.Cyan(l.config.Target))

	var email string
	if len(c.Args()) > 0 {
		email = c.Args()[0]
	} else {
		email = l.ui.Ask("Username%s", term.Cyan(">"))
	}

	for i := 0; i < maxLoginTries; i++ {
		password := l.ui.AskForPassword("Password%s", term.Cyan(">"))
		l.ui.Say("Authenticating...")

		err := l.authenticator.Authenticate(email, password)

		if err != nil {
			l.ui.Failed("Error Authenticating", err)
			continue
		}

		l.ui.Ok()

		organizations, err := l.orgRepo.FindAll()

		if err != nil {
			l.ui.Failed("Error fetching organizations.", err)
			return
		}

		if len(organizations) == 0 {
			l.ui.Say("No orgs found. Use 'cf create-organization' as an Administrator.")
			return
		}

		l.targetOrganization(l.config, organizations)

		spaces, err := l.spaceRepo.FindAll()

		if err != nil {
			l.ui.Failed("Error fetching spaces.", err)
			return
		}

		if len(spaces) == 0 {
			l.ui.ShowConfiguration(l.config)
			l.ui.Say("No spaces found. Use 'cf create-space' as an Org Manager.")
			return
		}

		l.targetSpace(l.config, spaces)
		l.ui.ShowConfiguration(l.config)

		return
	}
}
Exemplo n.º 2
0
func (cmd *UnbindService) Run(c *cli.Context) {
	app := cmd.appReq.GetApplication()
	instance := cmd.serviceInstanceReq.GetServiceInstance()

	cmd.ui.Say("Unbinding service %s from %s...", term.Cyan(instance.Name), term.Cyan(app.Name))

	err := cmd.serviceRepo.UnbindService(instance, app)
	if err != nil {
		cmd.ui.Failed("Failed unbinding service", err)
		return
	}

	cmd.ui.Ok()
}
Exemplo n.º 3
0
func (cmd CreateService) createService(name string, offeringName string, planName string) {
	offerings, err := cmd.serviceRepo.GetServiceOfferings()
	if err != nil {
		cmd.ui.Failed("Error fetching offerings", err)
		return
	}

	offering, err := findOffering(offerings, offeringName)
	if err != nil {
		cmd.ui.Failed("Offering not found", nil)
		return
	}

	plan, err := findPlan(offering.Plans, planName)
	if err != nil {
		cmd.ui.Failed("Plan not found", nil)
		return
	}

	cmd.ui.Say("Creating service %s", term.Cyan(name))
	err = cmd.serviceRepo.CreateServiceInstance(name, plan)
	if err != nil {
		cmd.ui.Failed("Error creating plan", err)
		return
	}

	cmd.ui.Ok()
	return
}
Exemplo n.º 4
0
func (cmd *BindService) Run(c *cli.Context) {
	app := cmd.appReq.GetApplication()
	instance := cmd.serviceInstanceReq.GetServiceInstance()

	cmd.ui.Say("Binding service %s to %s...", term.Cyan(instance.Name), term.Cyan(app.Name))

	errorCode, err := cmd.serviceRepo.BindService(instance, app)
	if err != nil && errorCode != 90003 {
		cmd.ui.Failed("Failed binding service", err)
		return
	}

	cmd.ui.Ok()

	if errorCode == 90003 {
		cmd.ui.Say("App %s is already bound to %s.", term.Cyan(app.Name), term.Cyan(instance.Name))
	}
}
Exemplo n.º 5
0
func (cmd *DeleteService) Run(c *cli.Context) {
	instance := cmd.serviceInstanceReq.GetServiceInstance()
	cmd.ui.Say("Deleting service %s...", term.Cyan(instance.Name))

	err := cmd.serviceRepo.DeleteService(instance)
	if err != nil {
		cmd.ui.Failed("Failed deleting service", err)
		return
	}

	cmd.ui.Ok()
}
Exemplo n.º 6
0
func (l Login) targetSpace(config *configuration.Configuration, spaces []cf.Space) {
	if len(spaces) == 1 {
		l.saveSpace(config, spaces[0])
	} else {
		selectedSpace := l.chooseSpace(spaces)
		l.ui.Say("Targeting space %s...", term.Cyan(selectedSpace.Name))
		err := l.saveSpace(config, selectedSpace)

		if err == nil {
			l.ui.Ok()
		}
	}
}
Exemplo n.º 7
0
func (l Login) chooseSpace(spaces []cf.Space) (space cf.Space) {
	for i, space := range spaces {
		l.ui.Say("%s: %s", term.Green(strconv.Itoa(i+1)), space.Name)
	}

	index, err := strconv.Atoi(l.ui.Ask("Space%s", term.Cyan(">")))

	if err != nil || index > len(spaces) {
		l.ui.Failed("Invalid number", err)
		return l.chooseSpace(spaces)
	}

	return spaces[index-1]
}
Exemplo n.º 8
0
func (l Login) chooseOrg(orgs []cf.Organization) (org cf.Organization) {
	for i, org := range orgs {
		l.ui.Say("%s: %s", term.Green(strconv.Itoa(i+1)), org.Name)
	}

	index, err := strconv.Atoi(l.ui.Ask("Organization%s", term.Cyan(">")))

	if err != nil || index > len(orgs) {
		l.ui.Failed("Invalid number", err)
		return l.chooseOrg(orgs)
	}

	return orgs[index-1]
}
Exemplo n.º 9
0
func doRequest(request *http.Request) (response *http.Response, errorCode int, err error) {
	httpClient := newHttpClient()

	if traceEnabled() {
		dumpedRequest, err := httputil.DumpRequest(request, true)
		if err != nil {
			fmt.Println("Error dumping request")
		} else {
			fmt.Printf("\n%s\n%s\n", term.Cyan("REQUEST:"), Sanitize(string(dumpedRequest)))
		}
	}

	response, err = httpClient.Do(request)

	if err != nil {
		err = errors.New(fmt.Sprintf("Error performing request: %s", err.Error()))
		return
	}

	if traceEnabled() {
		dumpedResponse, err := httputil.DumpResponse(response, true)
		if err != nil {
			fmt.Println("Error dumping response")
		} else {
			fmt.Printf("\n%s\n%s\n", term.Cyan("RESPONSE:"), Sanitize(string(dumpedResponse)))
		}
	}

	if response.StatusCode > 299 {
		errorResponse := getErrorResponse(response)
		errorCode = errorResponse.Code
		message := fmt.Sprintf("Server error, status code: %d, error code: %d, message: %s", response.StatusCode, errorCode, errorResponse.Description)
		err = errors.New(message)
	}

	return
}
Exemplo n.º 10
0
func (l Login) targetOrganization(config *configuration.Configuration, organizations []cf.Organization) {
	var selectedOrg cf.Organization

	if len(organizations) == 1 {
		selectedOrg = organizations[0]
	} else {
		selectedOrg = l.chooseOrg(organizations)
	}

	l.ui.Say("Targeting org %s...", term.Cyan(selectedOrg.Name))
	err := l.saveOrg(config, selectedOrg)

	if err == nil {
		l.ui.Ok()
	}
}
Exemplo n.º 11
0
Arquivo: stop.go Projeto: KaiYoung/cli
func (s *Stop) Run(c *cli.Context) {
	app := s.appReq.GetApplication()

	if app.State == "stopped" {
		s.ui.Say(term.Magenta("Application " + app.Name + " is already stopped."))
		return
	}

	s.ui.Say("Stopping %s...", term.Cyan(app.Name))

	err := s.appRepo.Stop(app)
	if err != nil {
		s.ui.Failed("Error stopping application.", err)
		return
	}
	s.ui.Ok()
}
Exemplo n.º 12
0
func (cmd CreateService) createUserProvidedService(name string, params string) {
	paramsMap := make(map[string]string)
	params = strings.Trim(params, `"`)

	for _, param := range strings.Split(params, ",") {
		param = strings.Trim(param, " ")
		paramsMap[param] = cmd.ui.Ask("%s%s", param, term.Cyan(">"))
	}

	cmd.ui.Say("Creating service...")
	err := cmd.serviceRepo.CreateUserProvidedServiceInstance(name, paramsMap)
	if err != nil {
		cmd.ui.Failed("Error creating user provided service instance", err)
		return
	}

	cmd.ui.Ok()
	return
}
Exemplo n.º 13
0
func (s *Start) ApplicationStart(app cf.Application) {
	if app.State == "started" {
		s.ui.Say(term.Magenta("Application " + app.Name + " is already started."))
		return
	}

	s.ui.Say("Starting %s...", term.Cyan(app.Name))

	err := s.appRepo.Start(app)
	if err != nil {
		s.ui.Failed("Error starting application.", err)
		return
	}

	s.ui.Ok()

	instances, errorCode, err := s.appRepo.GetInstances(app)

	for err != nil {
		if errorCode != 170002 {
			s.ui.Say("")
			s.ui.Failed("Error staging application", err)
			return
		}

		s.ui.Wait(1 * time.Second)
		instances, errorCode, err = s.appRepo.GetInstances(app)
		s.ui.LoadingIndication()
	}

	s.ui.Say("")

	s.startTime = time.Now()

	for s.displayInstancesStatus(app, instances) {
		s.ui.Wait(1 * time.Second)
		instances, _, _ = s.appRepo.GetInstances(app)
	}
}