Add --spec to package command (#1411)

This commit is contained in:
Ta-Ching Chen
2019-11-18 11:15:48 +08:00
committed by GitHub
parent f8e98f9f14
commit 66b9dfa8e9
9 changed files with 238 additions and 73 deletions
+2 -2
View File
@@ -31,8 +31,8 @@ func Commands() *cobra.Command {
}
wrapper.SetFlags(createCmd, flag.FlagSet{
Required: []flag.Flag{flag.PkgEnvironment},
Optional: []flag.Flag{flag.PkgName, flag.PkgSrcArchive, flag.PkgDeployArchive,
flag.PkgBuildCmd, flag.NamespacePackage, flag.NamespaceEnvironment},
Optional: []flag.Flag{flag.PkgName, flag.PkgCode, flag.PkgSrcArchive, flag.PkgDeployArchive,
flag.PkgBuildCmd, flag.NamespacePackage, flag.NamespaceEnvironment, flag.SpecSave},
})
getSrcCmd := &cobra.Command{
+29 -7
View File
@@ -62,7 +62,11 @@ func (opts *CreateSubCommand) do(input cli.Input) error {
func (opts *CreateSubCommand) run(input cli.Input) error {
pkgName := input.String(flagkey.PkgName)
if len(pkgName) == 0 {
console.Warn(fmt.Sprintf("--%v will be soon marked as required flag, see 'help' for details", flagkey.HtName))
if input.Bool(flagkey.SpecSave) && len(input.String(flagkey.PkgName)) == 0 {
return errors.Errorf("--%v is necessary when creating spec file", flagkey.PkgName)
} else {
console.Warn(fmt.Sprintf("--%v will be soon marked as required flag, see 'help' for details", flagkey.HtName))
}
}
pkgNamespace := input.String(flagkey.NamespacePackage)
envName := input.String(flagkey.PkgEnvironment)
@@ -71,12 +75,27 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
deployArchiveFiles := input.StringSlice(flagkey.PkgDeployArchive)
buildcmd := input.String(flagkey.PkgBuildCmd)
noZip := false
code := input.String(flagkey.PkgCode)
if len(code) == 0 {
deployArchiveFiles = input.StringSlice(flagkey.PkgDeployArchive)
} else {
deployArchiveFiles = append(deployArchiveFiles, input.String(flagkey.PkgCode))
noZip = true
}
if len(srcArchiveFiles) == 0 && len(deployArchiveFiles) == 0 {
return errors.Errorf("need --%v or --%v flag", flagkey.PkgSrcArchive, flagkey.PkgDeployArchive)
return errors.Errorf("need --%v or --%v or --%v argument", flagkey.PkgCode, flagkey.PkgSrcArchive, flagkey.PkgDeployArchive)
}
var specDir, specFile string
if input.Bool(flagkey.SpecSave) {
specDir = util.GetSpecDir(input)
specFile = fmt.Sprintf("package-%v.yaml", pkgName)
}
_, err := CreatePackage(input, opts.client, pkgName, pkgNamespace, envName, envNamespace,
srcArchiveFiles, deployArchiveFiles, buildcmd, "", "", false)
srcArchiveFiles, deployArchiveFiles, buildcmd, specDir, specFile, noZip)
return err
}
@@ -138,14 +157,17 @@ func CreatePackage(input cli.Input, client *client.Client, pkgName string, pkgNa
}
if len(specFile) > 0 {
// if a package sith the same spec exists, don't create a new spec file
// if a package with the same spec exists, don't create a new spec file
fr, err := spec.ReadSpecs(util.GetSpecDir(input))
if err != nil {
return nil, errors.Wrap(err, "error reading specs")
}
if m := fr.SpecExists(pkg, false, true); m != nil {
fmt.Printf("Re-using previously created package %v\n", m.Name)
return m, nil
obj := fr.SpecExists(pkg, true, true)
if obj != nil {
pkg := obj.(*fv1.Package)
fmt.Printf("Re-using previously created package %v\n", pkg.Metadata.Name)
return &pkg.Metadata, nil
}
err = spec.SpecSave(*pkg, specFile)
+32 -4
View File
@@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/dchest/uniuri"
"github.com/hashicorp/go-multierror"
@@ -41,6 +42,17 @@ import (
// upload the archive using client. noZip avoids zipping the
// includeFiles, but is ignored if there's more than one includeFile.
func CreateArchive(client *client.Client, includeFiles []string, noZip bool, specDir string, specFile string) (*fv1.Archive, error) {
// get root dir
var rootDir string
var err error
if len(specFile) > 0 {
rootDir, err = filepath.Abs(specDir + "/..")
if err != nil {
return nil, errors.Wrapf(err, "error getting root directory of spec directory")
}
}
errs := utils.MultiErrorWithFormat()
fileURL := ""
@@ -57,9 +69,22 @@ func CreateArchive(client *client.Client, includeFiles []string, noZip bool, spe
}
// Get files from inputs as number of files decide next steps
absPath, err := filepath.Abs(path)
if err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "error converting path to the absolute path \"%v\"", path))
continue
}
if !strings.HasPrefix(absPath, rootDir) {
errs = multierror.Append(errs, errors.Errorf("The files (%v) should be put under the same parent directory (%v) of spec directory; otherwise, the archive will be empty when applying spec files", path, rootDir))
continue
}
path := filepath.Join(rootDir, path)
files, err := utils.FindAllGlobs([]string{path})
if err != nil {
return nil, errors.Wrap(err, "error finding all globs")
errs = multierror.Append(errs, errors.Wrap(err, "error finding all globs"))
continue
}
if len(files) == 0 {
@@ -89,9 +114,12 @@ func CreateArchive(client *client.Client, includeFiles []string, noZip bool, spe
if err != nil {
return nil, errors.Wrap(err, "error reading specs")
}
if m := fr.SpecExists(aus, false, true); m != nil {
fmt.Printf("Re-using previously created archive %v\n", m.Name)
aus.Name = m.Name
obj := fr.SpecExists(aus, true, true)
if obj != nil {
oldAus := obj.(*spectypes.ArchiveUploadSpec)
fmt.Printf("Re-using previously created archive %v\n", oldAus.Name)
aus.Name = oldAus.Name
} else {
// save the uploadspec
err := spec.SpecSave(*aus, specFile)