예제 #1
0
파일: push.go 프로젝트: nota-ja/cli
func (cmd *Push) bindAppToRoute(app models.Application, params models.AppParams, c *cli.Context) {
	if params.NoRoute {
		if len(app.Routes) == 0 {
			cmd.ui.Say("App %s is a worker, skipping route creation", terminal.EntityNameColor(app.Name))
		} else {
			for _, route := range app.Routes {
				cmd.ui.Say("Removing route %s...", terminal.EntityNameColor(route.URL()))
				cmd.routeRepo.Unbind(route.Guid, app.Guid)
			}
		}

		return
	}

	routeFlagsPresent := c.String("n") != "" || c.String("d") != "" || c.Bool("no-hostname")
	if len(app.Routes) > 0 && !routeFlagsPresent {
		return
	}

	domain := cmd.findDomain(params)
	hostname := cmd.hostnameForApp(params, c)

	route, apiErr := cmd.routeRepo.FindByHostAndDomain(hostname, domain.Name)

	switch apiErr.(type) {
	case nil:
		cmd.ui.Say("Using route %s", terminal.EntityNameColor(route.URL()))
	case *errors.ModelNotFoundError:
		cmd.ui.Say("Creating route %s...", terminal.EntityNameColor(domain.UrlForHost(hostname)))

		route, apiErr = cmd.routeRepo.Create(hostname, domain.Guid)
		if apiErr != nil {
			cmd.ui.Failed(apiErr.Error())
		}

		cmd.ui.Ok()
		cmd.ui.Say("")
	default:
		cmd.ui.Failed(apiErr.Error())
	}

	if !app.HasRoute(route) {
		cmd.ui.Say("Binding %s to %s...", terminal.EntityNameColor(domain.UrlForHost(hostname)), terminal.EntityNameColor(app.Name))

		apiErr = cmd.routeRepo.Bind(route.Guid, app.Guid)
		switch apiErr := apiErr.(type) {
		case nil:
			cmd.ui.Ok()
			cmd.ui.Say("")
			return
		case errors.HttpError:
			if apiErr.ErrorCode() == errors.INVALID_RELATION {
				cmd.ui.Failed("The route %s is already in use.\nTIP: Change the hostname with -n HOSTNAME or use --random-route to generate a new route and then push again.", route.URL())
			}
		}
		cmd.ui.Failed(apiErr.Error())
	}
}