diff --git a/pkg/utils/file.go b/pkg/utils/file.go index 3ac3e4b8e..b5db81ba5 100644 --- a/pkg/utils/file.go +++ b/pkg/utils/file.go @@ -43,6 +43,14 @@ type FileData struct { // If path is a file, fetch the data from the file // If path is a dir, fetch the data from all the files inside the dir that passes the pathFilter func LoadDataFromPath(ctx context.Context, path string, pathFilter func(string) bool) ([]FileData, error) { + if path == "-" { + bs, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return nil, fmt.Errorf("failed to get data from stdin: %w", err) + } + return []FileData{{Path: path, Data: bs}}, nil + } + if IsValidURL(path) { bs, err := common.HTTPGetWithOption(ctx, path, nil) if err != nil { diff --git a/references/cli/addon.go b/references/cli/addon.go index 4b5118820..9fb8addc0 100644 --- a/references/cli/addon.go +++ b/references/cli/addon.go @@ -131,12 +131,16 @@ func NewAddonEnableCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra.Com Use: "enable", Short: "enable an addon", Long: "enable an addon in cluster.", - Example: `Enable addon by: + Example: ` Enable addon by: vela addon enable -Enable addon with specify version: + Enable addon with specify version: vela addon enable --version -Enable addon for specific clusters, (local means control plane): + Enable addon for specific clusters, (local means control plane): vela addon enable --clusters={local,cluster1,cluster2} + Enable addon locally: + vela addon enable + Enable addon with specified args (the args should be defined in addon's parameters): + vela addon enable = `, RunE: func(cmd *cobra.Command, args []string) error { @@ -229,12 +233,16 @@ func NewAddonUpgradeCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra.Co Short: "upgrade an addon", Long: "upgrade an addon in cluster.", Example: ` -Upgrade addon by: + Upgrade addon by: vela addon upgrade -Upgrade addon with specify version: + Upgrade addon with specify version: vela addon upgrade --version -Upgrade addon for specific clusters, (local means control plane): + Upgrade addon for specific clusters, (local means control plane): vela addon upgrade --clusters={local,cluster1,cluster2} + Upgrade addon locally: + vela addon enable + Upgrade addon with specified args (the args should be defined in addon's parameters): + vela addon enable = `, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { diff --git a/references/cli/cli.go b/references/cli/cli.go index a4946b542..5944ad3f1 100644 --- a/references/cli/cli.go +++ b/references/cli/cli.go @@ -127,7 +127,6 @@ func NewCommandWithIOStreams(ioStream util.IOStreams) *cobra.Command { NewHelpCommand(), // hide - NewTemplateCommand(ioStream), NewWorkloadsCommand(commandArgs, ioStream), ) diff --git a/references/cli/def.go b/references/cli/def.go index 776de0279..c626757de 100644 --- a/references/cli/def.go +++ b/references/cli/def.go @@ -55,6 +55,7 @@ import ( addonutil "github.com/oam-dev/kubevela/pkg/utils/addon" "github.com/oam-dev/kubevela/pkg/utils/common" "github.com/oam-dev/kubevela/pkg/utils/filters" + clicom "github.com/oam-dev/kubevela/references/common" "github.com/oam-dev/kubevela/references/plugins" ) @@ -112,15 +113,8 @@ func getPrompt(cmd *cobra.Command, reader *bufio.Reader, description string, pro } } -func loadYAMLBytesFromFileOrHTTP(pathOrURL string) ([]byte, error) { - if strings.HasPrefix(pathOrURL, "http://") || strings.HasPrefix(pathOrURL, "https://") { - return common.HTTPGetWithOption(context.Background(), pathOrURL, nil) - } - return os.ReadFile(path.Clean(pathOrURL)) -} - func buildTemplateFromYAML(templateYAML string, def *pkgdef.Definition) error { - templateYAMLBytes, err := loadYAMLBytesFromFileOrHTTP(templateYAML) + templateYAMLBytes, err := clicom.ReadRemoteOrLocalPath(templateYAML) if err != nil { return errors.Wrapf(err, "failed to get template YAML file %s", templateYAML) } @@ -787,7 +781,7 @@ func NewDefinitionRenderCommand(c common.Args) *cobra.Command { } render := func(inputFilename, outputFilename string) error { - cueBytes, err := loadYAMLBytesFromFileOrHTTP(inputFilename) + cueBytes, err := clicom.ReadRemoteOrLocalPath(inputFilename) if err != nil { return errors.Wrapf(err, "failed to get %s", args[0]) } @@ -881,7 +875,11 @@ func NewDefinitionApplyCommand(c common.Args) *cobra.Command { "# Command below will apply the ./defs/my-trait.cue file to kubernetes default namespace\n" + "> vela def apply ./defs/my-trait.cue --namespace default" + "# Command below will convert the ./defs/my-trait.cue file to kubernetes CRD object and print it without applying it to kubernetes\n" + - "> vela def apply ./defs/my-trait.cue --dry-run", + "> vela def apply ./defs/my-trait.cue --dry-run" + + "# Apply a CUE from URL \n" + + "> vela def apply https:///my-trait.cue --dry-run" + + "# Apply a CUE from stdin \n" + + "> vela def apply -", Args: cobra.ExactValidArgs(1), RunE: func(cmd *cobra.Command, args []string) error { dryRun, err := cmd.Flags().GetBool(FlagDryRun) @@ -900,8 +898,7 @@ func NewDefinitionApplyCommand(c common.Args) *cobra.Command { if err != nil { return errors.Wrapf(err, "failed to get k8s client") } - - cueBytes, err := loadYAMLBytesFromFileOrHTTP(args[0]) + cueBytes, err := clicom.ReadRemoteOrLocalPath(args[0]) if err != nil { return errors.Wrapf(err, "failed to get %s", args[0]) } diff --git a/references/cli/dryrun.go b/references/cli/dryrun.go index 262550cc1..e1356d5f3 100644 --- a/references/cli/dryrun.go +++ b/references/cli/dryrun.go @@ -37,6 +37,7 @@ import ( oamutil "github.com/oam-dev/kubevela/pkg/oam/util" "github.com/oam-dev/kubevela/pkg/utils/common" cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" + clicom "github.com/oam-dev/kubevela/references/common" ) // DryRunCmdOptions contains dry-run cmd options @@ -54,8 +55,13 @@ func NewDryRunCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Command Use: "dry-run", DisableFlagsInUseLine: true, Short: "Dry Run an application, and output the K8s resources as result to stdout", - Long: "Dry-run application locally, render the Kubernetes resources as result to stdout.", - Example: "vela dry-run", + Long: `Dry-run application locally, render the Kubernetes resources as result to stdout. + vela dry-run -d -f /path/to/app.yaml + +You can also specify a remote url for app: + vela dry-run -d -f https:///app.yaml +`, + Example: "vela dry-run", Annotations: map[string]string{ types.TagCommandType: types.TypeApp, }, @@ -194,8 +200,7 @@ func ReadObjectsFromFile(path string) ([]oam.Object, error) { } func readApplicationFromFile(filename string) (*corev1beta1.Application, error) { - - fileContent, err := os.ReadFile(filepath.Clean(filename)) + fileContent, err := clicom.ReadRemoteOrLocalPath(filename) if err != nil { return nil, err } diff --git a/references/cli/logs.go b/references/cli/logs.go index a94eab889..7f827576e 100644 --- a/references/cli/logs.go +++ b/references/cli/logs.go @@ -24,8 +24,6 @@ import ( "text/template" "time" - "github.com/oam-dev/kubevela/pkg/multicluster" - "github.com/fatih/color" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -35,6 +33,7 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/apis/types" + "github.com/oam-dev/kubevela/pkg/multicluster" "github.com/oam-dev/kubevela/pkg/utils/common" "github.com/oam-dev/kubevela/pkg/utils/util" "github.com/oam-dev/kubevela/references/appfile" diff --git a/references/cli/ls.go b/references/cli/ls.go index 8858ad3f6..3360dd023 100644 --- a/references/cli/ls.go +++ b/references/cli/ls.go @@ -21,7 +21,6 @@ import ( "strings" "github.com/gosuri/uitable" - "github.com/spf13/cobra" apierrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" diff --git a/references/cli/registry.go b/references/cli/registry.go index f3d0f28ec..da2fe330c 100644 --- a/references/cli/registry.go +++ b/references/cli/registry.go @@ -30,6 +30,8 @@ import ( "strings" "github.com/google/go-github/v32/github" + "github.com/pkg/errors" + "github.com/spf13/cobra" "golang.org/x/oauth2" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/yaml" @@ -40,13 +42,9 @@ import ( "github.com/oam-dev/kubevela/pkg/oam/util" "github.com/oam-dev/kubevela/pkg/utils/common" "github.com/oam-dev/kubevela/pkg/utils/system" + cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" "github.com/oam-dev/kubevela/references/apis" "github.com/oam-dev/kubevela/references/plugins" - - "github.com/pkg/errors" - "github.com/spf13/cobra" - - cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" ) // NewRegistryCommand Manage Capability Center diff --git a/references/cli/template.go b/references/cli/template.go deleted file mode 100644 index db5aad472..000000000 --- a/references/cli/template.go +++ /dev/null @@ -1,60 +0,0 @@ -/* -Copyright 2021 The KubeVela Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package cli - -import ( - "github.com/spf13/cobra" - - "github.com/oam-dev/kubevela/apis/types" - "github.com/oam-dev/kubevela/pkg/cue" - cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" -) - -// NewTemplateCommand creates `template` command and its nested children command -func NewTemplateCommand(ioStream cmdutil.IOStreams) *cobra.Command { - cmd := &cobra.Command{ - Use: "template", - DisableFlagsInUseLine: true, - Short: "Manage templates", - Long: "Manage templates", - Hidden: true, - Annotations: map[string]string{}, - } - cmd.SetOut(ioStream.Out) - cmd.AddCommand(NewTemplateContextCommand(ioStream)) - return cmd -} - -// NewTemplateContextCommand creates `context` command -func NewTemplateContextCommand(ioStream cmdutil.IOStreams) *cobra.Command { - cmd := &cobra.Command{ - Use: "context", - DisableFlagsInUseLine: true, - Short: "Show context parameters", - Long: "Show context parameter", - Example: `vela template context`, - Annotations: map[string]string{ - types.TagCommandType: types.TypeSystem, - }, - RunE: func(cmd *cobra.Command, args []string) error { - ioStream.Info(cue.BaseTemplate) - return nil - }, - } - cmd.SetOut(ioStream.Out) - return cmd -} diff --git a/references/cli/uninstall.go b/references/cli/uninstall.go index cd9a4c00a..90ba03b8a 100644 --- a/references/cli/uninstall.go +++ b/references/cli/uninstall.go @@ -21,14 +21,13 @@ import ( "fmt" "time" - "k8s.io/client-go/rest" - "github.com/pkg/errors" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" apierror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" apitypes "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" diff --git a/references/cli/up.go b/references/cli/up.go index 934139c71..fabf41d9b 100644 --- a/references/cli/up.go +++ b/references/cli/up.go @@ -264,7 +264,13 @@ var ( vela up example-app -n example-ns --publish-version beta # Deploy an application using existing revision - vela up example-app -n example-ns --publish-version beta --revision example-app-v2`)) + vela up example-app -n example-ns --publish-version beta --revision example-app-v2 + + # Deploy an application from stdin + cat < ... + EOF +`)) ) // NewUpCommand will create command for applying an AppFile diff --git a/references/cli/velaql.go b/references/cli/velaql.go index 64d1947d9..8f20cef92 100644 --- a/references/cli/velaql.go +++ b/references/cli/velaql.go @@ -27,10 +27,9 @@ import ( "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/oam-dev/kubevela/pkg/utils" - "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/cue/model/value" + "github.com/oam-dev/kubevela/pkg/utils" "github.com/oam-dev/kubevela/pkg/utils/common" "github.com/oam-dev/kubevela/pkg/utils/util" "github.com/oam-dev/kubevela/pkg/velaql" @@ -59,11 +58,14 @@ func NewQlCommand(c common.Args, order string, ioStreams util.IOStreams) *cobra. Long: `Show result of executing velaQL, use it like: vela ql --query "{=,=} vela ql --file ./ql.cue`, - Example: `Users can query with a query statement: + Example: ` Users can query with a query statement: vela ql --query "{=,=}" -They can also query by a ql file: + Query by a ql file: vela ql --file ./ql.cue + Query by a ql file from remote url: + vela ql --file https:///ql.cue + Query by a ql file from stdin: cat ./ql.cue | vela ql --file - Example content of ql.cue: @@ -209,7 +211,7 @@ func queryFromStatement(ctx context.Context, client client.Client, velaC common. if err != nil { return err } - return print(queryValue, cmd) + return printValue(queryValue, cmd) } // queryFromView print velaQL result from query view @@ -222,10 +224,10 @@ func queryFromView(ctx context.Context, client client.Client, velaC common.Args, if err != nil { return err } - return print(queryValue, cmd) + return printValue(queryValue, cmd) } -func print(queryValue *value.Value, cmd *cobra.Command) error { +func printValue(queryValue *value.Value, cmd *cobra.Command) error { response, err := queryValue.CueValue().MarshalJSON() if err != nil { return err