Exemple #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.")
	}
}
func RunTask(cliConnection plugin.CliConnection, args []string) {
	appName := args[1]
	taskName := args[2]
	taskCommand := args[3]

	fmt.Println("OK\n")
	fmt.Printf("Running task %s on app %s...\n\n", taskName, appName)

	go Logs(cliConnection, args)
	time.Sleep(2 * time.Second) // b/c sharing the cliConnection makes things break

	rawOutput, _ := cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps?names=%s", appName))
	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

	body := fmt.Sprintf(`{
		"name": "%s",
		"command": "%s"
	}`, taskName, taskCommand)

	rawOutput, err := cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps/%s/tasks", appGuid), "-X", "POST", "-d", body)
	if err != nil {
		fmt.Printf("Failed to run task %s\n", taskName)
		return
	}
	output = strings.Join(rawOutput, "")

	task := models.V3TaskModel{}
	err = json.Unmarshal([]byte(output), &task)
	util.FreakOut(err)
	if task.Guid == "" {
		fmt.Printf("Failed to run task %s:\n%s\n", taskName, output)
		return
	}

	util.Poll(cliConnection, fmt.Sprintf("/v3/tasks/%s", task.Guid), "SUCCEEDED", 1*time.Minute, "Task failed to run")

	fmt.Printf("Task %s successfully completed.\n", task.Guid)
}
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)
	}
}