-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add configs in the workspace mod definition (#1066) * feat: add the 'mod list' command (#1070)
- Loading branch information
Showing
20 changed files
with
508 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package mod | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pterm/pterm" | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericiooptions" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" | ||
"kusionstack.io/kusion/pkg/backend" | ||
"kusionstack.io/kusion/pkg/cmd/meta" | ||
"kusionstack.io/kusion/pkg/cmd/util" | ||
"kusionstack.io/kusion/pkg/util/i18n" | ||
) | ||
|
||
var listExample = i18n.T(`# List kusion modules in the current workspace | ||
kusion mod list | ||
# List modules in a specified workspace | ||
kusion mod list --workspace=dev | ||
`) | ||
var listShort = i18n.T("List kusion modules in a workspace ") | ||
|
||
type Flag struct { | ||
meta.MetaFlags | ||
} | ||
|
||
func (f *Flag) toOption(io genericiooptions.IOStreams) (*Options, error) { | ||
storageBackend, err := f.ParseBackend() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Get current workspace from backend | ||
workspace, err := f.ParseWorkspace(storageBackend) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Options{ | ||
Workspace: workspace, | ||
StorageBackend: storageBackend, | ||
IO: io, | ||
}, nil | ||
} | ||
|
||
type Options struct { | ||
// Workspace referenced the workspace for this CLI invocation. | ||
Workspace *v1.Workspace | ||
// StorageBackend referenced the target storage backend for this CLI invocation. | ||
StorageBackend backend.Backend | ||
IO genericiooptions.IOStreams | ||
} | ||
|
||
func (o *Options) Run() error { | ||
tableHeader := []string{"Name", "URL", "Version"} | ||
tableData := pterm.TableData{tableHeader} | ||
workspace := o.Workspace | ||
if workspace == nil { | ||
return fmt.Errorf("cannot find workspace with nil") | ||
} | ||
modules := workspace.Modules | ||
for k, module := range modules { | ||
tableData = append(tableData, []string{k, module.Path, module.Version}) | ||
} | ||
_ = pterm.DefaultTable.WithHasHeader(). | ||
WithHeaderStyle(&pterm.ThemeDefault.TableHeaderStyle). | ||
WithLeftAlignment(true). | ||
WithSeparator(" "). | ||
WithData(tableData). | ||
WithWriter(o.IO.Out). | ||
Render() | ||
pterm.Println() | ||
|
||
return nil | ||
} | ||
|
||
func NewCmdList(io genericiooptions.IOStreams) *cobra.Command { | ||
f := &Flag{ | ||
MetaFlags: *meta.NewMetaFlags(), | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list [WORKSPACE]", | ||
Short: listShort, | ||
Example: templates.Examples(listExample), | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
defer util.RecoverErr(&err) | ||
|
||
o, err := f.toOption(io) | ||
util.CheckErr(err) | ||
util.CheckErr(o.Run()) | ||
return | ||
}, | ||
} | ||
f.MetaFlags.AddFlags(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package mod | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/cli-runtime/pkg/genericiooptions" | ||
|
||
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" | ||
"kusionstack.io/kusion/pkg/backend/storages" | ||
"kusionstack.io/kusion/pkg/cmd/meta" | ||
) | ||
|
||
func TestFlag_ToOption(t *testing.T) { | ||
f := &Flag{ | ||
MetaFlags: *meta.NewMetaFlags(), | ||
} | ||
|
||
t.Run("Successful Option Creation", func(t *testing.T) { | ||
_, err := f.toOption(genericiooptions.IOStreams{}) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("Failed Option Creation Due to Invalid Backend", func(t *testing.T) { | ||
s := "invalid-backend" | ||
f.MetaFlags.Backend = &s | ||
_, err := f.toOption(genericiooptions.IOStreams{}) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestOptions_Run(t *testing.T) { | ||
o := &Options{ | ||
Workspace: &v1.Workspace{ | ||
Modules: map[string]*v1.ModuleConfig{ | ||
"module1": { | ||
Path: "path1", | ||
Version: "v1.0.0", | ||
}, | ||
}, | ||
}, | ||
StorageBackend: &storages.LocalStorage{}, | ||
IO: genericiooptions.IOStreams{}, | ||
} | ||
|
||
t.Run("Successful Run", func(t *testing.T) { | ||
err := o.Run() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("Failed Run Due to Nil Workspace", func(t *testing.T) { | ||
o.Workspace = nil | ||
err := o.Run() | ||
assert.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
modules: | ||
database: | ||
default: | ||
instanceType: db.t3.micro | ||
type: aws | ||
version: "5.7" | ||
smallClass: | ||
projectSelector: | ||
- foo | ||
- bar | ||
instanceType: db.t3.small | ||
port: | ||
default: | ||
type: aws | ||
mysql: | ||
path: ghcr.io/kusionstack/mysql | ||
version: 0.1.0 | ||
configs: | ||
default: | ||
instanceType: db.t3.micro | ||
type: aws | ||
version: '5.7' | ||
smallClass: | ||
projectSelector: | ||
- foo | ||
- bar | ||
instanceType: db.t3.small | ||
network: | ||
configs: | ||
default: | ||
type: aws | ||
runtimes: | ||
kubernetes: | ||
kubeConfig: /etc/kubeconfig.yaml | ||
terraform: | ||
aws: | ||
source: hashicorp/aws | ||
version: 1.0.4 | ||
region: us-east-1 | ||
backends: | ||
local: {} | ||
region: us-east-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.