Ejemplo n.º 1
0
Archivo: app.go Proyecto: jbayer/cli
func (cmd *App) Run(c *cli.Context) {
	app := cmd.appReq.GetApplication()
	cmd.ui.Say("Showing health and status for app %s...", term.EntityNameColor(app.Name))

	summary, err := cmd.appSummaryRepo.GetSummary(app)
	if err != nil {
		cmd.ui.Failed(err.Error())
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("\n%s %s", term.HeaderColor("health:"), coloredState(summary.App.Health()))
	cmd.ui.Say("%s %s x %d instances", term.HeaderColor("usage:"), byteSize(summary.App.Memory*MEGABYTE), summary.App.Instances)
	cmd.ui.Say("%s %s\n", term.HeaderColor("urls:"), strings.Join(summary.App.Urls, ", "))

	table := [][]string{
		[]string{"", "status", "since", "cpu", "memory", "disk"},
	}

	for index, instance := range summary.Instances {
		table = append(table, []string{
			fmt.Sprintf("#%d", index),
			string(instance.State),
			instance.Since.Format("2006-01-02 03:04:05 PM"),
			fmt.Sprintf("%.1f%%", instance.CpuUsage),
			fmt.Sprintf("%s of %s", byteSize(instance.MemUsage), byteSize(instance.MemQuota)),
			fmt.Sprintf("%s of %s", byteSize(instance.DiskUsage), byteSize(instance.DiskQuota)),
		})
	}

	cmd.ui.DisplayTable(table, cmd.coloringFunc)
}
Ejemplo n.º 2
0
Archivo: show_app.go Proyecto: nsnt/cli
func (cmd *ShowApp) ShowApp(app cf.Application) {

	cmd.ui.Say("Showing health and status for app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.OrganizationFields.Name),
		terminal.EntityNameColor(cmd.config.SpaceFields.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	appSummary, apiResponse := cmd.appSummaryRepo.GetSummary(app.Guid)
	appIsStopped := apiResponse.ErrorCode == cf.APP_STOPPED ||
		apiResponse.ErrorCode == cf.APP_NOT_STAGED ||
		appSummary.State == "stopped"

	if apiResponse.IsNotSuccessful() && !appIsStopped {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	var instances []cf.AppInstanceFields
	instances, apiResponse = cmd.appInstancesRepo.GetInstances(app.Guid)
	if apiResponse.IsNotSuccessful() && !appIsStopped {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("\n%s %s", terminal.HeaderColor("requested state:"), coloredAppState(appSummary.ApplicationFields))
	cmd.ui.Say("%s %s", terminal.HeaderColor("instances:"), coloredAppInstances(appSummary.ApplicationFields))
	cmd.ui.Say("%s %s x %d instances", terminal.HeaderColor("usage:"), formatters.ByteSize(appSummary.Memory*formatters.MEGABYTE), appSummary.InstanceCount)

	var urls []string
	for _, route := range appSummary.RouteSummaries {
		urls = append(urls, route.URL())
	}

	cmd.ui.Say("%s %s\n", terminal.HeaderColor("urls:"), strings.Join(urls, ", "))

	if appIsStopped {
		cmd.ui.Say("There are no running instances of this app.")
		return
	}

	table := [][]string{
		[]string{"", "state", "since", "cpu", "memory", "disk"},
	}

	for index, instance := range instances {
		table = append(table, []string{
			fmt.Sprintf("#%d", index),
			coloredInstanceState(instance),
			instance.Since.Format("2006-01-02 03:04:05 PM"),
			fmt.Sprintf("%.1f%%", instance.CpuUsage*100),
			fmt.Sprintf("%s of %s", formatters.ByteSize(instance.MemUsage), formatters.ByteSize(instance.MemQuota)),
			fmt.Sprintf("%s of %s", formatters.ByteSize(instance.DiskUsage), formatters.ByteSize(instance.DiskQuota)),
		})
	}

	cmd.ui.DisplayTable(table)
}
Ejemplo n.º 3
0
func (cmd *ShowApp) Run(c *cli.Context) {
	app := cmd.appReq.GetApplication()
	cmd.ui.Say("Showing health and status for app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.Organization.Name),
		terminal.EntityNameColor(cmd.config.Space.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	summary, apiResponse := cmd.appSummaryRepo.GetSummary(app)
	appIsStopped := apiResponse.ErrorCode == cf.APP_STOPPED || apiResponse.ErrorCode == cf.APP_NOT_STAGED

	if apiResponse.IsNotSuccessful() && !appIsStopped {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("\n%s %s", terminal.HeaderColor("state:"), coloredAppState(summary.App))
	cmd.ui.Say("%s %s", terminal.HeaderColor("instances:"), coloredAppInstaces(summary.App))
	cmd.ui.Say("%s %s x %d instances", terminal.HeaderColor("usage:"), formatters.ByteSize(summary.App.Memory*formatters.MEGABYTE), summary.App.Instances)

	var urls []string
	for _, route := range summary.App.Routes {
		urls = append(urls, route.URL())
	}
	cmd.ui.Say("%s %s\n", terminal.HeaderColor("urls:"), strings.Join(urls, ", "))

	if appIsStopped {
		return
	}

	table := [][]string{
		[]string{"", "status", "since", "cpu", "memory", "disk"},
	}

	for index, instance := range summary.Instances {
		table = append(table, []string{
			fmt.Sprintf("#%d", index),
			coloredInstanceState(instance),
			instance.Since.Format("2006-01-02 03:04:05 PM"),
			fmt.Sprintf("%.1f%%", instance.CpuUsage),
			fmt.Sprintf("%s of %s", formatters.ByteSize(instance.MemUsage), formatters.ByteSize(instance.MemQuota)),
			fmt.Sprintf("%s of %s", formatters.ByteSize(instance.DiskUsage), formatters.ByteSize(instance.DiskQuota)),
		})
	}

	cmd.ui.DisplayTable(table)
}
Ejemplo n.º 4
0
func (repo LoggregatorLogsRepository) connectToWebsocket(location string, app cf.Application, onConnect func(), outputChan chan *logmessage.Message, stopLoggingChan chan bool, printTimeBuffer time.Duration) (err error) {

	trace.Logger.Printf("\n%s %s\n", terminal.HeaderColor("CONNECTING TO WEBSOCKET:"), location)

	config, err := websocket.NewConfig(location, "http://localhost")
	if err != nil {
		return
	}

	config.Header.Add("Authorization", repo.config.AccessToken)
	config.TlsConfig = &tls.Config{InsecureSkipVerify: true}

	ws, err := websocket.DialConfig(config)
	if err != nil {
		return
	}

	onConnect()

	inputChan := make(chan *logmessage.Message, 1000)

	go repo.sendKeepAlive(ws)
	go repo.listenForMessages(ws, inputChan, stopLoggingChan)
	go makeAndStartMessageSorter(inputChan, outputChan, stopLoggingChan, printTimeBuffer)

	return
}
Ejemplo n.º 5
0
func (cmd *SpaceUsers) Run(c *cli.Context) {
	spaceName := c.Args()[1]
	org := cmd.orgReq.GetOrganization()

	space, apiResponse := cmd.spaceRepo.FindByNameInOrg(spaceName, org)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
	}

	cmd.ui.Say("Getting users in org %s / space %s as %s",
		terminal.EntityNameColor(org.Name),
		terminal.EntityNameColor(space.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	cmd.userRepo.FindAllInSpaceByRole(space)
	usersByRole, apiResponse := cmd.userRepo.FindAllInSpaceByRole(space)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
	}

	cmd.ui.Ok()

	for role, users := range usersByRole {
		cmd.ui.Say("")
		cmd.ui.Say("%s", terminal.HeaderColor(role))

		for _, user := range users {
			cmd.ui.Say("  %s", user.Username)
		}
	}
}
Ejemplo n.º 6
0
func (cmd *OrgUsers) Run(c *cli.Context) {
	org := cmd.orgReq.GetOrganization()
	all := c.Bool("a")

	cmd.ui.Say("Getting users in org %s as %s...",
		terminal.EntityNameColor(org.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	roles := orgRoles
	if all {
		roles = []string{models.ORG_USER}
	}

	for _, role := range roles {
		displayName := orgRoleToDisplayName[role]

		users, apiErr := cmd.userRepo.ListUsersInOrgForRole(org.Guid, role)

		cmd.ui.Say("")
		cmd.ui.Say("%s", terminal.HeaderColor(displayName))

		for _, user := range users {
			cmd.ui.Say("  %s", user.Username)
		}

		if apiErr != nil {
			cmd.ui.Failed("Failed fetching org-users for role %s.\n%s", apiErr.Error(), displayName)
			return
		}
	}
}
Ejemplo n.º 7
0
func (cmd *OrgUsers) Run(c *cli.Context) {
	org := cmd.orgReq.GetOrganization()

	cmd.ui.Say("Getting users in org %s as %s...",
		terminal.EntityNameColor(org.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	usersByRole, apiResponse := cmd.userRepo.FindAllInOrgByRole(org)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()

	for role, users := range usersByRole {
		cmd.ui.Say("")
		cmd.ui.Say("%s", terminal.HeaderColor(role))

		for _, user := range users {
			cmd.ui.Say("  %s", user.Username)
		}
	}
}
Ejemplo n.º 8
0
Archivo: logs.go Proyecto: pmuellr/cli
func (repo LoggregatorLogsRepository) connectToWebsocket(location string, onConnect func(), outputChan chan *logmessage.Message, stopLoggingChan chan bool, printTimeBuffer time.Duration) (err error) {
	trace.Logger.Printf("\n%s %s\n", terminal.HeaderColor("CONNECTING TO WEBSOCKET:"), location)

	config, err := websocket.NewConfig(location, "http://localhost")
	if err != nil {
		return
	}

	config.Header.Add("Authorization", repo.config.AccessToken)
	config.TlsConfig = &tls.Config{InsecureSkipVerify: true}

	ws, err := websocket.DialConfig(config)
	if err != nil {
		return
	}
	defer ws.Close()

	onConnect()

	go repo.sendKeepAlive(ws)

	inputChan := make(chan *logmessage.Message, LogBufferSize)
	stopInputChan := make(chan bool, 1)
	go func() {
		defer close(stopInputChan)
		defer close(inputChan)
		repo.listenForMessages(ws, inputChan, stopInputChan)
	}()

	messageQueue := &SortedMessageQueue{printTimeBuffer: printTimeBuffer}

	repo.processMessages(messageQueue, inputChan, outputChan, stopLoggingChan, stopInputChan)

	return
}
Ejemplo n.º 9
0
func (cmd *SpaceUsers) Run(c *cli.Context) {
	spaceName := c.Args()[1]
	org := cmd.orgReq.GetOrganization()

	space, apiErr := cmd.spaceRepo.FindByNameInOrg(spaceName, org.Guid)
	if apiErr != nil {
		cmd.ui.Failed(apiErr.Error())
	}

	cmd.ui.Say("Getting users in org %s / space %s as %s",
		terminal.EntityNameColor(org.Name),
		terminal.EntityNameColor(space.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	for _, role := range spaceRoles {
		displayName := spaceRoleToDisplayName[role]

		users, apiErr := cmd.userRepo.ListUsersInSpaceForRole(space.Guid, role)

		cmd.ui.Say("")
		cmd.ui.Say("%s", terminal.HeaderColor(displayName))

		for _, user := range users {
			cmd.ui.Say("  %s", user.Username)
		}

		if apiErr != nil {
			cmd.ui.Failed("Failed fetching space-users for role %s.\n%s", apiErr.Error(), displayName)
			return
		}
	}
}
Ejemplo n.º 10
0
func (cmd *OrgUsers) Run(c *cli.Context) {
	org := cmd.orgReq.GetOrganization()

	cmd.ui.Say("Getting users in org %s as %s...",
		terminal.EntityNameColor(org.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	for _, role := range orgRoles {
		stopChan := make(chan bool)
		defer close(stopChan)

		displayName := orgRoleToDisplayName[role]

		usersChan, statusChan := cmd.userRepo.ListUsersInOrgForRole(org.Guid, role, stopChan)

		cmd.ui.Say("")
		cmd.ui.Say("%s", terminal.HeaderColor(displayName))

		for users := range usersChan {
			for _, user := range users {
				cmd.ui.Say("  %s", user.Username)
			}
		}

		apiStatus := <-statusChan
		if apiStatus.IsNotSuccessful() {
			cmd.ui.Failed("Failed fetching org-users for role %s.\n%s", apiStatus.Message, displayName)
			return
		}
	}
}
Ejemplo n.º 11
0
func dumpResponse(res *http.Response) {
	dumpedResponse, err := httputil.DumpResponse(res, true)
	if err != nil {
		trace.Logger.Printf("Error dumping response\n%s\n", err)
	} else {
		trace.Logger.Printf("\n%s [%s]\n%s\n", terminal.HeaderColor("RESPONSE:"), time.Now().Format(time.RFC3339), Sanitize(string(dumpedResponse)))
	}
}
Ejemplo n.º 12
0
Archivo: client.go Proyecto: jbayer/cli
func dumpRequest(req *http.Request) {
	dumpedRequest, err := httputil.DumpRequest(req, true)
	if err != nil {
		fmt.Println("Error dumping request")
	} else {
		fmt.Printf("\n%s\n%s\n", term.HeaderColor("REQUEST:"), Sanitize(string(dumpedRequest)))
	}
}
Ejemplo n.º 13
0
func dumpResponse(res *http.Response) {
	dumpedResponse, err := httputil.DumpResponse(res, true)
	if err != nil {
		trace.Logger.Printf("Error dumping response")
	} else {
		trace.Logger.Printf("\n%s\n%s\n", terminal.HeaderColor("RESPONSE:"), Sanitize(string(dumpedResponse)))
	}
}
Ejemplo n.º 14
0
func (cmd *Start) ApplicationStart(app cf.Application) (updatedApp cf.Application, err error) {
	if app.State == "started" {
		cmd.ui.Say(terminal.WarningColor("App " + app.Name + " is already started"))
		return
	}

	cmd.ui.Say("Starting app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.Organization.Name),
		terminal.EntityNameColor(cmd.config.Space.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	updatedApp, apiResponse := cmd.appRepo.Start(app)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()

	logChan := make(chan *logmessage.Message, 1000)
	go cmd.displayLogMessages(logChan)

	onConnect := func() {
		cmd.ui.Say("\n%s", terminal.HeaderColor("Staging..."))
	}

	stopLoggingChan := make(chan bool)
	go cmd.logRepo.TailLogsFor(app, onConnect, logChan, stopLoggingChan, 1)

	instances, apiResponse := cmd.appRepo.GetInstances(updatedApp)
	for apiResponse.IsNotSuccessful() {
		if apiResponse.ErrorCode != cf.APP_NOT_STAGED {
			cmd.ui.Say("")
			cmd.ui.Failed(apiResponse.Message)
			return
		}

		cmd.ui.Wait(1 * time.Second)
		instances, apiResponse = cmd.appRepo.GetInstances(updatedApp)
	}

	stopLoggingChan <- true

	cmd.ui.Say("")

	cmd.startTime = time.Now()

	for cmd.displayInstancesStatus(app, instances) {
		cmd.ui.Wait(1 * time.Second)
		instances, _ = cmd.appRepo.GetInstances(updatedApp)
	}
	return
}
Ejemplo n.º 15
0
func dumpRequest(req *http.Request) {
	shouldDisplayBody := !strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data")
	dumpedRequest, err := httputil.DumpRequest(req, shouldDisplayBody)
	if err != nil {
		trace.Logger.Print("Error dumping request")
	} else {
		trace.Logger.Printf("\n%s\n%s\n", terminal.HeaderColor("REQUEST:"), Sanitize(string(dumpedRequest)))
		if !shouldDisplayBody {
			trace.Logger.Println("[MULTIPART/FORM-DATA CONTENT HIDDEN]")
		}
	}
}
Ejemplo n.º 16
0
Archivo: start.go Proyecto: nsnt/cli
func (cmd *Start) ApplicationStart(app cf.Application) (updatedApp cf.Application, err error) {
	if app.State == "started" {
		cmd.ui.Say(terminal.WarningColor("App " + app.Name + " is already started"))
		return
	}

	stopLoggingChan := make(chan bool, 1)
	defer close(stopLoggingChan)
	loggingStartedChan := make(chan bool)
	defer close(loggingStartedChan)

	go cmd.tailStagingLogs(app, loggingStartedChan, stopLoggingChan)

	<-loggingStartedChan

	cmd.ui.Say("Starting app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.OrganizationFields.Name),
		terminal.EntityNameColor(cmd.config.SpaceFields.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	params := cf.NewEmptyAppParams()
	params.Set("state", "STARTED")
	updatedApp, apiResponse := cmd.appRepo.Update(app.Guid, params)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()

	cmd.waitForInstancesToStage(updatedApp)
	stopLoggingChan <- true

	cmd.ui.Say("")

	cmd.waitForOneRunningInstance(app.Guid)
	cmd.ui.Say(terminal.HeaderColor("\nApp started\n"))

	cmd.appDisplayer.ShowApp(app)
	return
}
Ejemplo n.º 17
0
func (cmd Start) displayInstancesStatus(app cf.Application, instances []cf.ApplicationInstance) (notFinished bool) {
	totalCount := len(instances)
	runningCount, startingCount, flappingCount, downCount := 0, 0, 0, 0

	for _, inst := range instances {
		switch inst.State {
		case cf.InstanceRunning:
			runningCount++
		case cf.InstanceStarting:
			startingCount++
		case cf.InstanceFlapping:
			flappingCount++
		case cf.InstanceDown:
			downCount++
		}
	}

	if flappingCount > 0 {
		cmd.ui.Failed("Start unsuccessful")
		return false
	}

	anyInstanceRunning := runningCount > 0

	if anyInstanceRunning {
		if len(app.Routes) == 0 {
			cmd.ui.Say(terminal.HeaderColor("Started"))
		} else {
			cmd.ui.Say("Started: app %s available at %s", terminal.EntityNameColor(app.Name), terminal.EntityNameColor(app.Routes[0].URL()))
		}
		return false
	} else {
		details := instancesDetails(runningCount, startingCount, downCount)
		cmd.ui.Say("%d of %d instances running (%s)", runningCount, totalCount, details)
	}

	if time.Since(cmd.startTime) > cmd.config.ApplicationStartTimeout*time.Second {
		cmd.ui.Failed("Start app timeout")
		return false
	}

	return totalCount > runningCount
}
Ejemplo n.º 18
0
Archivo: start.go Proyecto: julz/cli
func (cmd *Start) ApplicationStart(app models.Application) (updatedApp models.Application, err error) {
	if app.State == "started" {
		cmd.ui.Say(terminal.WarningColor("App " + app.Name + " is already started"))
		return
	}

	stopLoggingChan := make(chan bool, 1)
	defer close(stopLoggingChan)
	loggingStartedChan := make(chan bool)
	defer close(loggingStartedChan)

	go cmd.tailStagingLogs(app, loggingStartedChan, stopLoggingChan)

	<-loggingStartedChan

	cmd.ui.Say("Starting app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
		terminal.EntityNameColor(cmd.config.SpaceFields().Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	state := "STARTED"
	updatedApp, apiErr := cmd.appRepo.Update(app.Guid, models.AppParams{State: &state})
	if apiErr != nil {
		cmd.ui.Failed(apiErr.Error())
		return
	}

	cmd.ui.Ok()

	cmd.waitForInstancesToStage(updatedApp)
	stopLoggingChan <- true

	cmd.ui.Say("")

	cmd.waitForOneRunningInstance(updatedApp)
	cmd.ui.Say(terminal.HeaderColor("\nApp started\n"))

	cmd.appDisplayer.ShowApp(updatedApp)
	return
}
Ejemplo n.º 19
0
Archivo: app.go Proyecto: nota-ja/cli
func NewApp(cmdRunner command_runner.Runner, metadatas ...command_metadata.CommandMetadata) (app *cli.App) {
	helpCommand := cli.Command{
		Name:        "help",
		ShortName:   "h",
		Description: "Show help",
		Usage:       fmt.Sprintf("%s help [COMMAND]", cf.Name()),
		Action: func(c *cli.Context) {
			args := c.Args()
			if len(args) > 0 {
				cli.ShowCommandHelp(c, args[0])
			} else {
				showAppHelp(appHelpTemplate, c.App)
			}
		},
	}
	cli.HelpPrinter = showAppHelp
	cli.AppHelpTemplate = appHelpTemplate

	trace.Logger.Printf("\n%s\n%s\n\n", terminal.HeaderColor("VERSION:"), cf.Version)

	app = cli.NewApp()
	app.Usage = cf.Usage
	app.Version = cf.Version
	app.Action = helpCommand.Action

	compiledAtTime, err := time.Parse("Jan 2, 2006 3:04PM", cf.BuiltOnDate)

	if err == nil {
		app.Compiled = compiledAtTime
	} else {
		err = nil
		app.Compiled = time.Now()
	}

	app.Commands = []cli.Command{helpCommand}

	for _, metadata := range metadatas {
		app.Commands = append(app.Commands, getCommand(metadata, cmdRunner))
	}
	return
}
Ejemplo n.º 20
0
func (cmd *SpaceUsers) Run(c *cli.Context) {
	spaceName := c.Args()[1]
	org := cmd.orgReq.GetOrganization()

	space, apiResponse := cmd.spaceRepo.FindByNameInOrg(spaceName, org.Guid)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
	}

	cmd.ui.Say("Getting users in org %s / space %s as %s",
		terminal.EntityNameColor(org.Name),
		terminal.EntityNameColor(space.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	for _, role := range spaceRoles {
		stopChan := make(chan bool)
		defer close(stopChan)

		displayName := spaceRoleToDisplayName[role]

		usersChan, statusChan := cmd.userRepo.ListUsersInSpaceForRole(space.Guid, role, stopChan)

		cmd.ui.Say("")
		cmd.ui.Say("%s", terminal.HeaderColor(displayName))

		for users := range usersChan {
			for _, user := range users {
				cmd.ui.Say("  %s", user.Username)
			}
		}

		apiStatus := <-statusChan
		if apiStatus.IsNotSuccessful() {
			cmd.ui.Failed("Failed fetching space-users for role %s.\n%s", apiStatus.Message, displayName)
			return
		}
	}
}
Ejemplo n.º 21
0
func (repo LoggregatorLogsRepository) connectToWebsocket(location string, onConnect func(), outputChan chan *logmessage.Message, stopLoggingChan chan bool, printTimeBuffer time.Duration) (err error) {
	trace.Logger.Printf("\n%s %s\n", terminal.HeaderColor("CONNECTING TO WEBSOCKET:"), location)

	inputChan := make(chan *logmessage.Message, LogBufferSize)
	messageQueue := NewSortedMessageQueue(printTimeBuffer, time.Now)

	wsConfig, err := websocket.NewConfig(location, "http://localhost")
	if err != nil {
		return
	}

	wsConfig.Header.Add("Authorization", repo.config.AccessToken())
	wsConfig.TlsConfig = net.NewTLSConfig(repo.TrustedCerts, repo.config.IsSSLDisabled())

	ws, err := websocket.DialConfig(wsConfig)
	if err != nil {
		err = net.WrapSSLErrors(location, err)
		return
	}

	defer func() {
		ws.Close()
		repo.drainRemainingMessages(messageQueue, inputChan, outputChan)
	}()

	onConnect()

	go repo.sendKeepAlive(ws)

	go func() {
		defer close(inputChan)
		repo.listenForMessages(ws, inputChan)
	}()

	repo.processMessages(messageQueue, inputChan, outputChan, stopLoggingChan)

	return
}
Ejemplo n.º 22
0
Archivo: client.go Proyecto: jbayer/cli
func doRequest(request *http.Request) (response *http.Response, err error) {
	httpClient := newHttpClient()

	if traceEnabled() {
		dumpRequest(request)
	}

	response, err = httpClient.Do(request)

	if err != nil {
		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.HeaderColor("RESPONSE:"), Sanitize(string(dumpedResponse)))
		}
	}

	return
}
Ejemplo n.º 23
0
Archivo: app.go Proyecto: TildeWill/cli
func NewApp(cmdRunner commands.Runner) (app *cli.App, err error) {
	helpCommand := cli.Command{
		Name:        "help",
		ShortName:   "h",
		Description: "Show help",
		Usage:       fmt.Sprintf("%s help [COMMAND]", cf.Name()),
		Action: func(c *cli.Context) {
			args := c.Args()
			if len(args) > 0 {
				cli.ShowCommandHelp(c, args[0])
			} else {
				showAppHelp(appHelpTemplate, c.App)
			}
		},
	}
	cli.HelpPrinter = showAppHelp
	cli.AppHelpTemplate = appHelpTemplate

	trace.Logger.Printf("\n%s\n%s\n\n", terminal.HeaderColor("VERSION:"), cf.Version)

	app = cli.NewApp()
	app.Usage = cf.Usage
	app.Version = cf.Version
	app.Action = helpCommand.Action
	app.Commands = []cli.Command{
		helpCommand,
		{
			Name:        "api",
			Description: "Set or view target api url",
			Usage:       fmt.Sprintf("%s api [URL]", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("api", c)
			},
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "skip-ssl-validation", Usage: "Please don't"},
			},
		},
		{
			Name:        "app",
			Description: "Display health and status for app",
			Usage:       fmt.Sprintf("%s app APP", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("app", c)
			},
		},
		{
			Name:        "apps",
			ShortName:   "a",
			Description: "List all apps in the target space",
			Usage:       fmt.Sprintf("%s apps", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("apps", c)
			},
		},
		{
			Name:        "auth",
			Description: "Authenticate user non-interactively",
			Usage: fmt.Sprintf("%s auth USERNAME PASSWORD\n\n", cf.Name()) +
				terminal.WarningColor("WARNING:\n   Providing your password as a command line option is highly discouraged\n   Your password may be visible to others and may be recorded in your shell history\n\n") +
				"EXAMPLE:\n" +
				fmt.Sprintf("   %s auth [email protected] \"my password\" (use quotes for passwords with a space)\n", cf.Name()) +
				fmt.Sprintf("   %s auth [email protected] \"\\\"password\\\"\" (escape quotes if used in password)", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("auth", c)
			},
		},
		{
			Name:        "bind-service",
			ShortName:   "bs",
			Description: "Bind a service instance to an app",
			Usage:       fmt.Sprintf("%s bind-service APP SERVICE_INSTANCE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("bind-service", c)
			},
		},
		{
			Name:        "buildpacks",
			Description: "List all buildpacks",
			Usage:       fmt.Sprintf("%s buildpacks", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("buildpacks", c)
			},
		},
		{
			Name:        "create-buildpack",
			Description: "Create a buildpack",
			Usage: fmt.Sprintf("%s create-buildpack BUILDPACK PATH POSITION [--enable|--disable]", cf.Name()) +
				"\n\nTIP:\n" +
				"   Path should be a zip file, a url to a zip file, or a local directory. Position is an integer, sets priority, and is sorted from lowest to highest.",
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "enable", Usage: "Enable the buildpack"},
				cli.BoolFlag{Name: "disable", Usage: "Disable the buildpack"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-buildpack", c)
			},
		},
		{
			Name:        "create-domain",
			Description: "Create a domain in an org for later use",
			Usage:       fmt.Sprintf("%s create-domain ORG DOMAIN", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-domain", c)
			},
		},
		{
			Name:        "create-org",
			ShortName:   "co",
			Description: "Create an org",
			Usage:       fmt.Sprintf("%s create-org ORG", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-org", c)
			},
		},
		{
			Name:        "create-route",
			Description: "Create a url route in a space for later use",
			Usage:       fmt.Sprintf("%s create-route SPACE DOMAIN [-n HOSTNAME]", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("n", "Hostname"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-route", c)
			},
		},
		{
			Name:        "create-service",
			ShortName:   "cs",
			Description: "Create a service instance",
			Usage: fmt.Sprintf("%s create-service SERVICE PLAN SERVICE_INSTANCE\n\n", cf.Name()) +
				"EXAMPLE:\n" +
				fmt.Sprintf("   %s create-service cleardb spark clear-db-mine\n\n", cf.Name()) +
				"TIP:\n" +
				"   Use '" + cf.Name() + " create-user-provided-service' to make user-provided services available to cf apps",
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-service", c)
			},
		},
		{
			Name:        "create-service-auth-token",
			Description: "Create a service auth token",
			Usage:       fmt.Sprintf("%s create-service-auth-token LABEL PROVIDER TOKEN", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-service-auth-token", c)
			},
		},
		{
			Name:        "create-service-broker",
			Description: "Create a service broker",
			Usage:       fmt.Sprintf("%s create-service-broker SERVICE_BROKER USERNAME PASSWORD URL", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-service-broker", c)
			},
		},
		{
			Name:        "create-space",
			Description: "Create a space",
			Usage:       fmt.Sprintf("%s create-space SPACE [-o ORG]", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("o", "Organization"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-space", c)
			},
		},
		{
			Name:        "create-user",
			Description: "Create a new user",
			Usage:       fmt.Sprintf("%s create-user USERNAME PASSWORD", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-user", c)
			},
		},
		{
			Name:        "create-user-provided-service",
			ShortName:   "cups",
			Description: "Make a user-provided service available to cf apps",
			Usage: fmt.Sprintf("%s create-user-provided-service SERVICE_INSTANCE [-p PARAMETERS] [-l SYSLOG-DRAIN-URL]\n", cf.Name()) +
				"\n   Pass comma separated parameter names to enable interactive mode:\n" +
				fmt.Sprintf("   %s create-user-provided-service SERVICE_INSTANCE -p \"comma, separated, parameter, names\"\n", cf.Name()) +
				"\n   Pass parameters as JSON to create a service non-interactively:\n" +
				fmt.Sprintf("   %s create-user-provided-service SERVICE_INSTANCE -p '{\"name\":\"value\",\"name\":\"value\"}'\n", cf.Name()) +
				"\nEXAMPLE:\n" +
				fmt.Sprintf("   %s create-user-provided-service oracle-db-mine -p \"host, port, dbname, username, password\"\n", cf.Name()) +
				fmt.Sprintf("   %s create-user-provided-service oracle-db-mine -p '{\"username\":\"admin\",\"password\":\"pa55woRD\"}'\n", cf.Name()) +
				fmt.Sprintf("   %s create-user-provided-service my-drain-service -l syslog://example.com\n", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("p", "Parameters"),
				NewStringFlag("l", "Syslog Drain Url"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-user-provided-service", c)
			},
		},
		{
			Name:        "curl",
			Description: "Executes a raw request, content-type set to application/json by default",
			Usage:       fmt.Sprintf("%s curl PATH [-X METHOD] [-H HEADER] [-d DATA] [-i]", cf.Name()),
			Flags: []cli.Flag{
				cli.StringFlag{Name: "X", Value: "GET", Usage: "HTTP method (GET,POST,PUT,DELETE,etc)"},
				NewStringSliceFlag("H", "Custom headers to include in the request, flag can be specified multiple times"),
				NewStringFlag("d", "HTTP data to include in the request body"),
				cli.BoolFlag{Name: "i", Usage: "Include response headers in the output"},
				cli.BoolFlag{Name: "v", Usage: "Enable CF_TRACE output for all requests and responses"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("curl", c)
			},
		},
		{
			Name:        "delete",
			ShortName:   "d",
			Description: "Delete an app",
			Usage:       fmt.Sprintf("%s delete APP [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete", c)
			},
		},
		{
			Name:        "delete-buildpack",
			Description: "Delete a buildpack",
			Usage:       fmt.Sprintf("%s delete-buildpack BUILDPACK [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-buildpack", c)
			},
		},
		{
			Name:        "delete-domain",
			Description: "Delete a domain",
			Usage:       fmt.Sprintf("%s delete-domain DOMAIN [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-domain", c)
			},
		},
		{
			Name:        "delete-shared-domain",
			Description: "Delete a shared domain",
			Usage:       fmt.Sprintf("%s delete-shared-domain DOMAIN [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-shared-domain", c)
			},
		},
		{
			Name:        "delete-org",
			Description: "Delete an org",
			Usage:       fmt.Sprintf("%s delete-org ORG [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-org", c)
			},
		},
		{
			Name:        "delete-route",
			Description: "Delete a route",
			Usage:       fmt.Sprintf("%s delete-route DOMAIN [-n HOSTNAME] [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
				NewStringFlag("n", "Hostname"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-route", c)
			},
		},
		{
			Name:        "delete-service",
			ShortName:   "ds",
			Description: "Delete a service instance",
			Usage:       fmt.Sprintf("%s delete-service SERVICE_INSTANCE [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-service", c)
			},
		},
		{
			Name:        "delete-service-auth-token",
			Description: "Delete a service auth token",
			Usage:       fmt.Sprintf("%s delete-service-auth-token LABEL PROVIDER [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-service-auth-token", c)
			},
		},
		{
			Name:        "delete-service-broker",
			Description: "Delete a service broker",
			Usage:       fmt.Sprintf("%s delete-service-broker SERVICE_BROKER [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-service-broker", c)
			},
		},
		{
			Name:        "delete-space",
			Description: "Delete a space",
			Usage:       fmt.Sprintf("%s delete-space SPACE [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-space", c)
			},
		},
		{
			Name:        "delete-user",
			Description: "Delete a user",
			Usage:       fmt.Sprintf("%s delete-user USERNAME [-f]", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("delete-user", c)
			},
		},
		{
			Name:        "domains",
			Description: "List domains in the target org",
			Usage:       fmt.Sprintf("%s domains", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("domains", c)
			},
		},
		{
			Name:        "env",
			ShortName:   "e",
			Description: "Show all env variables for an app",
			Usage:       fmt.Sprintf("%s env APP", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("env", c)
			},
		},
		{
			Name:        "events",
			Description: "Show recent app events",
			Usage:       fmt.Sprintf("%s events APP", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("events", c)
			},
		},
		{
			Name:        "files",
			ShortName:   "f",
			Description: "Print out a list of files in a directory or the contents of a specific file",
			Usage:       fmt.Sprintf("%s files APP [PATH]", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("files", c)
			},
		},
		{
			Name:        "login",
			ShortName:   "l",
			Description: "Log user in",
			Usage: fmt.Sprintf("%s login [-a API_URL] [-u USERNAME] [-p PASSWORD] [-o ORG] [-s SPACE]\n\n", cf.Name()) +
				terminal.WarningColor("WARNING:\n   Providing your password as a command line option is highly discouraged\n   Your password may be visible to others and may be recorded in your shell history\n\n") +
				"EXAMPLE:\n" +
				fmt.Sprintf("   %s login (omit username and password to login interactively -- %s will prompt for both)\n", cf.Name(), cf.Name()) +
				fmt.Sprintf("   %s login -u [email protected] -p pa55woRD (specify username and password as arguments)\n", cf.Name()) +
				fmt.Sprintf("   %s login -u [email protected] -p \"my password\" (use quotes for passwords with a space)\n", cf.Name()) +
				fmt.Sprintf("   %s login -u [email protected] -p \"\\\"password\\\"\" (escape quotes if used in password)", cf.Name()),
			Flags: []cli.Flag{
				StringFlagWithNoDefault{cli.StringFlag{
					Name: "a", Usage: "API endpoint (e.g. https://api.example.com)",
				}},
				NewStringFlag("u", "Username"),
				NewStringFlag("p", "Password"),
				NewStringFlag("o", "Org"),
				NewStringFlag("s", "Space"),
				cli.BoolFlag{Name: "skip-ssl-validation", Usage: "Please don't"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("login", c)
			},
		},
		{
			Name:        "logout",
			ShortName:   "lo",
			Description: "Log user out",
			Usage:       fmt.Sprintf("%s logout", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("logout", c)
			},
		},
		{
			Name:        "logs",
			Description: "Tail or show recent logs for an app",
			Usage:       fmt.Sprintf("%s logs APP", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "recent", Usage: "Dump recent logs instead of tailing"},
			},

			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("logs", c)
			},
		},
		{
			Name:        "marketplace",
			ShortName:   "m",
			Description: "List available offerings in the marketplace",
			Usage:       fmt.Sprintf("%s marketplace", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("marketplace", c)
			},
		},
		{
			Name:        "map-route",
			Description: "Add a url route to an app",
			Usage:       fmt.Sprintf("%s map-route APP DOMAIN [-n HOSTNAME]", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("n", "Hostname"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("map-route", c)
			},
		},
		{
			Name:        "org",
			Description: "Show org info",
			Usage:       fmt.Sprintf("%s org ORG", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("org", c)
			},
		},
		{
			Name:        "org-users",
			Description: "Show org users by role",
			Usage:       fmt.Sprintf("%s org-users ORG", cf.Name()),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "a", Usage: "List all users in the org"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("org-users", c)
			},
		},
		{
			Name:        "orgs",
			ShortName:   "o",
			Description: "List all orgs",
			Usage:       fmt.Sprintf("%s orgs", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("orgs", c)
			},
		},
		{
			Name:        "passwd",
			ShortName:   "pw",
			Description: "Change user password",
			Usage:       fmt.Sprintf("%s passwd", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("passwd", c)
			},
		},
		{
			Name:        "purge-service-offering",
			Description: "Recursively remove a service and child objects from Cloud Foundry database without making requests to a service broker",
			Usage: fmt.Sprintf("%s purge-service-offering SERVICE [-p PROVIDER]", cf.Name()) +
				"\n\nWARNING:\n" +
				"This operation assumes that the service broker responsible for this service offering is no longer available, and all service instances have been deleted, leaving orphan records in Cloud Foundry's database. All knowledge of the service will be removed from Cloud Foundry, including service instances and service bindings. No attempt will be made to contact the service broker; running this command without destroying the service broker will cause orphan service instances. After running this command you may want to run either delete-service-auth-token or delete-service-broker to complete the cleanup.",
			Flags: []cli.Flag{
				NewStringFlag("p", "Provider"),
				cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("purge-service-offering", c)
			},
		},
		{
			Name:        "push",
			ShortName:   "p",
			Description: "Push a new app or sync changes to an existing app",
			Usage: "Push a single app (with or without a manifest):\n" +
				fmt.Sprintf("   %s push APP [-b BUILDPACK_NAME] [-c COMMAND] [-d DOMAIN] [-f MANIFEST_PATH]\n", cf.Name()) +
				"   [-i NUM_INSTANCES] [-m MEMORY] [-n HOST] [-p PATH] [-s STACK] [-t TIMEOUT]\n" +
				"   [--no-hostname] [--no-manifest] [--no-route] [--no-start]" +
				"\n\n   Push multiple apps with a manifest:\n" +
				fmt.Sprintf("   %s push [-f MANIFEST_PATH]\n", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("b", "Custom buildpack by name (e.g. my-buildpack) or GIT URL (e.g. https://github.com/heroku/heroku-buildpack-play.git)"),
				NewStringFlag("c", "Startup command, set to null to reset to default start command"),
				NewStringFlag("d", "Domain (e.g. example.com)"),
				NewStringFlag("f", "Path to manifest"),
				NewStringFlag("i", "Number of instances"),
				NewStringFlag("m", "Memory limit (e.g. 256M, 1024M, 1G)"),
				NewStringFlag("n", "Hostname (e.g. my-subdomain)"),
				NewStringFlag("p", "Path of app directory or zip file"),
				NewStringFlag("s", "Stack to use"),
				NewStringFlag("t", "Start timeout in seconds"),
				cli.BoolFlag{Name: "no-hostname", Usage: "Map the root domain to this app"},
				cli.BoolFlag{Name: "no-manifest", Usage: "Ignore manifest file"},
				cli.BoolFlag{Name: "no-route", Usage: "Do not map a route to this app"},
				cli.BoolFlag{Name: "no-start", Usage: "Do not start an app after pushing"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("push", c)
			},
		},
		{
			Name:        "quotas",
			Description: "List available usage quotas ",
			Usage:       fmt.Sprintf("%s quotas", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("quotas", c)
			},
		},
		{
			Name:        "rename",
			Description: "Rename an app",
			Usage:       fmt.Sprintf("%s rename APP NEW_APP", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("rename", c)
			},
		},
		{
			Name:        "rename-org",
			Description: "Rename an org",
			Usage:       fmt.Sprintf("%s rename-org ORG NEW_ORG", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("rename-org", c)
			},
		},
		{
			Name:        "rename-service",
			Description: "Rename a service instance",
			Usage:       fmt.Sprintf("%s rename-service SERVICE_INSTANCE NEW_SERVICE_INSTANCE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("rename-service", c)
			},
		},
		{
			Name:        "rename-service-broker",
			Description: "Rename a service broker",
			Usage:       fmt.Sprintf("%s rename-service-broker SERVICE_BROKER NEW_SERVICE_BROKER", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("rename-service-broker", c)
			},
		},
		{
			Name:        "rename-space",
			Description: "Rename a space",
			Usage:       fmt.Sprintf("%s rename-space SPACE NEW_SPACE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("rename-space", c)
			},
		},
		{
			Name:        "restart",
			ShortName:   "rs",
			Description: "Restart an app",
			Usage:       fmt.Sprintf("%s restart APP", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("restart", c)
			},
		},
		{
			Name:        "routes",
			ShortName:   "r",
			Description: "List all routes",
			Usage:       fmt.Sprintf("%s routes", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("routes", c)
			},
		},
		{
			Name:        "scale",
			Description: "Change or view the instance count, disk space limit, and memory limit for an app",
			Usage:       fmt.Sprintf("%s scale APP [-i INSTANCES] [-k DISK] [-m MEMORY] [-f]", cf.Name()),
			Flags: []cli.Flag{
				NewIntFlagWithValue("i", "Number of instances", -1),
				NewStringFlag("k", "Disk limit (e.g. 256M, 1024M, 1G)"),
				NewStringFlag("m", "Memory limit (e.g. 256M, 1024M, 1G)"),
				cli.BoolFlag{Name: "f", Usage: "Force restart of app without prompt"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("scale", c)
			},
		},
		{
			Name:        "service",
			Description: "Show service instance info",
			Usage:       fmt.Sprintf("%s service SERVICE_INSTANCE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("service", c)
			},
		},
		{
			Name:        "service-auth-tokens",
			Description: "List service auth tokens",
			Usage:       fmt.Sprintf("%s service-auth-tokens", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("service-auth-tokens", c)
			},
		},
		{
			Name:        "service-brokers",
			Description: "List service brokers",
			Usage:       fmt.Sprintf("%s service-brokers", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("service-brokers", c)
			},
		},
		{
			Name:        "services",
			ShortName:   "s",
			Description: "List all services in the target space",
			Usage:       fmt.Sprintf("%s services", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("services", c)
			},
		},
		{
			Name:        "migrate-service-instances",
			Description: "Migrate service instances from one service plan to another",
			Usage: fmt.Sprintf(
				"%s migrate-service-instances v1_SERVICE v1_PROVIDER v1_PLAN v2_SERVICE v2_PLAN\n\n"+
					"WARNING: This operation is internal to Cloud Foundry; service brokers will not be contacted and"+
					" resources for service instances will not be altered. The primary use case for this operation is"+
					" to replace a service broker which implements the v1 Service Broker API with a broker which"+
					" implements the v2 API by remapping service instances from v1 plans to v2 plans.  We recommend"+
					" making the v1 plan private or shutting down the v1 broker to prevent additional instances from"+
					" being created. Once service instances have been migrated, the v1 services and plans can be"+
					" removed from Cloud Foundry.",
				cf.Name(),
			),
			Flags: []cli.Flag{
				cli.BoolFlag{Name: "f", Usage: "Force migration without confirmation"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("migrate-service-instances", c)
			},
		},
		{
			Name:        "set-env",
			ShortName:   "se",
			Description: "Set an env variable for an app",
			Usage:       fmt.Sprintf("%s set-env APP NAME VALUE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("set-env", c)
			},
		},
		{
			Name:        "set-org-role",
			Description: "Assign an org role to a user",
			Usage: fmt.Sprintf("%s set-org-role USERNAME ORG ROLE\n\n", cf.Name()) +
				"ROLES:\n" +
				"   OrgManager - Invite and manage users, select and change plans, and set spending limits\n" +
				"   BillingManager - Create and manage the billing account and payment info\n" +
				"   OrgAuditor - Read-only access to org info and reports\n",
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("set-org-role", c)
			},
		},
		{
			Name:        "set-quota",
			Description: "Define the quota for an org",
			Usage: fmt.Sprintf("%s set-quota ORG QUOTA\n\n", cf.Name()) +
				"TIP:\n" +
				fmt.Sprintf("   View allowable quotas with '%s quotas'", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("set-quota", c)
			},
		},
		{
			Name:        "set-space-role",
			Description: "Assign a space role to a user",
			Usage: fmt.Sprintf("%s set-space-role USERNAME ORG SPACE ROLE\n\n", cf.Name()) +
				"ROLES:\n" +
				"   SpaceManager - Invite and manage users, and enable features for a given space\n" +
				"   SpaceDeveloper - Create and manage apps and services, and see logs and reports\n" +
				"   SpaceAuditor - View logs, reports, and settings on this space\n",
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("set-space-role", c)
			},
		},
		{
			Name:        "create-shared-domain",
			Description: "Create a domain that can be used by all orgs (admin-only)",
			Usage:       fmt.Sprintf("%s create-shared-domain DOMAIN", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("create-shared-domain", c)
			},
		},
		{
			Name:        "space",
			Description: "Show space info",
			Usage:       fmt.Sprintf("%s space SPACE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("space", c)
			},
		},
		{
			Name:        "space-users",
			Description: "Show space users by role",
			Usage:       fmt.Sprintf("%s space-users ORG SPACE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("space-users", c)
			},
		},
		{
			Name:        "spaces",
			Description: "List all spaces in an org",
			Usage:       fmt.Sprintf("%s spaces", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("spaces", c)
			},
		},
		{
			Name:        "stacks",
			Description: "List all stacks",
			Usage:       fmt.Sprintf("%s stacks", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("stacks", c)
			},
		},
		{
			Name:        "start",
			ShortName:   "st",
			Description: "Start an app",
			Usage:       fmt.Sprintf("%s start APP", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("start", c)
			},
		},
		{
			Name:        "stop",
			ShortName:   "sp",
			Description: "Stop an app",
			Usage:       fmt.Sprintf("%s stop APP", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("stop", c)
			},
		},
		{
			Name:        "target",
			ShortName:   "t",
			Description: "Set or view the targeted org or space",
			Usage:       fmt.Sprintf("%s target [-o ORG] [-s SPACE]", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("o", "organization"),
				NewStringFlag("s", "space"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("target", c)
			},
		},
		{
			Name:        "unbind-service",
			ShortName:   "us",
			Description: "Unbind a service instance from an app",
			Usage:       fmt.Sprintf("%s unbind-service APP SERVICE_INSTANCE", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("unbind-service", c)
			},
		},
		{
			Name:        "unmap-route",
			Description: "Remove a url route from an app",
			Usage:       fmt.Sprintf("%s unmap-route APP DOMAIN [-n HOSTNAME]", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("n", "Hostname"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("unmap-route", c)
			},
		},
		{
			Name:        "unset-env",
			Description: "Remove an env variable",
			Usage:       fmt.Sprintf("%s unset-env APP NAME", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("unset-env", c)
			},
		},
		{
			Name:        "unset-org-role",
			Description: "Remove an org role from a user",
			Usage: fmt.Sprintf("%s unset-org-role USERNAME ORG ROLE\n\n", cf.Name()) +
				"ROLES:\n" +
				"   OrgManager - Invite and manage users, select and change plans, and set spending limits\n" +
				"   BillingManager - Create and manage the billing account and payment info\n" +
				"   OrgAuditor - Read-only access to org info and reports\n",
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("unset-org-role", c)
			},
		},
		{
			Name:        "unset-space-role",
			Description: "Remove a space role from a user",
			Usage: fmt.Sprintf("%s unset-space-role USERNAME ORG SPACE ROLE\n\n", cf.Name()) +
				"ROLES:\n" +
				"   SpaceManager - Invite and manage users, and enable features for a given space\n" +
				"   SpaceDeveloper - Create and manage apps and services, and see logs and reports\n" +
				"   SpaceAuditor - View logs, reports, and settings on this space\n",
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("unset-space-role", c)
			},
		},
		{
			Name:        "update-buildpack",
			Description: "Update a buildpack",
			Usage:       fmt.Sprintf("%s update-buildpack BUILDPACK [-p PATH] [-i POSITION] [--enable|--disable] [--lock|--unlock]", cf.Name()),
			Flags: []cli.Flag{
				NewIntFlag("i", "Buildpack position among other buildpacks"),
				NewStringFlag("p", "Path to directory or zip file"),
				cli.BoolFlag{Name: "enable", Usage: "Enable the buildpack"},
				cli.BoolFlag{Name: "disable", Usage: "Disable the buildpack"},
				cli.BoolFlag{Name: "lock", Usage: "Lock the buildpack"},
				cli.BoolFlag{Name: "unlock", Usage: "Unlock the buildpack"},
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("update-buildpack", c)
			},
		},
		{
			Name:        "update-service-broker",
			Description: "Update a service broker",
			Usage:       fmt.Sprintf("%s update-service-broker SERVICE_BROKER USERNAME PASSWORD URL", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("update-service-broker", c)
			},
		},
		{
			Name:        "update-service-auth-token",
			Description: "Update a service auth token",
			Usage:       fmt.Sprintf("%s update-service-auth-token LABEL PROVIDER TOKEN", cf.Name()),
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("update-service-auth-token", c)
			},
		},
		{
			Name:        "update-user-provided-service",
			ShortName:   "uups",
			Description: "Update user-provided service name value pairs",
			Usage: fmt.Sprintf("%s update-user-provided-service SERVICE_INSTANCE [-p PARAMETERS] [-l SYSLOG-DRAIN-URL]'\n\n", cf.Name()) +
				"EXAMPLE:\n" +
				fmt.Sprintf("   %s update-user-provided-service oracle-db-mine -p '{\"username\":\"admin\",\"password\":\"pa55woRD\"}'\n", cf.Name()) +
				fmt.Sprintf("   %s update-user-provided-service my-drain-service -l syslog://example.com\n", cf.Name()),
			Flags: []cli.Flag{
				NewStringFlag("p", "Parameters"),
				NewStringFlag("l", "Syslog Drain Url"),
			},
			Action: func(c *cli.Context) {
				cmdRunner.RunCmdByName("update-user-provided-service", c)
			},
		},
	}
	return
}
Ejemplo n.º 24
0
Archivo: help.go Proyecto: nota-ja/cli
func (c groupedCommands) SubTitle(name string) string {
	return terminal.HeaderColor(name + ":")
}
Ejemplo n.º 25
0
Archivo: scale.go Proyecto: julz/cli
func (cmd *Scale) Run(c *cli.Context) {
	currentApp := cmd.appReq.GetApplication()
	if !anyFlagsSet(c) {
		cmd.ui.Say("Showing current scale of app %s in org %s / space %s as %s...",
			terminal.EntityNameColor(currentApp.Name),
			terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
			terminal.EntityNameColor(cmd.config.SpaceFields().Name),
			terminal.EntityNameColor(cmd.config.Username()),
		)
		cmd.ui.Ok()
		cmd.ui.Say("")

		cmd.ui.Say("%s %s", terminal.HeaderColor("memory:"), formatters.ByteSize(currentApp.Memory*bytesInAMegabyte))
		cmd.ui.Say("%s %s", terminal.HeaderColor("disk:"), formatters.ByteSize(currentApp.DiskQuota*bytesInAMegabyte))
		cmd.ui.Say("%s %d", terminal.HeaderColor("instances:"), currentApp.InstanceCount)

		return
	}

	params := models.AppParams{}
	shouldRestart := false

	if c.String("m") != "" {
		memory, err := formatters.ToMegabytes(c.String("m"))
		if err != nil {
			cmd.ui.Failed("Invalid memory limit: %s\n%s", c.String("m"), err)
		}
		params.Memory = &memory
		shouldRestart = true
	}

	if c.String("k") != "" {
		diskQuota, err := formatters.ToMegabytes(c.String("k"))
		if err != nil {
			cmd.ui.Failed("Invalid disk quota: %s\n%s", c.String("k"), err)
		}
		params.DiskQuota = &diskQuota
		shouldRestart = true
	}

	if c.IsSet("i") {
		instances := c.Int("i")
		if instances > 0 {
			params.InstanceCount = &instances
		} else {
			cmd.ui.Failed("Invalid instance count: %d\nInstance count must be a positive integer", instances)
		}
	}

	if shouldRestart && !cmd.confirmRestart(c, currentApp.Name) {
		return
	}

	cmd.ui.Say("Scaling app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(currentApp.Name),
		terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
		terminal.EntityNameColor(cmd.config.SpaceFields().Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	updatedApp, apiErr := cmd.appRepo.Update(currentApp.Guid, params)
	if apiErr != nil {
		cmd.ui.Failed(apiErr.Error())
		return
	}

	cmd.ui.Ok()

	if shouldRestart {
		cmd.restarter.ApplicationRestart(updatedApp)
	}
}
Ejemplo n.º 26
0
Archivo: help.go Proyecto: nota-ja/cli
func (p appPresenter) Title(name string) string {
	return terminal.HeaderColor(name)
}
Ejemplo n.º 27
0
Archivo: app.go Proyecto: nota-ja/cli
func (cmd *ShowApp) ShowApp(app models.Application) {

	cmd.ui.Say("Showing health and status for app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
		terminal.EntityNameColor(cmd.config.SpaceFields().Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	application, apiErr := cmd.appSummaryRepo.GetSummary(app.Guid)

	appIsStopped := (application.State == "stopped")
	if err, ok := apiErr.(errors.HttpError); ok {
		if err.ErrorCode() == errors.APP_STOPPED || err.ErrorCode() == errors.APP_NOT_STAGED {
			appIsStopped = true
		}
	}

	if apiErr != nil && !appIsStopped {
		cmd.ui.Failed(apiErr.Error())
		return
	}

	var instances []models.AppInstanceFields
	instances, apiErr = cmd.appInstancesRepo.GetInstances(app.Guid)
	if apiErr != nil && !appIsStopped {
		cmd.ui.Failed(apiErr.Error())
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("\n%s %s", terminal.HeaderColor("requested state:"), ui_helpers.ColoredAppState(application.ApplicationFields))
	cmd.ui.Say("%s %s", terminal.HeaderColor("instances:"), ui_helpers.ColoredAppInstances(application.ApplicationFields))
	cmd.ui.Say("%s %s x %d instances", terminal.HeaderColor("usage:"), formatters.ByteSize(application.Memory*formatters.MEGABYTE), application.InstanceCount)

	var urls []string
	for _, route := range application.Routes {
		urls = append(urls, route.URL())
	}

	cmd.ui.Say("%s %s\n", terminal.HeaderColor("urls:"), strings.Join(urls, ", "))

	if appIsStopped {
		cmd.ui.Say("There are no running instances of this app.")
		return
	}

	table := terminal.NewTable(cmd.ui, []string{"", "state", "since", "cpu", "memory", "disk"})
	rows := [][]string{}

	for index, instance := range instances {
		rows = append(rows, []string{
			fmt.Sprintf("#%d", index),
			ui_helpers.ColoredInstanceState(instance),
			instance.Since.Format("2006-01-02 03:04:05 PM"),
			fmt.Sprintf("%.1f%%", instance.CpuUsage*100),
			fmt.Sprintf("%s of %s", formatters.ByteSize(instance.MemUsage), formatters.ByteSize(instance.MemQuota)),
			fmt.Sprintf("%s of %s", formatters.ByteSize(instance.DiskUsage), formatters.ByteSize(instance.DiskQuota)),
		})
	}

	table.Print(rows)
}