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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go.work.sum
.env
.envrc
cs
!**/cs/

# openapi generator
openapitools.json
15 changes: 15 additions & 0 deletions .mockery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all: false
dir: '{{.InterfaceDir}}'
force-file-write: true
formatter: gofmt
log-level: info
structname: '{{.Mock}}{{.InterfaceName}}'
pkgname: '{{.SrcPackageName}}_test'
recursive: false
require-template-schema-exists: true
template: testify
template-schema: '{{.Template}}.schema.json'
packages:
github.com/codesphere-cloud/cs-go/cmd:
interfaces:
Client:
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

OPENAPI_DIR = ./pkg/api/openapi_client
OPENAPI_DIR = ./api/openapi_client

all: format build

Expand All @@ -12,6 +12,9 @@ lint:
test:
go test ./...

generate:
go generate ./...

build:
cd cmd/cs && go build
mv cmd/cs/cs .
Expand All @@ -37,4 +40,4 @@ generate-client:
${OPENAPI_DIR}/test


generate: generate-client format
generate-api: generate-client format
2 changes: 1 addition & 1 deletion pkg/api/client.go → api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"net/url"

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

type Client struct {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/api/types.go → api/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api

import (
openapi "github.com/codesphere-cloud/cs-go/pkg/api/openapi_client"
openapi "github.com/codesphere-cloud/cs-go/api/openapi_client"
)

type DataCenter = openapi.MetadataGetDatacenters200ResponseInner
Expand Down
33 changes: 33 additions & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

//go:generate mockery

import (
"context"
"fmt"
"net/url"

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

type Client interface {
ListTeams() ([]api.Team, error)
ListWorkspaces(teamId int) ([]api.Workspace, error)
}

func NewClient(opts GlobalOptions) (Client, error) {
token, err := cs.GetApiToken()
if err != nil {
return nil, fmt.Errorf("failed to get API token: %e", err)
}
apiUrl, err := url.Parse(opts.GetApiUrl())
if err != nil {
return nil, fmt.Errorf("failed to parse URL '%s': %e", opts.GetApiUrl(), err)
}
client := api.NewClient(context.Background(), api.Configuration{
BaseUrl: apiUrl,
Token: token,
})
return client, nil
}
6 changes: 2 additions & 4 deletions cmd/list-teams.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/*
Copyright © 2025 Codesphere Inc. <support@codesphere.com>
*/
package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/cs"
"github.com/codesphere-cloud/cs-go/pkg/out"
"github.com/jedib0t/go-pretty/v6/table"

Expand Down Expand Up @@ -53,7 +51,7 @@ func (l *ListTeamsCmd) RunE(_ *cobra.Command, args []string) (err error) {
if team.IsFirst != nil && *team.IsFirst {
first = "*"
}
t.AppendRow(table.Row{first, team.Id, team.Name, GetRoleName(int(team.Role)), team.DefaultDataCenterId})
t.AppendRow(table.Row{first, team.Id, team.Name, cs.GetRoleName(int(team.Role)), team.DefaultDataCenterId})
}
t.Render()

Expand Down
47 changes: 26 additions & 21 deletions cmd/list-workspaces.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/*
Copyright © 2025 Codesphere Inc. <support@codesphere.com>
*/
package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/api"
"github.com/codesphere-cloud/cs-go/api"
"github.com/codesphere-cloud/cs-go/pkg/out"
"github.com/jedib0t/go-pretty/v6/table"

"github.com/spf13/cobra"
)

type ListWorkspacesCmd struct {
Opts ListWorkspacesOptions
cmd *cobra.Command
opts ListWorkspacesOptions
}

type ListWorkspacesOptions struct {
Expand All @@ -35,34 +32,26 @@ List all workspaces:
$ cs list workspaces --team-id <team-id>
`,
},
opts: ListWorkspacesOptions{GlobalOptions: opts},
Opts: ListWorkspacesOptions{GlobalOptions: opts},
}
l.cmd.RunE = l.RunE
l.parseLogCmdFlags()
p.AddCommand(l.cmd)
}

func (l *ListWorkspacesCmd) parseLogCmdFlags() {
l.opts.TeamId = l.cmd.Flags().IntP("team-id", "t", -1, "ID of team to query")
l.Opts.TeamId = l.cmd.Flags().IntP("team-id", "t", -1, "ID of team to query")
}

func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
client, err := NewClient(l.opts.GlobalOptions)
client, err := NewClient(l.Opts.GlobalOptions)
if err != nil {
return fmt.Errorf("failed to create Codesphere client: %e", err)
}

teams, err := l.getTeamIds(client)
workspaces, err := l.ListWorkspaces(client)
if err != nil {
return fmt.Errorf("failed to get teams: %e", err)
}
workspaces := []api.Workspace{}
for _, team := range teams {
teamWorkspaces, err := client.ListWorkspaces(team)
if err != nil {
return fmt.Errorf("failed to list workspaces: %e", err)
}
workspaces = append(workspaces, teamWorkspaces...)
return fmt.Errorf("failed to list workspaces: %e", err)
}

t := out.GetTableWriter()
Expand All @@ -79,9 +68,25 @@ func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
return nil
}

func (l *ListWorkspacesCmd) getTeamIds(client *api.Client) (teams []int, err error) {
if l.opts.TeamId != nil && *l.opts.TeamId >= 0 {
teams = append(teams, *l.opts.TeamId)
func (l *ListWorkspacesCmd) ListWorkspaces(client Client) ([]api.Workspace, error) {
teams, err := l.getTeamIds(client)
if err != nil {
return nil, fmt.Errorf("failed to get teams: %e", err)
}
workspaces := []api.Workspace{}
for _, team := range teams {
teamWorkspaces, err := client.ListWorkspaces(team)
if err != nil {
return nil, fmt.Errorf("failed to list workspaces: %e", err)
}
workspaces = append(workspaces, teamWorkspaces...)
}
return workspaces, nil
}

func (l *ListWorkspacesCmd) getTeamIds(client Client) (teams []int, err error) {
if l.Opts.TeamId != nil && *l.Opts.TeamId >= 0 {
teams = append(teams, *l.Opts.TeamId)
return
}
var allTeams []api.Team
Expand Down
48 changes: 48 additions & 0 deletions cmd/list-workspaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd_test

import (
"testing"

"github.com/codesphere-cloud/cs-go/api"
"github.com/codesphere-cloud/cs-go/cmd"
"github.com/stretchr/testify/assert"
)

func TestListWorkspaces(t *testing.T) {
l := newListWorkspacesCmdWithTeam(0)
client := NewMockClient(t)
client.EXPECT().ListWorkspaces(0).Return([]api.Workspace{}, nil)

w, err := l.ListWorkspaces(client)
assert.Equal(t, w, []api.Workspace{}, "should return empty list of workspaces")
assert.Nil(t, err, "should be nil")
}

func TestListWorkspacesAllTeams(t *testing.T) {
l := newListWorkspacesCmd()
client := NewMockClient(t)
client.EXPECT().ListTeams().Return([]api.Team{{Id: 0}, {Id: 1}}, nil)

expectedWorkspaces := []api.Workspace{
{Id: 0, Name: "fakeForTeam0"},
{Id: 1, Name: "fakeForTeam1"},
}
client.EXPECT().ListWorkspaces(0).Return([]api.Workspace{expectedWorkspaces[0]}, nil)
client.EXPECT().ListWorkspaces(1).Return([]api.Workspace{expectedWorkspaces[1]}, nil)

w, err := l.ListWorkspaces(client)
assert.Equal(t, w, expectedWorkspaces, "should return both workspaces")
assert.Nil(t, err, "should be nil")
}

func newListWorkspacesCmdWithTeam(teamId int) cmd.ListWorkspacesCmd {
return cmd.ListWorkspacesCmd{
Opts: cmd.ListWorkspacesOptions{
TeamId: &teamId,
},
}
}

func newListWorkspacesCmd() cmd.ListWorkspacesCmd {
return cmd.ListWorkspacesCmd{}
}
3 changes: 0 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2025 Codesphere Inc. <support@codesphere.com>
*/
package cmd

import (
Expand Down
8 changes: 3 additions & 5 deletions cmd/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2025 Codesphere Inc. <support@codesphere.com>
*/
package cmd

import (
Expand All @@ -17,6 +14,7 @@ import (
"strings"
"sync"

"github.com/codesphere-cloud/cs-go/pkg/cs"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -125,7 +123,7 @@ func (l *LogCmd) RunE(_ *cobra.Command, args []string) (err error) {
func (l *LogCmd) printAllLogs() error {
fmt.Println("Printing logs of all replicas")

replicas, err := GetPipelineStatus(*l.scope.workspaceId, "run")
replicas, err := cs.GetPipelineStatus(*l.scope.workspaceId, "run")
if err != nil {
return fmt.Errorf("failed to get pipeline status: %e", err)
}
Expand Down Expand Up @@ -186,7 +184,7 @@ func printLogsOfEndpoint(prefix string, endpoint string) error {

// Set the Accept header to indicate SSE
req.Header.Set("Accept", "text/event-stream")
err = SetAuthoriziationHeader(req)
err = cs.SetAuthoriziationHeader(req)
if err != nil {
return fmt.Errorf("failed to set header: %e", err)
}
Expand Down
Loading