Skip to content

Commit

Permalink
Cli Usability: Cleanup fn commands and make dir optional (kptdev#1710)
Browse files Browse the repository at this point in the history
* Make Dir Optional For Fn Commands

* Clean -<STDIN> to -
  • Loading branch information
phanimarupaka authored and frankfarzan committed Jun 3, 2021
1 parent 5ce90ed commit 2a7f9ea
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 291 deletions.
4 changes: 2 additions & 2 deletions commands/fncmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package commands
import (
"github.com/GoogleContainerTools/kpt/internal/cmdexport"
"github.com/GoogleContainerTools/kpt/internal/cmdfndoc"
"github.com/GoogleContainerTools/kpt/internal/cmdrender"
"github.com/GoogleContainerTools/kpt/internal/docs/generated/fndocs"
"github.com/GoogleContainerTools/kpt/internal/pipeline"
"github.com/GoogleContainerTools/kpt/thirdparty/cmdconfig/commands/cmdeval"
"github.com/GoogleContainerTools/kpt/thirdparty/cmdconfig/commands/cmdsink"
"github.com/GoogleContainerTools/kpt/thirdparty/cmdconfig/commands/cmdsource"
Expand Down Expand Up @@ -49,7 +49,7 @@ func GetFnCommand(name string) *cobra.Command {
eval.Long = fndocs.RunShort + "\n" + fndocs.RunLong
eval.Example = fndocs.RunExamples

render := pipeline.NewCommand(name)
render := cmdrender.NewCommand(name)

doc := cmdfndoc.NewCommand(name)
doc.Short = fndocs.DocShort
Expand Down
10 changes: 7 additions & 3 deletions internal/cmdexport/cmdexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ExportCommand() *cobra.Command {
func GetExportRunner() *ExportRunner {
r := &ExportRunner{PipelineConfig: &types.PipelineConfig{}}
c := &cobra.Command{
Use: "export DIR/",
Use: "export [DIR]",
Short: fndocs.ExportShort,
Long: fndocs.ExportLong,
Example: fndocs.ExportExamples,
Expand Down Expand Up @@ -77,7 +77,11 @@ type ExportRunner struct {
}

func (r *ExportRunner) preRunE(cmd *cobra.Command, args []string) (err error) {
r.Dir = args[0]
// default to current working directory
r.Dir = "."
if len(args) > 0 {
r.Dir = args[0]
}

if len(r.WorkflowOrchestrator) == 0 {
return fmt.Errorf(
Expand Down Expand Up @@ -109,7 +113,7 @@ func (r *ExportRunner) preRunE(cmd *cobra.Command, args []string) (err error) {
}

// runE generates the pipeline and writes it into a file or stdout.
func (r *ExportRunner) runE(c *cobra.Command, args []string) error {
func (r *ExportRunner) runE(c *cobra.Command, _ []string) error {
pipeline, e := r.Pipeline.Init(r.PipelineConfig).Generate()

if e != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/cmdfndoc/cmdfndoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewRunner(parent string) *Runner {
r := &Runner{}
c := &cobra.Command{
Use: "doc --image=IMAGE",
Args: cobra.MaximumNArgs(1),
Args: cobra.MaximumNArgs(0),
Short: fndocs.DocShort,
Long: fndocs.DocShort + "\n" + fndocs.DocLong,
Example: fndocs.DocExamples,
Expand All @@ -52,7 +52,7 @@ type Runner struct {
Command *cobra.Command
}

func (r *Runner) runE(c *cobra.Command, args []string) error {
func (r *Runner) runE(c *cobra.Command, _ []string) error {
if r.Image == "" {
return errors.New("image must be specified")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pipeline/cmd.go → internal/cmdrender/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Package cmdget contains the get command
package pipeline
package cmdrender

import (
"fmt"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (r *Runner) preRunE(c *cobra.Command, args []string) error {
return nil
}

func (r *Runner) runE(c *cobra.Command, args []string) error {
func (r *Runner) runE(c *cobra.Command, _ []string) error {
err := cmdutil.DockerCmdAvailable()
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pipeline
package cmdrender

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

// Package pipeline provides struct definitions for Pipeline and utility
// methods to read and write a pipeline resource.
package pipeline
package cmdrender

import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/GoogleContainerTools/kpt/internal/pipeline/runtime"
"github.com/GoogleContainerTools/kpt/internal/cmdrender/runtime"
"github.com/GoogleContainerTools/kpt/internal/types"
kptfilev1alpha2 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1alpha2"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

// Package pipeline provides struct definitions for Pipeline and utility
// methods to read and write a pipeline resource.
package pipeline
package cmdrender

import (
"io/ioutil"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"testing"

"github.com/GoogleContainerTools/kpt/internal/pipeline/runtime"
"github.com/GoogleContainerTools/kpt/internal/cmdrender/runtime"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/docs/generated/pkgdocs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion site/reference/pkg/tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ kubectl get all -o yaml | kpt pkg tree \
### Synopsis
<!--mdtogo:Long-->
```
kpt pkg tree [DIR | -<STDIN>] [flags]
kpt pkg tree [DIR | -] [flags]
```

#### Args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func GetEvalFnRunner(name string) *EvalFnRunner {
r := &EvalFnRunner{}
c := &cobra.Command{
Use: "eval [DIR]",
Use: "eval [DIR | -]",
RunE: r.runE,
PreRunE: r.preRunE,
}
Expand Down Expand Up @@ -83,7 +83,7 @@ type EvalFnRunner struct {
IncludeMetaResources bool
}

func (r *EvalFnRunner) runE(c *cobra.Command, args []string) error {
func (r *EvalFnRunner) runE(c *cobra.Command, _ []string) error {
return runner.HandleError(c, r.RunFns.Execute())
}

Expand Down Expand Up @@ -224,12 +224,15 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error {
if r.Image == "" && r.ExecPath == "" {
return errors.Errorf("must specify --image or --exec-path")
}

var dataItems []string
if c.ArgsLenAtDash() >= 0 {
dataItems = args[c.ArgsLenAtDash():]
dataItems = append(dataItems, args[c.ArgsLenAtDash():]...)
args = args[:c.ArgsLenAtDash()]
}
if len(args) == 0 {
// default to current working directory
args = append(args, ".")
}
if len(args) > 1 {
return errors.Errorf("0 or 1 arguments supported, function arguments go after '--'")
}
Expand All @@ -245,9 +248,11 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error {
// set the output to stdout if in dry-run mode or no arguments are specified
var output io.Writer
var input io.Reader
if len(args) == 0 {
if args[0] == "-" {
output = c.OutOrStdout()
input = c.InOrStdin()
// clear args as it indicates stdin and not path
args = []string{}
} else if r.DryRun {
output = c.OutOrStdout()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ apiVersion: v1
},
{
name: "config map stdin / stdout",
args: []string{"eval", "--image", "foo:bar", "--", "a=b", "c=d", "e=f"},
args: []string{"eval", "-", "--image", "foo:bar", "--", "a=b", "c=d", "e=f"},
input: os.Stdin,
output: os.Stdout,
expected: `
Expand Down
15 changes: 3 additions & 12 deletions thirdparty/cmdconfig/commands/cmdsink/cmdsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import (
"github.com/GoogleContainerTools/kpt/thirdparty/cmdconfig/commands/runner"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
)

// GetSinkRunner returns a command for Sink.
func GetSinkRunner(name string) *SinkRunner {
r := &SinkRunner{}
c := &cobra.Command{
Use: "sink DIR",
Use: "sink [DIR]",
Short: fndocs.SinkShort,
Long: fndocs.SinkLong,
Example: fndocs.SinkExamples,
RunE: r.runE,
Args: cobra.MaximumNArgs(1),
Args: cobra.MinimumNArgs(1),
}
r.Command = c
return r
Expand All @@ -36,15 +35,7 @@ type SinkRunner struct {
}

func (r *SinkRunner) runE(c *cobra.Command, args []string) error {
var outputs []kio.Writer
if len(args) == 1 {
outputs = []kio.Writer{&kio.LocalPackageWriter{PackagePath: args[0]}}
} else {
outputs = []kio.Writer{&kio.ByteWriter{
Writer: c.OutOrStdout(),
ClearAnnotations: []string{kioutil.PathAnnotation}},
}
}
outputs := []kio.Writer{&kio.LocalPackageWriter{PackagePath: args[0]}}

err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}},
Expand Down
Loading

0 comments on commit 2a7f9ea

Please sign in to comment.