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