-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server, cli] Allow flexible workspace timeouts
- Loading branch information
1 parent
2b17f0d
commit c79ff26
Showing
10 changed files
with
105 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod" | ||
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol" | ||
"github.com/sourcegraph/jsonrpc2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// setTimeoutCmd sets the timeout of current workspace | ||
var setTimeoutCmd = &cobra.Command{ | ||
Use: "set <duration>", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Set timeout of current workspace", | ||
Long: `Set timeout of current workspace. | ||
Duration must be in the format of <n>m (minutes), <n>h (hours), or <n>d (days). | ||
For example, 30m, 1h, 2d, etc.`, | ||
Example: `gitpod timeout set 1h`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
wsInfo, err := gitpod.GetWSInfo(ctx) | ||
if err != nil { | ||
fail(err.Error()) | ||
} | ||
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{ | ||
"function:setWorkspaceTimeout", | ||
"resource:workspace::" + wsInfo.WorkspaceId + "::get/update", | ||
}) | ||
if err != nil { | ||
fail(err.Error()) | ||
} | ||
duration, err := time.ParseDuration(args[0]) | ||
if err != nil { | ||
fail(err.Error()) | ||
} | ||
if _, err := client.SetWorkspaceTimeout(ctx, wsInfo.WorkspaceId, duration); err != nil { | ||
if err, ok := err.(*jsonrpc2.Error); ok && err.Code == serverapi.PLAN_PROFESSIONAL_REQUIRED { | ||
fail("Cannot extend workspace timeout for current plan, please upgrade your plan") | ||
} | ||
fail(err.Error()) | ||
} | ||
fmt.Printf("Workspace timeout has been set to %d minutes.\n", int(duration.Minutes())) | ||
}, | ||
} | ||
|
||
func init() { | ||
timeoutCmd.AddCommand(setTimeoutCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.