From 7d8bd1e055451d06dd55e6334c43d46261749ed7 Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Wed, 25 Mar 2020 10:34:41 -0700 Subject: [PATCH] chore(pkger): refactor influx pkg cmd to not validate in CLI and have server validate the pkger.ValidSkipParseError option allows our server to be the one to validate the the pkg is accurate. If a user has an older version of the UI and our cloud gets updated with new validation rules,they'll get immediate access to that change without having to rol their CLI build. also fixes issue where we swallow initial errors when check setup middleware fails. --- cmd/influx/main.go | 1 + cmd/influx/pkg.go | 15 +++++++++++---- pkger/http_server.go | 2 +- pkger/parser.go | 4 ++-- pkger/parser_test.go | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/influx/main.go b/cmd/influx/main.go index 3754692adb..cf085d98a7 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -327,6 +327,7 @@ func checkSetupRunEMiddleware(f *globalFlags) cobraRunEMiddleware { } if setupErr := checkSetup(f.Host, f.skipVerify); setupErr != nil && influxdb.EUnauthorized != influxdb.ErrorCode(setupErr) { + cmd.OutOrStderr().Write([]byte(fmt.Sprintf("Error: %s\n", internal.ErrorFmt(err).Error()))) return internal.ErrorFmt(setupErr) } diff --git a/cmd/influx/pkg.go b/cmd/influx/pkg.go index 511a18ee26..e1f723e484 100644 --- a/cmd/influx/pkg.go +++ b/cmd/influx/pkg.go @@ -318,8 +318,9 @@ func (b *cmdPkgBuilder) cmdPkgSummary() *cobra.Command { return nil } - cmd := b.newCmd("summary", runE) + cmd := b.newCmd("summary", nil) cmd.Short = "Summarize the provided package" + cmd.RunE = runE b.registerPkgFileFlags(cmd) cmd.Flags().BoolVarP(&b.disableColor, "disable-color", "c", false, "Disable color in output") @@ -337,8 +338,9 @@ func (b *cmdPkgBuilder) cmdPkgValidate() *cobra.Command { return pkg.Validate() } - cmd := b.newCmd("validate", runE) + cmd := b.newCmd("validate", nil) cmd.Short = "Validate the provided package" + cmd.RunE = runE b.registerPkgFileFlags(cmd) @@ -428,8 +430,13 @@ func (b *cmdPkgBuilder) readPkg() (*pkger.Pkg, bool, error) { } pkgs = append(pkgs, urlPkgs...) + // the pkger.ValidSkipParseError option allows our server to be the one to validate the + // the pkg is accurate. If a user has an older version of the CLI and cloud gets updated + // with new validation rules,they'll get immediate access to that change without having to + // rol their CLI build. + if _, err := b.inStdIn(); err != nil { - pkg, err := pkger.Combine(pkgs...) + pkg, err := pkger.Combine(pkgs, pkger.ValidSkipParseError()) return pkg, false, err } @@ -437,7 +444,7 @@ func (b *cmdPkgBuilder) readPkg() (*pkger.Pkg, bool, error) { if err != nil { return nil, true, err } - pkg, err := pkger.Combine(append(pkgs, stdinPkg)...) + pkg, err := pkger.Combine(append(pkgs, stdinPkg), pkger.ValidSkipParseError()) return pkg, true, err } diff --git a/pkger/http_server.go b/pkger/http_server.go index 081946cd0b..4b8d5d3a52 100644 --- a/pkger/http_server.go +++ b/pkger/http_server.go @@ -213,7 +213,7 @@ func (r ReqApplyPkg) Pkgs(encoding Encoding) (*Pkg, error) { rawPkgs = append(rawPkgs, pkg) } - return Combine(rawPkgs...) + return Combine(rawPkgs) } // RespApplyPkg is the response body for the apply pkg endpoint. diff --git a/pkger/parser.go b/pkger/parser.go index 90b1799435..9c51927509 100644 --- a/pkger/parser.go +++ b/pkger/parser.go @@ -370,13 +370,13 @@ func (p *Pkg) applySecrets(secrets map[string]string) { // Combine combines pkgs together. Is useful when you want to take multiple disparate pkgs // and compile them into one to take advantage of the parser and service guarantees. -func Combine(pkgs ...*Pkg) (*Pkg, error) { +func Combine(pkgs []*Pkg, validationOpts ...ValidateOptFn) (*Pkg, error) { newPkg := new(Pkg) for _, p := range pkgs { newPkg.Objects = append(newPkg.Objects, p.Objects...) } - return newPkg, newPkg.Validate() + return newPkg, newPkg.Validate(validationOpts...) } type ( diff --git a/pkger/parser_test.go b/pkger/parser_test.go index bc818cf84f..80dc7fa8d5 100644 --- a/pkger/parser_test.go +++ b/pkger/parser_test.go @@ -4038,7 +4038,7 @@ spec: name: label_2 `, APIVersion))) - combinedPkg, err := Combine(pkgs...) + combinedPkg, err := Combine(pkgs) require.NoError(t, err) sum := combinedPkg.Summary()