Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
*.xml

# Test binary, built with `go test -c`
*.test
Expand Down
13 changes: 0 additions & 13 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,6 @@ func (c *Client) ListWorkspaces(teamId int) ([]Workspace, error) {
return workspaces, err
}

func (c *Client) WorkspaceStatus(workspaceId int) (*WorkspaceStatus, error) {
status, _, err := c.api.WorkspacesAPI.WorkspacesGetWorkspaceStatus(c.ctx, float32(workspaceId)).Execute()
return status, err
}

func (c *Client) CreateWorkspace(args CreateWorkspaceArgs) (*Workspace, error) {
workspace, _, err := c.api.WorkspacesAPI.
WorkspacesCreateWorkspace(c.ctx).
WorkspacesCreateWorkspaceRequest(args).
Execute()
return workspace, err
}

func (c *Client) SetEnvVarOnWorkspace(workspaceId int, envVars map[string]string) error {
vars := []openapi_client.WorkspacesListEnvVars200ResponseInner{}
for k, v := range envVars {
Expand Down
1 change: 1 addition & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Client interface {
ListTeams() ([]api.Team, error)
ListWorkspaces(teamId int) ([]api.Workspace, error)
StartPipelines(workspaceId int, pipelineStage string) error
SetEnvVarOnWorkspace(workspaceId int, vars map[string]string) error
}

func NewClient(opts GlobalOptions) (Client, error) {
Expand Down
46 changes: 46 additions & 0 deletions cmd/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Execute() {
addLogCmd(rootCmd, opts)
addListCmd(rootCmd, opts)
addPipelinesCmd(rootCmd, opts)
addSetEnvVarCmd(rootCmd, opts)

opts.ApiUrl = rootCmd.PersistentFlags().StringP("api", "a", "https://codesphere.com/api", "URL of Codesphere API (can also be CS_API)")

Expand Down
64 changes: 64 additions & 0 deletions cmd/set-env-vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"
)

type SetEnvVarCmd struct {
opts SetEnvVarOptions
cmd *cobra.Command
}

type SetEnvVarOptions struct {
GlobalOptions
WorkspaceId *int
EnvVar *[]string
}

func addSetEnvVarCmd(p *cobra.Command, opts GlobalOptions) {
l := SetEnvVarCmd{
cmd: &cobra.Command{
Use: "set-env",
Short: "set env vars",
Long: `set an environment variable for your workspace`,
Example: `
Set environment variable:

$ cs set env var --workspace-id <workspace-id> --name <env-var-name> --value <env-var-value>
`,
},
opts: SetEnvVarOptions{GlobalOptions: opts},
}
l.cmd.RunE = l.RunE
l.parseFlags()
p.AddCommand(l.cmd)
}

func (l *SetEnvVarCmd) parseFlags() {
l.opts.WorkspaceId = l.cmd.Flags().IntP("workspace-id", "w", -1, "ID of workspace to set var")
l.opts.EnvVar = l.cmd.Flags().StringArrayP("env-var", "e", []string{}, "env vars to set in form key=val")
}

func (l *SetEnvVarCmd) RunE(_ *cobra.Command, args []string) (err error) {
client, err := NewClient(l.opts.GlobalOptions)
if err != nil {
return fmt.Errorf("failed to create Codesphere client: %w", err)
}
envVarMap := map[string]string{}
for _, x := range *l.opts.EnvVar {
split := strings.Split(x, "=")
envVarMap[split[0]] = split[1]
}
err = client.SetEnvVarOnWorkspace(*l.opts.WorkspaceId, envVarMap)
if err != nil {
return fmt.Errorf("failed to set environment variables %v: %w", envVarMap, err)
}

return nil
}
69 changes: 0 additions & 69 deletions pkg/cs/workspace.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package cs

import (
"fmt"
"time"

"github.com/codesphere-cloud/cs-go/api"
)

type WaitForWorkspaceRunningOptions struct {
Expand All @@ -15,38 +12,6 @@ type WaitForWorkspaceRunningOptions struct {
// Waits for a given workspace to be running.
//
// Returns [TimedOut] error if the workspace does not become running in time.
func WaitForWorkspaceRunning(
client *api.Client,
workspace *api.Workspace,
opts WaitForWorkspaceRunningOptions,
) error {
timeout := opts.Timeout
if timeout == 0 {
timeout = 20 * time.Minute
}
delay := opts.Delay
if delay == 0 {
delay = 5 * time.Second
}

maxWaitTime := time.Now().Add(timeout)
for time.Now().Before(maxWaitTime) {
status, err := client.WorkspaceStatus(workspace.Id)

if err != nil {
// TODO: log error and retry until timeout is reached.
return err
}
if status.IsRunning {
return nil
}
time.Sleep(delay)
}

return NewTimedOut(
fmt.Sprintf("Waiting for workspace %s(%d) to be ready", workspace.Name, workspace.Id),
timeout)
}

type DeployWorkspaceArgs struct {
TeamId int
Expand All @@ -57,37 +22,3 @@ type DeployWorkspaceArgs struct {

Timeout time.Duration
}

// Deploys a workspace with the given configuration.
//
// Returns [TimedOut] error if the timeout is reached
func DeployWorkspace(
client api.Client,
args DeployWorkspaceArgs,
) error {
workspace, err := client.CreateWorkspace(api.CreateWorkspaceArgs{
TeamId: args.TeamId,
Name: args.Name,
PlanId: args.PlanId,
IsPrivateRepo: true,
GitUrl: nil,
InitialBranch: nil,
SourceWorkspaceId: nil,
WelcomeMessage: nil,
Replicas: 1,
VpnConfig: args.VpnConfigName,
})
if err != nil {
return err
}
if err := WaitForWorkspaceRunning(&client, workspace, WaitForWorkspaceRunningOptions{Timeout: args.Timeout}); err != nil {
return err
}

if len(args.EnvVars) != 0 {
if err := client.SetEnvVarOnWorkspace(workspace.Id, args.EnvVars); err != nil {
return err
}
}
return nil
}