Skip to content

Commit

Permalink
fix cosmoctl get ws
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed Aug 3, 2023
1 parent e34f63c commit 1eee651
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 50 deletions.
44 changes: 12 additions & 32 deletions internal/cmd/__snapshots__/workspace_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -589,28 +589,28 @@ unknown shorthand flag: 'A' in -A"""

['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --namespace cosmo-user-user1 1']
SnapShot = """
USER NAME TEMPLATE PHASE
user1 ws1 template1 Pending
user1 ws2 template1 Pending
NAME TEMPLATE PHASE
ws1 template1 Pending
ws2 template1 Pending
"""
['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --namespace cosmo-user-user1 ws2 1']
SnapShot = """
USER NAME TEMPLATE PHASE
user1 ws2 template1 Pending
NAME TEMPLATE PHASE
ws2 template1 Pending
"""
['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --user user1 1']
SnapShot = """
USER NAME TEMPLATE PHASE
user1 ws1 template1 Pending
user1 ws2 template1 Pending
NAME TEMPLATE PHASE
ws1 template1 Pending
ws2 template1 Pending
"""
['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --user user1 ws2 1']
SnapShot = """
USER NAME TEMPLATE PHASE
user1 ws2 template1 Pending
NAME TEMPLATE PHASE
ws2 template1 Pending
"""
['cosmoctl [workspace] [get] ✅ success in normal context: workspace get -A --network 1']
Expand All @@ -636,12 +636,12 @@ USER NAME TEMPLATE PHASE
['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get --namespace cosmo-user-user1 1']
SnapShot = """
USER NAME TEMPLATE PHASE
NAME TEMPLATE PHASE
"""
['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get --user user1 1']
SnapShot = """
USER NAME TEMPLATE PHASE
NAME TEMPLATE PHASE
"""
['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get -A --network 1']
Expand Down Expand Up @@ -809,26 +809,6 @@ Global Flags:

"""
['cosmoctl [workspace] [get] ❌ fail with invalid args: workspace get -A ws1 1']
SnapShot = """
Error: invalid options: --all-namespaces is not allowed to use if WORKSPACE_NAME specified
Usage:
cosmoctl workspace get [WORKSPACE_NAME] [flags]

Flags:
-A, --all-namespaces all namespaces
-h, --help help for get
-n, --namespace string namespace
--network show workspace network
-u, --user string user name

Global Flags:
--context string kube-context (default: current context)
--kubeconfig string kubeconfig file path (default: $HOME/.kube/config)
-v, --verbose int log level. -1:DISABLED, 0:INFO, 1:DEBUG, 2:ALL (default -1)

"""
['cosmoctl [workspace] [run-instance] ✅ success in normal context: workspace run-instance ws1 --user user1 1']
SnapShot = """
\u001B[32mSuccessfully run workspace ws1
Expand Down
53 changes: 36 additions & 17 deletions internal/cmd/workspace/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package workspace

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -61,9 +60,6 @@ func (o *GetOption) Complete(cmd *cobra.Command, args []string) error {
}
if len(args) > 0 {
o.WorkspaceName = args[0]
if o.AllNamespace {
return errors.New("--all-namespaces is not allowed to use if WORKSPACE_NAME specified")
}
}
return nil
}
Expand Down Expand Up @@ -120,24 +116,47 @@ func (o *GetOption) RunE(cmd *cobra.Command, args []string) error {
defer w.Flush()

if o.showNetwork {
columnNames := []string{"USER", "NAME", "PORT", "URL", "PUBLIC"}
fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t"))

for _, ws := range wss {
for _, v := range ws.Spec.Network {
url := ws.Status.URLs[v.UniqueKey()]
rowdata := []string{cosmov1alpha1.UserNameByNamespace(ws.Namespace), ws.Name, strconv.Itoa(int(v.PortNumber)), url, strconv.FormatBool(v.Public)}
fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t"))
if o.AllNamespace {
columnNames := []string{"USER", "NAME", "PORT", "URL", "PUBLIC"}
fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t"))

for _, ws := range wss {
for _, v := range ws.Spec.Network {
url := ws.Status.URLs[v.UniqueKey()]
rowdata := []string{cosmov1alpha1.UserNameByNamespace(ws.Namespace), ws.Name, strconv.Itoa(int(v.PortNumber)), url, strconv.FormatBool(v.Public)}
fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t"))
}
}
} else {
columnNames := []string{"INDEX", "NAME", "PORT", "URL", "PUBLIC"}
fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t"))

for _, ws := range wss {
for i, v := range ws.Spec.Network {
url := ws.Status.URLs[v.UniqueKey()]
rowdata := []string{fmt.Sprint(i), ws.Name, strconv.Itoa(int(v.PortNumber)), url, strconv.FormatBool(v.Public)}
fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t"))
}
}
}

} else {
columnNames := []string{"USER", "NAME", "TEMPLATE", "PHASE"}
fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t"))
if o.AllNamespace {
columnNames := []string{"USER", "NAME", "TEMPLATE", "PHASE"}
fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t"))

for _, ws := range wss {
rowdata := []string{cosmov1alpha1.UserNameByNamespace(ws.Namespace), ws.Name, ws.Spec.Template.Name, string(ws.Status.Phase)}
fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t"))
}
} else {
columnNames := []string{"NAME", "TEMPLATE", "PHASE"}
fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t"))

for _, ws := range wss {
rowdata := []string{cosmov1alpha1.UserNameByNamespace(ws.Namespace), ws.Name, ws.Spec.Template.Name, string(ws.Status.Phase)}
fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t"))
for _, ws := range wss {
rowdata := []string{ws.Name, ws.Spec.Template.Name, string(ws.Status.Phase)}
fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t"))
}
}
}
return nil
Expand Down
1 change: 0 additions & 1 deletion internal/cmd/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ var _ = Describe("cosmoctl [workspace]", func() {
Ω(err).Should(HaveOccurred())
Expect(consoleOut()).To(MatchSnapShot())
},
Entry(desc, "workspace", "get", "-A", "ws1"),
Entry(desc, "workspace", "get", "--namespace", "cosmo-user-user1", "--user", "user1"),
Entry(desc, "workspace", "get", "--namespace", "xxx"),
Entry(desc, "workspace", "get", "-A", "--user", "user1"),
Expand Down

0 comments on commit 1eee651

Please sign in to comment.