Files
kubevela/references/cli/up.go
chival 22d014d91a Align vela cli and kubectl vela client tools (#1827)
* allow vela CLI to specify NS

* vela up support application yaml

* fix

* add default cap center to vela CLI

* add alias for `vela components`(`vela comp`,or `vela component`) and `vela traits`(`vela trait`)

* fix cap ls STATUS fields are always "uninstalled"

* fix vela up process

* Revert "allow vela CLI to specify NS"

This reverts commit 33f27362

will refactor to use Initializer

* add --discover for vela CLI

* * rfc capcenter to reuse registry type
* change default cap center to oss

* judge if application file in advance

* fix CI

* try CI

* fix error check
2021-06-29 15:52:43 +08:00

93 lines
2.4 KiB
Go

/*
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 (
"io/ioutil"
"path/filepath"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/spf13/cobra"
corev1beta1 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/apis/types"
common2 "github.com/oam-dev/kubevela/pkg/utils/common"
cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
"github.com/oam-dev/kubevela/references/common"
)
var (
appFilePath string
)
// NewUpCommand will create command for applying an AppFile
func NewUpCommand(c common2.Args, ioStream cmdutil.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "up",
DisableFlagsInUseLine: true,
Short: "Apply an appfile",
Long: "Apply an appfile",
Annotations: map[string]string{
types.TagCommandType: types.TypeStart,
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return c.SetConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
velaEnv, err := GetEnv(cmd)
if err != nil {
return err
}
kubecli, err := c.GetClient()
if err != nil {
return err
}
filePath, err := cmd.Flags().GetString(appFilePath)
if err != nil {
return err
}
fileContent, err := ioutil.ReadFile(filepath.Clean(filePath))
if err != nil {
return err
}
var app corev1beta1.Application
err = yaml.Unmarshal(fileContent, &app)
if err != nil {
return errors.Wrap(err, "File format is illegal")
}
if app.APIVersion != "" && app.Kind != "" {
err = common.ApplyApplication(app, ioStream, kubecli)
if err != nil {
return err
}
} else {
o := &common.AppfileOptions{
Kubecli: kubecli,
IO: ioStream,
Env: velaEnv,
}
return o.Run(filePath, velaEnv.Namespace, c)
}
return nil
},
}
cmd.SetOut(ioStream.Out)
cmd.Flags().StringP(appFilePath, "f", "", "specify file path for appfile")
return cmd
}