Feat: support read from URL for --file command globally (#4346)

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
This commit is contained in:
Jianbo Sun
2022-07-11 16:59:54 +08:00
committed by GitHub
parent 0cb85d897a
commit 4bd4e9014f
12 changed files with 61 additions and 101 deletions

View File

@@ -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 {

View File

@@ -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 <addon-name>
Enable addon with specify version:
Enable addon with specify version:
vela addon enable <addon-name> --version <addon-version>
Enable addon for specific clusters, (local means control plane):
Enable addon for specific clusters, (local means control plane):
vela addon enable <addon-name> --clusters={local,cluster1,cluster2}
Enable addon locally:
vela addon enable <your-local-addon-path>
Enable addon with specified args (the args should be defined in addon's parameters):
vela addon enable <addon-name> <my-parameter-of-addon>=<my-value>
`,
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 <addon-name>
Upgrade addon with specify version:
Upgrade addon with specify version:
vela addon upgrade <addon-name> --version <addon-version>
Upgrade addon for specific clusters, (local means control plane):
Upgrade addon for specific clusters, (local means control plane):
vela addon upgrade <addon-name> --clusters={local,cluster1,cluster2}
Upgrade addon locally:
vela addon enable <your-local-addon-path>
Upgrade addon with specified args (the args should be defined in addon's parameters):
vela addon enable <addon-name> <my-parameter-of-addon>=<my-value>
`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {

View File

@@ -127,7 +127,6 @@ func NewCommandWithIOStreams(ioStream util.IOStreams) *cobra.Command {
NewHelpCommand(),
// hide
NewTemplateCommand(ioStream),
NewWorkloadsCommand(commandArgs, ioStream),
)

View File

@@ -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-host-to-def>/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])
}

View File

@@ -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 <definition file or directory> -f /path/to/app.yaml
You can also specify a remote url for app:
vela dry-run -d <definition file or directory> -f https://<remote-host>/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
}

View File

@@ -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"

View File

@@ -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"

View File

@@ -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

View File

@@ -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
}

View File

@@ -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"

View File

@@ -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 | vela up vela up -f -
... <app.yaml here> ...
EOF
`))
)
// NewUpCommand will create command for applying an AppFile

View File

@@ -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 "<inner-view-name>{<param1>=<value1>,<param2>=<value2>}
vela ql --file ./ql.cue`,
Example: `Users can query with a query statement:
Example: ` Users can query with a query statement:
vela ql --query "<inner-view-name>{<param1>=<value1>,<param2>=<value2>}"
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://<my-host-to-cue>/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