forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathworkspaces.go
More file actions
104 lines (89 loc) · 2.59 KB
/
workspaces.go
File metadata and controls
104 lines (89 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package list
import (
"context"
"encoding/json"
"fmt"
"os"
managementv1 "github.com/loft-sh/api/v4/pkg/apis/management/v1"
"github.com/skevetter/devpod/cmd/pro/flags"
"github.com/skevetter/devpod/pkg/config"
"github.com/skevetter/devpod/pkg/platform"
"github.com/skevetter/devpod/pkg/platform/client"
"github.com/skevetter/devpod/pkg/platform/labels"
"github.com/skevetter/devpod/pkg/platform/project"
"github.com/skevetter/log"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// WorkspacesCmd holds the cmd flags.
type WorkspacesCmd struct {
*flags.GlobalFlags
log log.Logger
}
// NewWorkspacesCmd creates a new command.
func NewWorkspacesCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &WorkspacesCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
}
c := &cobra.Command{
Use: "workspaces",
Short: "Lists workspaces for the provider",
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd.Context())
},
}
return c
}
func (cmd *WorkspacesCmd) Run(ctx context.Context) error {
baseClient, err := client.InitClientFromPath(ctx, cmd.Config)
if err != nil {
return err
}
managementClient, err := baseClient.Management()
if err != nil {
return err
}
projectList, err := managementClient.Loft().
ManagementV1().
Projects().
List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("list projects: %w", err)
} else if len(projectList.Items) == 0 {
return fmt.Errorf(
"you don't have access to any projects within DevPod Pro, please make sure you have at least access to 1 project",
)
}
filterByOwner := os.Getenv(config.EnvLoftFilterByOwner) == config.BoolTrue
workspaces := []*managementv1.DevPodWorkspaceInstance{}
for _, p := range projectList.Items {
ns := project.ProjectNamespace(p.GetName())
workspaceList, err := managementClient.Loft().
ManagementV1().
DevPodWorkspaceInstances(ns).
List(ctx, metav1.ListOptions{})
if err != nil {
cmd.log.Info("list workspaces in project \"%s\": %w", p.GetName(), err)
continue
}
for _, instance := range workspaceList.Items {
instance := &instance
if filterByOwner && !platform.IsOwner(baseClient.Self(), instance.GetOwner()) {
continue
}
if instance.GetLabels() == nil {
instance.Labels = map[string]string{}
}
instance.Labels[labels.ProjectLabel] = p.GetName()
workspaces = append(workspaces, instance)
}
}
wBytes, err := json.Marshal(workspaces)
if err != nil {
return fmt.Errorf("marshal workspaces: %w", err)
}
fmt.Println(string(wBytes))
return nil
}