Ejemplo n.º 1
0
func Apps(cliConnection plugin.CliConnection, args []string) {
	mySpace, err := cliConnection.GetCurrentSpace()
	util.FreakOut(err)
	rawOutput, err := cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("v3/apps?space_guids=%s", mySpace.Guid), "-X", "GET")
	output := strings.Join(rawOutput, "")
	util.FreakOut(err)
	apps := V3AppsModel{}
	err = json.Unmarshal([]byte(output), &apps)
	util.FreakOut(err)

	if len(apps.Apps) > 0 {
		appsTable := util.NewTable([]string{("name"), ("total_desired_instances")})
		for _, v := range apps.Apps {
			appsTable.Add(
				v.Name,
				strconv.Itoa(v.Instances),
			)
		}
		appsTable.Print()
	} else {
		fmt.Println("No v3 apps found.")
	}
}
Ejemplo n.º 2
0
func CancelTask(cliConnection plugin.CliConnection, args []string) {
	appName := args[1]
	taskName := args[2]

	rawOutput, err := cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps?names=%s", appName))
	util.FreakOut(err)
	output := strings.Join(rawOutput, "")

	apps := models.V3AppsModel{}
	json.Unmarshal([]byte(output), &apps)

	if len(apps.Apps) == 0 {
		fmt.Printf("App %s not found\n", appName)
		return
	}

	appGuid := apps.Apps[0].Guid

	rawTasksJson, err := cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps/%s/tasks", appGuid))
	util.FreakOut(err)
	tasksJson := strings.Join(rawTasksJson, "")

	tasks := models.V3TasksModel{}
	err = json.Unmarshal([]byte(tasksJson), &tasks)
	util.FreakOut(err)

	var runningTasks []runningTask
	for _, task := range tasks.Tasks {
		if taskName == task.Name && task.State == "RUNNING" {
			runningTasks = append(runningTasks, runningTask{task.Guid, task.Command, task.State, task.UpdatedAt})
		}
	}

	if len(runningTasks) == 0 {
		fmt.Println("No running task found. Task name:", taskName)
		return
	} else if len(runningTasks) == 1 {
		rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/tasks/%s/cancel", runningTasks[0].guid), "-X", "PUT", "-d", "{}")
		util.FreakOut(err)
		output := strings.Join(rawOutput, "")
		fmt.Println(output)
		return
	} else {
		fmt.Printf("Please select which task to cancel: \n\n")
		tasksTable := util.NewTable([]string{"#", "Task Name", "Command", "State", "Time"})
		for i, task := range runningTasks {
			tasksTable.Add(
				strconv.Itoa(i+1),
				taskName,
				task.command,
				task.state,
				fmt.Sprintf("%s", task.time.Format("Jan 2, 15:04:05 MST")),
			)
		}
		tasksTable.Print()

		var i int64 = -1
		var str string

		for i <= 0 || i > int64(len(runningTasks)) {
			fmt.Printf("\nSelect from above > ")
			fmt.Scanf("%s", &str)
			i, _ = strconv.ParseInt(str, 10, 32)
		}

		rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/tasks/%s/cancel", runningTasks[i-1].guid), "-X", "PUT", "-d", "{}")
		util.FreakOut(err)
		output := strings.Join(rawOutput, "")
		fmt.Println(output)
	}
}