Path: blob/main/components/gitpod-cli/cmd/timeout-set.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"context"8"fmt"9"time"1011"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"12"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"13serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"14"github.com/sourcegraph/jsonrpc2"15"github.com/spf13/cobra"16)1718// setTimeoutCmd sets the timeout of current workspace19var setTimeoutCmd = &cobra.Command{20Use: "set <duration>",21Args: cobra.ExactArgs(1),22Short: "Set timeout of current workspace",23Long: `Set timeout of current workspace.2425Duration must be in the format of <n>m (minutes), <n>h (hours) and cannot be longer than 24 hours.26For example: 30m or 1h`,27Example: fmt.Sprintf("%s %s set 1h", rootCmd.Use, timeoutCmd.Use),28RunE: func(cmd *cobra.Command, args []string) error {29ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)30defer cancel()31wsInfo, err := gitpod.GetWSInfo(ctx)32if err != nil {33return err34}35client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{36"function:setWorkspaceTimeout",37"resource:workspace::" + wsInfo.WorkspaceId + "::get/update",38})39if err != nil {40return err41}42defer client.Close()43duration, err := time.ParseDuration(args[0])44if err != nil {45return GpError{Err: err, OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}46}4748res, err := client.SetWorkspaceTimeout(ctx, wsInfo.WorkspaceId, duration)49if err != nil {50if err, ok := err.(*jsonrpc2.Error); ok && err.Code == serverapi.PLAN_PROFESSIONAL_REQUIRED {51return GpError{OutCome: utils.Outcome_UserErr, Message: "Cannot extend workspace timeout for current plan, please upgrade your plan", ErrorCode: utils.UserErrorCode_NeedUpgradePlan}52}53return err54}55fmt.Printf("Workspace timeout has been set to %s.\n", getHumanReadableDuration(res.HumanReadableDuration, duration))56return nil57},58}5960func getHumanReadableDuration(humanReadableDuration string, inputDuration time.Duration) string {61readable := humanReadableDuration62if humanReadableDuration == "" {63readable = fmt.Sprintf("%d minutes", int(inputDuration.Minutes()))64}65return readable66}6768func init() {69timeoutCmd.AddCommand(setTimeoutCmd)70}717273