Merge pull request #231 from monopole/addinit

Add init command.
This commit is contained in:
k8s-ci-robot 2018-01-29 15:50:34 -08:00 committed by GitHub
commit 62ba5fb685
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 141 additions and 38 deletions

View File

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

View File

@ -1 +0,0 @@
../../../../../pkg/kinflate/examples/simple/instances

View File

@ -1 +0,0 @@
../../../../../pkg/kinflate/examples/simple/package

View File

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

View File

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

View File

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

View File

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