From eebd1921d84c857b4e19b528325e0518299d2c68 Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Mon, 29 Jan 2018 15:27:02 -0800 Subject: [PATCH] Add init command. --- cmd/kinflate/kinflate.go | 17 ++--- cmd/kinflate/testdata/simple/in/instances | 1 - cmd/kinflate/testdata/simple/in/package | 1 - .../{kinflate.go => commands/inflate.go} | 29 +++---- .../kinflate/commands/inflate_test.go | 21 +++--- pkg/kinflate/commands/init.go | 75 +++++++++++++++++++ pkg/kinflate/commands/init_test.go | 35 +++++++++ .../testdata/simple/out/expected.yaml | 0 8 files changed, 141 insertions(+), 38 deletions(-) delete mode 120000 cmd/kinflate/testdata/simple/in/instances delete mode 120000 cmd/kinflate/testdata/simple/in/package rename pkg/kinflate/{kinflate.go => commands/inflate.go} (72%) rename cmd/kinflate/kinflate_test.go => pkg/kinflate/commands/inflate_test.go (79%) create mode 100644 pkg/kinflate/commands/init.go create mode 100644 pkg/kinflate/commands/init_test.go rename {cmd/kinflate => pkg/kinflate/commands}/testdata/simple/out/expected.yaml (100%) diff --git a/cmd/kinflate/kinflate.go b/cmd/kinflate/kinflate.go index c0599f1a4..b7849c5c6 100644 --- a/cmd/kinflate/kinflate.go +++ b/cmd/kinflate/kinflate.go @@ -17,25 +17,18 @@ limitations under the License. package main import ( - "io" "os" "github.com/spf13/cobra" - "k8s.io/kubectl/pkg/kinflate" + "k8s.io/kubectl/pkg/kinflate/commands" ) -// TestableMain allows test coverage for main. -func TestableMain(out, errOut io.Writer, cmdMungeFn func(*cobra.Command)) error { - cmd := kinflate.NewCmdKinflate(out, errOut) - if cmdMungeFn != nil { - cmdMungeFn(cmd) - } - return cmd.Execute() -} - func main() { - err := TestableMain(os.Stdout, os.Stderr, nil) + var cmd = &cobra.Command{} + cmd.AddCommand(commands.NewCmdInflate(os.Stdout, os.Stderr)) + cmd.AddCommand(commands.NewCmdInit(os.Stdout, os.Stderr)) + err := cmd.Execute() if err != nil { os.Exit(1) } diff --git a/cmd/kinflate/testdata/simple/in/instances b/cmd/kinflate/testdata/simple/in/instances deleted file mode 120000 index 5af42042d..000000000 --- a/cmd/kinflate/testdata/simple/in/instances +++ /dev/null @@ -1 +0,0 @@ -../../../../../pkg/kinflate/examples/simple/instances \ No newline at end of file diff --git a/cmd/kinflate/testdata/simple/in/package b/cmd/kinflate/testdata/simple/in/package deleted file mode 120000 index 37de90e0a..000000000 --- a/cmd/kinflate/testdata/simple/in/package +++ /dev/null @@ -1 +0,0 @@ -../../../../../pkg/kinflate/examples/simple/package \ No newline at end of file diff --git a/pkg/kinflate/kinflate.go b/pkg/kinflate/commands/inflate.go similarity index 72% rename from pkg/kinflate/kinflate.go rename to pkg/kinflate/commands/inflate.go index 38ec630bf..0a8076a52 100644 --- a/pkg/kinflate/kinflate.go +++ b/pkg/kinflate/commands/inflate.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package kinflate +package commands import ( "fmt" @@ -23,25 +23,26 @@ import ( "github.com/spf13/cobra" + outil "k8s.io/kubectl/pkg/kinflate" kutil "k8s.io/kubectl/pkg/kinflate/util" ) -type kinflateOptions struct { +type inflateOptions struct { manifestPath string namespace string } -// NewCmdKinflate creates a new kinflate command. -func NewCmdKinflate(out, errOut io.Writer) *cobra.Command { - var o kinflateOptions +// NewCmdInflate creates a new inflate command. +func NewCmdInflate(out, errOut io.Writer) *cobra.Command { + var o inflateOptions cmd := &cobra.Command{ - Use: "kinflate -f [path]", + Use: "inflate -f [path]", Short: "Use a Manifest file to generate a set of api resources", Long: "Use a Manifest file to generate a set of api resources", Example: ` # Use the Kube-manifest.yaml file under somedir/ to generate a set of api resources. - kinflate -f somedir/`, + inflate -f somedir/`, Run: func(cmd *cobra.Command, args []string) { err := o.Validate(cmd, args) if err != nil { @@ -67,19 +68,19 @@ func NewCmdKinflate(out, errOut io.Writer) *cobra.Command { return cmd } -// Validate validates kinflate command. -func (o *kinflateOptions) Validate(cmd *cobra.Command, args []string) error { +// Validate validates inflate command. +func (o *inflateOptions) Validate(cmd *cobra.Command, args []string) error { return nil } -// Complete completes kinflate command. -func (o *kinflateOptions) Complete(cmd *cobra.Command, args []string) error { +// Complete completes inflate command. +func (o *inflateOptions) Complete(cmd *cobra.Command, args []string) error { return nil } -// RunKinflate runs kinflate command (do real work). -func (o *kinflateOptions) RunKinflate(cmd *cobra.Command, out, errOut io.Writer) error { - m, err := LoadFromManifestPath(o.manifestPath) +// RunKinflate runs inflate command (do real work). +func (o *inflateOptions) RunKinflate(cmd *cobra.Command, out, errOut io.Writer) error { + m, err := outil.LoadFromManifestPath(o.manifestPath) if err != nil { return err } diff --git a/cmd/kinflate/kinflate_test.go b/pkg/kinflate/commands/inflate_test.go similarity index 79% rename from cmd/kinflate/kinflate_test.go rename to pkg/kinflate/commands/inflate_test.go index 4a41a3d10..da5b446c1 100644 --- a/cmd/kinflate/kinflate_test.go +++ b/pkg/kinflate/commands/inflate_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package commands import ( "bytes" @@ -22,22 +22,23 @@ import ( "os" "reflect" "testing" - - "github.com/spf13/cobra" ) -func TestTrueMain(t *testing.T) { +const ( + input = "../examples/simple/instances/exampleinstance/" + expected = "testdata/simple/out/expected.yaml" +) + +func TestInflate(t *testing.T) { const updateEnvVar = "UPDATE_KINFLATE_EXPECTED_DATA" updateKinflateExpected := os.Getenv(updateEnvVar) == "true" - input := "testdata/simple/in/instances/exampleinstance/" - expected := "testdata/simple/out/expected.yaml" - cmdMungeFn := func(cmd *cobra.Command) { - cmd.Flags().Set("filename", input) - } buf := bytes.NewBuffer([]byte{}) - err := TestableMain(buf, os.Stderr, cmdMungeFn) + cmd := NewCmdInflate(buf, os.Stderr) + cmd.Flags().Set("filename", input) + + err := cmd.Execute() if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/pkg/kinflate/commands/init.go b/pkg/kinflate/commands/init.go new file mode 100644 index 000000000..4f3e5dfe2 --- /dev/null +++ b/pkg/kinflate/commands/init.go @@ -0,0 +1,75 @@ +/* +Copyright 2017 The Kubernetes 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 commands + +import ( + "fmt" + "io" + "os" + + "github.com/spf13/cobra" +) + +type initOptions struct { +} + +// NewCmdInit make the init command. +func NewCmdInit(out, errOut io.Writer) *cobra.Command { + var o initOptions + + cmd := &cobra.Command{ + Use: "init", + Short: "TDB", + Long: "TBD", + Example: ` + TBD`, + Run: func(cmd *cobra.Command, args []string) { + err := o.Validate(cmd, args) + if err != nil { + fmt.Fprintf(errOut, "error: %v\n", err) + os.Exit(1) + } + err = o.Complete(cmd, args) + if err != nil { + fmt.Fprintf(errOut, "error: %v\n", err) + os.Exit(1) + } + err = o.RunInit(cmd, out, errOut) + if err != nil { + fmt.Fprintf(errOut, "error: %v\n", err) + os.Exit(1) + } + }, + } + return cmd +} + +// Validate validates init command. +func (o *initOptions) Validate(cmd *cobra.Command, args []string) error { + return nil +} + +// Complete completes init command. +func (o *initOptions) Complete(cmd *cobra.Command, args []string) error { + return nil +} + +// RunKinflate runs init command (do real work). +func (o *initOptions) RunInit(cmd *cobra.Command, out, errOut io.Writer) error { + _, err := out.Write([]byte("Hello I am init.\n")) + return err +} diff --git a/pkg/kinflate/commands/init_test.go b/pkg/kinflate/commands/init_test.go new file mode 100644 index 000000000..767f72040 --- /dev/null +++ b/pkg/kinflate/commands/init_test.go @@ -0,0 +1,35 @@ +/* +Copyright 2017 The Kubernetes 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 commands + +import ( + "bytes" + "os" + "testing" +) + +func TestInit(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + cmd := NewCmdInit(buf, os.Stderr) + err := cmd.Execute() + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if buf.String() != "Hello I am init.\n" { + t.Errorf("unexpected output: %v", buf.String()) + } +} diff --git a/cmd/kinflate/testdata/simple/out/expected.yaml b/pkg/kinflate/commands/testdata/simple/out/expected.yaml similarity index 100% rename from cmd/kinflate/testdata/simple/out/expected.yaml rename to pkg/kinflate/commands/testdata/simple/out/expected.yaml