Skip to content

Commit

Permalink
Add support for dynamic commands published by CRDs
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittrock authored and Liujingfang1 committed Jul 31, 2019
1 parent 25ac98a commit 38e1577
Show file tree
Hide file tree
Showing 45 changed files with 3,248 additions and 144 deletions.
6 changes: 5 additions & 1 deletion .gitignore
@@ -1,10 +1,14 @@
# go build binaries
main
cli-experimental
dyctl

#intellij
.idea/
*.iml

# scripts/verify.sh creates a vendor for metalinter
vendor/
vendor/

# go artifacts
cover.out
12 changes: 5 additions & 7 deletions cmd/apply/apply.go
Expand Up @@ -16,16 +16,17 @@ package apply
import (
"fmt"

"sigs.k8s.io/cli-experimental/internal/pkg/util"

"sigs.k8s.io/cli-experimental/internal/pkg/clik8s"

"github.com/spf13/cobra"
"sigs.k8s.io/cli-experimental/cmd/apply/status"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli/wirek8s"
)

// GetApplyCommand returns the `apply` cobra Command
func GetApplyCommand() *cobra.Command {
func GetApplyCommand(a util.Args) *cobra.Command {
cmd := &cobra.Command{
Use: "apply",
Short: ".",
Expand All @@ -35,7 +36,7 @@ func GetApplyCommand() *cobra.Command {

cmd.RunE = func(cmd *cobra.Command, args []string) error {
for i := range args {
r, err := wirecli.DoApply(clik8s.ResourceConfigPath(args[i]), cmd.OutOrStdout())
r, err := wirecli.DoApply(clik8s.ResourceConfigPath(args[i]), cmd.OutOrStdout(), a)
if err != nil {
return err
}
Expand All @@ -44,10 +45,7 @@ func GetApplyCommand() *cobra.Command {
return nil
}

// Add Flags
wirek8s.Flags(cmd)

// Add Commands
cmd.AddCommand(status.GetApplyStatusCommand())
cmd.AddCommand(status.GetApplyStatusCommand(a))
return cmd
}
12 changes: 7 additions & 5 deletions cmd/apply/apply_test.go
Expand Up @@ -23,18 +23,19 @@ import (

"github.com/stretchr/testify/assert"
"sigs.k8s.io/cli-experimental/cmd/apply"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli/wirek8s"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli/wiretest"
)

var h string
var host string

func TestMain(m *testing.M) {
c, stop, err := wiretest.NewRestConfig()
if err != nil {
os.Exit(1)
}
defer stop()
h = c.Host
host = c.Host
os.Exit(m.Run())
}

Expand All @@ -51,12 +52,13 @@ configMapGenerator:
}

func TestApply(t *testing.T) {
f := setupKustomize(t)
buf := new(bytes.Buffer)

cmd := apply.GetApplyCommand()
args := []string{fmt.Sprintf("--server=%s", host), "--namespace=default", setupKustomize(t)}
cmd := apply.GetApplyCommand(args)
cmd.SetOutput(buf)
cmd.SetArgs([]string{fmt.Sprintf("--master=%s", h), f})
cmd.SetArgs(args)
wirek8s.Flags(cmd.PersistentFlags())

assert.NoError(t, cmd.Execute())
assert.Equal(t, "Doing `cli-experimental apply`\nResources: 1\n", buf.String())
Expand Down
9 changes: 4 additions & 5 deletions cmd/apply/status/status.go
Expand Up @@ -16,14 +16,15 @@ package status
import (
"fmt"

"sigs.k8s.io/cli-experimental/internal/pkg/util"

"github.com/spf13/cobra"
"sigs.k8s.io/cli-experimental/internal/pkg/clik8s"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli/wirek8s"
)

// GetApplyStatusCommand returns a new `apply status` command
func GetApplyStatusCommand() *cobra.Command {
func GetApplyStatusCommand(a util.Args) *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: ".",
Expand All @@ -33,7 +34,7 @@ func GetApplyStatusCommand() *cobra.Command {

cmd.RunE = func(cmd *cobra.Command, args []string) error {
for i := range args {
r, err := wirecli.DoStatus(clik8s.ResourceConfigPath(args[i]), cmd.OutOrStdout())
r, err := wirecli.DoStatus(clik8s.ResourceConfigPath(args[i]), cmd.OutOrStdout(), a)
if err != nil {
return err
}
Expand All @@ -42,7 +43,5 @@ func GetApplyStatusCommand() *cobra.Command {
return nil
}

// Add Flags
wirek8s.Flags(cmd)
return cmd
}
13 changes: 8 additions & 5 deletions cmd/apply/status/status_test.go
Expand Up @@ -21,20 +21,22 @@ import (
"path/filepath"
"testing"

"sigs.k8s.io/cli-experimental/internal/pkg/wirecli/wirek8s"

"github.com/stretchr/testify/assert"
"sigs.k8s.io/cli-experimental/cmd/apply/status"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli/wiretest"
)

var h string
var host string

func TestMain(m *testing.M) {
c, stop, err := wiretest.NewRestConfig()
if err != nil {
os.Exit(1)
}
defer stop()
h = c.Host
host = c.Host
os.Exit(m.Run())
}

Expand All @@ -51,12 +53,13 @@ configMapGenerator:
}

func TestStatus(t *testing.T) {
f := setupKustomize(t)
buf := new(bytes.Buffer)

cmd := status.GetApplyStatusCommand()
args := []string{fmt.Sprintf("--server=%s", host), "--namespace=default", setupKustomize(t)}
cmd := status.GetApplyStatusCommand(args)
cmd.SetOutput(buf)
cmd.SetArgs([]string{fmt.Sprintf("--master=%s", h), f})
cmd.SetArgs(args)
wirek8s.Flags(cmd.PersistentFlags())

assert.NoError(t, cmd.Execute())
assert.Equal(t, "Doing `cli-experimental apply status`\nResources: 1\n", buf.String())
Expand Down
55 changes: 55 additions & 0 deletions cmd/cmd.go
@@ -0,0 +1,55 @@
/*
Copyright 2019 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 cmd

import (
"os"

"github.com/spf13/cobra"
"sigs.k8s.io/cli-experimental/cmd/apply"
"sigs.k8s.io/cli-experimental/internal/pkg/dy"
"sigs.k8s.io/cli-experimental/internal/pkg/wirecli/wirek8s"
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(args []string, fn func(*cobra.Command)) error {
rootCmd := &cobra.Command{
Use: "cli-experimental",
Short: "",
Long: ``,
}
if fn != nil {
fn(rootCmd)
}
rootCmd.AddCommand(apply.GetApplyCommand(os.Args))
wirek8s.Flags(rootCmd.PersistentFlags())
rootCmd.PersistentFlags().Set("namespace", "default")

// Add dynamic Commands published by CRDs as go-templates
b, err := dy.InitializeCommandBuilder(rootCmd.OutOrStdout(), args)
if err != nil {
return err
}
if err := b.Build(rootCmd, nil); err != nil {
return err
}

// Run the Command
if err := rootCmd.Execute(); err != nil {
return err
}

return nil
}

0 comments on commit 38e1577

Please sign in to comment.