Skip to content

Commit 710bc0b

Browse files
JayByeunNautiluX
authored andcommitted
add(set-env-var): cmd for set env var
Fixed build fails Fixed build fails Fixed build fails Fixed build fails Fixed build fails add command line params remove kaput workspace functions fix array parsing
1 parent 3b3433a commit 710bc0b

File tree

8 files changed

+113
-82
lines changed

8 files changed

+113
-82
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.dll
88
*.so
99
*.dylib
10+
*.xml
1011

1112
# Test binary, built with `go test -c`
1213
*.test

api/client.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,6 @@ func (c *Client) ListWorkspaces(teamId int) ([]Workspace, error) {
141141
return workspaces, err
142142
}
143143

144-
func (c *Client) WorkspaceStatus(workspaceId int) (*WorkspaceStatus, error) {
145-
status, _, err := c.api.WorkspacesAPI.WorkspacesGetWorkspaceStatus(c.ctx, float32(workspaceId)).Execute()
146-
return status, err
147-
}
148-
149-
func (c *Client) CreateWorkspace(args CreateWorkspaceArgs) (*Workspace, error) {
150-
workspace, _, err := c.api.WorkspacesAPI.
151-
WorkspacesCreateWorkspace(c.ctx).
152-
WorkspacesCreateWorkspaceRequest(args).
153-
Execute()
154-
return workspace, err
155-
}
156-
157144
func (c *Client) SetEnvVarOnWorkspace(workspaceId int, envVars map[string]string) error {
158145
vars := []openapi_client.WorkspacesListEnvVars200ResponseInner{}
159146
for k, v := range envVars {

cmd/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Client interface {
1818
ListTeams() ([]api.Team, error)
1919
ListWorkspaces(teamId int) ([]api.Workspace, error)
2020
StartPipelines(workspaceId int, pipelineStage string) error
21+
SetEnvVarOnWorkspace(workspaceId int, vars map[string]string) error
2122
}
2223

2324
func NewClient(opts GlobalOptions) (Client, error) {

cmd/mocks_test.go

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func Execute() {
3333
addLogCmd(rootCmd, opts)
3434
addListCmd(rootCmd, opts)
3535
addPipelinesCmd(rootCmd, opts)
36+
addSetEnvVarCmd(rootCmd, opts)
3637

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

cmd/set-env-vars.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Codesphere Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
type SetEnvVarCmd struct {
14+
opts SetEnvVarOptions
15+
cmd *cobra.Command
16+
}
17+
18+
type SetEnvVarOptions struct {
19+
GlobalOptions
20+
WorkspaceId *int
21+
EnvVar *[]string
22+
}
23+
24+
func addSetEnvVarCmd(p *cobra.Command, opts GlobalOptions) {
25+
l := SetEnvVarCmd{
26+
cmd: &cobra.Command{
27+
Use: "set-env",
28+
Short: "set env vars",
29+
Long: `set an environment variable for your workspace`,
30+
Example: `
31+
Set environment variable:
32+
33+
$ cs set env var --workspace-id <workspace-id> --name <env-var-name> --value <env-var-value>
34+
`,
35+
},
36+
opts: SetEnvVarOptions{GlobalOptions: opts},
37+
}
38+
l.cmd.RunE = l.RunE
39+
l.parseFlags()
40+
p.AddCommand(l.cmd)
41+
}
42+
43+
func (l *SetEnvVarCmd) parseFlags() {
44+
l.opts.WorkspaceId = l.cmd.Flags().IntP("workspace-id", "w", -1, "ID of workspace to set var")
45+
l.opts.EnvVar = l.cmd.Flags().StringArrayP("env-var", "e", []string{}, "env vars to set in form key=val")
46+
}
47+
48+
func (l *SetEnvVarCmd) RunE(_ *cobra.Command, args []string) (err error) {
49+
client, err := NewClient(l.opts.GlobalOptions)
50+
if err != nil {
51+
return fmt.Errorf("failed to create Codesphere client: %w", err)
52+
}
53+
envVarMap := map[string]string{}
54+
for _, x := range *l.opts.EnvVar {
55+
split := strings.Split(x, "=")
56+
envVarMap[split[0]] = split[1]
57+
}
58+
err = client.SetEnvVarOnWorkspace(*l.opts.WorkspaceId, envVarMap)
59+
if err != nil {
60+
return fmt.Errorf("failed to set environment variables %v: %w", envVarMap, err)
61+
}
62+
63+
return nil
64+
}

pkg/cs/workspace.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package cs
22

33
import (
4-
"fmt"
54
"time"
6-
7-
"github.com/codesphere-cloud/cs-go/api"
85
)
96

107
type WaitForWorkspaceRunningOptions struct {
@@ -15,38 +12,6 @@ type WaitForWorkspaceRunningOptions struct {
1512
// Waits for a given workspace to be running.
1613
//
1714
// Returns [TimedOut] error if the workspace does not become running in time.
18-
func WaitForWorkspaceRunning(
19-
client *api.Client,
20-
workspace *api.Workspace,
21-
opts WaitForWorkspaceRunningOptions,
22-
) error {
23-
timeout := opts.Timeout
24-
if timeout == 0 {
25-
timeout = 20 * time.Minute
26-
}
27-
delay := opts.Delay
28-
if delay == 0 {
29-
delay = 5 * time.Second
30-
}
31-
32-
maxWaitTime := time.Now().Add(timeout)
33-
for time.Now().Before(maxWaitTime) {
34-
status, err := client.WorkspaceStatus(workspace.Id)
35-
36-
if err != nil {
37-
// TODO: log error and retry until timeout is reached.
38-
return err
39-
}
40-
if status.IsRunning {
41-
return nil
42-
}
43-
time.Sleep(delay)
44-
}
45-
46-
return NewTimedOut(
47-
fmt.Sprintf("Waiting for workspace %s(%d) to be ready", workspace.Name, workspace.Id),
48-
timeout)
49-
}
5015

5116
type DeployWorkspaceArgs struct {
5217
TeamId int
@@ -57,37 +22,3 @@ type DeployWorkspaceArgs struct {
5722

5823
Timeout time.Duration
5924
}
60-
61-
// Deploys a workspace with the given configuration.
62-
//
63-
// Returns [TimedOut] error if the timeout is reached
64-
func DeployWorkspace(
65-
client api.Client,
66-
args DeployWorkspaceArgs,
67-
) error {
68-
workspace, err := client.CreateWorkspace(api.CreateWorkspaceArgs{
69-
TeamId: args.TeamId,
70-
Name: args.Name,
71-
PlanId: args.PlanId,
72-
IsPrivateRepo: true,
73-
GitUrl: nil,
74-
InitialBranch: nil,
75-
SourceWorkspaceId: nil,
76-
WelcomeMessage: nil,
77-
Replicas: 1,
78-
VpnConfig: args.VpnConfigName,
79-
})
80-
if err != nil {
81-
return err
82-
}
83-
if err := WaitForWorkspaceRunning(&client, workspace, WaitForWorkspaceRunningOptions{Timeout: args.Timeout}); err != nil {
84-
return err
85-
}
86-
87-
if len(args.EnvVars) != 0 {
88-
if err := client.SetEnvVarOnWorkspace(workspace.Id, args.EnvVars); err != nil {
89-
return err
90-
}
91-
}
92-
return nil
93-
}

0 commit comments

Comments
 (0)