func NewListAppsCommand(cliConnection api.Connection, orgName string, spaceName string, runtime ui.Runtime) (ui.ListAppsCommand, error) {
	username, err := cliConnection.Username()
	if err != nil {
		return ui.ListAppsCommand{}, err
	}

	if spaceName != "" {
		space, err := cliConnection.GetSpace(spaceName)
		if err != nil || space.Guid == "" {
			return ui.ListAppsCommand{}, err
		}
		orgName = space.Organization.Name
	}

	traceEnv := os.Getenv("CF_TRACE")
	traceLogger := trace.NewLogger(false, traceEnv, "")
	tUI := terminal.NewUI(os.Stdin, terminal.NewTeePrinter(), traceLogger)

	cmd := ui.ListAppsCommand{
		Username:     username,
		Organization: orgName,
		Space:        spaceName,
		UI:           tUI,
		Runtime:      runtime,
	}
	return cmd, nil
}
func NewAppsGetterFunc(
	cliConnection api.Connection,
	orgName string,
	spaceName string,
	runtime ui.Runtime,
) (thingdoer.AppsGetterFunc, error) {
	diegoAppsCommand := thingdoer.AppsGetter{}

	if orgName != "" {
		org, err := cliConnection.GetOrg(orgName)
		if err != nil || org.Guid == "" {
			return nil, OrgNotFoundErr{OrganizationName: orgName}
		}
		diegoAppsCommand.OrganizationGuid = org.Guid
	} else if spaceName != "" {
		space, err := cliConnection.GetSpace(spaceName)
		if err != nil || space.Guid == "" {
			return nil, SpaceNotFoundErr{SpaceName: spaceName}
		}
		diegoAppsCommand.SpaceGuid = space.Guid
	}

	var appsGetterFunc = diegoAppsCommand.DiegoApps
	if runtime == ui.DEA {
		appsGetterFunc = diegoAppsCommand.DeaApps
	}

	return appsGetterFunc, nil
}
func ToggleDiegoSupport(on bool, cliConnection api.Connection, appName string) error {
	d := diegosupport.NewDiegoSupport(cliConnection)

	fmt.Printf("Setting %s Diego support to %t\n", appName, on)
	app, err := cliConnection.GetApp(appName)
	if err != nil {
		return err
	}

	if output, err := d.SetDiegoFlag(app.Guid, on); err != nil {
		return fmt.Errorf("%s\n%s", err, strings.Join(output, "\n"))
	}
	ui.SayOK()

	fmt.Printf("Verifying %s Diego support is set to %t\n", appName, on)
	app, err = cliConnection.GetApp(appName)
	if err != nil {
		return err
	}

	if app.Diego == on {
		ui.SayOK()
	} else {
		return fmt.Errorf("Diego support for %s is NOT set to %t\n\n", appName, on)
	}

	return nil
}
func IsDiegoEnabled(cliConnection api.Connection, appName string) error {
	app, err := cliConnection.GetApp(appName)
	if err != nil {
		return err
	}

	if app.Guid == "" {
		return fmt.Errorf("App %s not found\n\n", appName)
	}

	fmt.Println(app.Diego)

	return nil
}
func NewMigrateAppsCommand(cliConnection api.Connection, organizationName string, spaceName string, runtime ui.Runtime) (ui.MigrateAppsCommand, error) {
	username, err := cliConnection.Username()
	if err != nil {
		return ui.MigrateAppsCommand{}, err
	}

	if spaceName != "" {
		space, err := cliConnection.GetSpace(spaceName)
		if err != nil || space.Guid == "" {
			return ui.MigrateAppsCommand{}, err
		}
		organizationName = space.Organization.Name
	}

	return ui.MigrateAppsCommand{
		Username:     username,
		Runtime:      runtime,
		Organization: organizationName,
		Space:        spaceName,
	}, nil
}