-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from vmware-tanzu/cli
Enhancements/additions to kctrl CLI
- Loading branch information
Showing
17 changed files
with
380 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package core | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type PackageCommandTreeOpts struct { | ||
BinaryName string | ||
PositionalArgs bool | ||
|
||
Color bool | ||
JSON bool | ||
} | ||
|
||
type Example struct { | ||
Description string | ||
Args []string | ||
} | ||
|
||
func (e Example) asString(nameFlag string, opts PackageCommandTreeOpts) string { | ||
command := opts.BinaryName | ||
for _, arg := range e.Args { | ||
if opts.PositionalArgs && arg == nameFlag { | ||
continue | ||
} | ||
command += " " + arg | ||
} | ||
return fmt.Sprintf("# %s \n%s", e.Description, command) | ||
} | ||
|
||
type Examples []Example | ||
|
||
func (es Examples) Description(nameFlag string, opts PackageCommandTreeOpts) string { | ||
var description string | ||
for _, example := range es { | ||
description += example.asString(nameFlag, opts) + "\n\n" | ||
} | ||
return strings.TrimSuffix(description, "\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2020 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package core | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
type InputFile struct { | ||
source FileSource | ||
} | ||
|
||
func NewInputFile(file string) InputFile { | ||
switch file { | ||
case "-": | ||
return InputFile{source: NewStdinSource()} | ||
default: | ||
return InputFile{source: NewLocalFileSource(file)} | ||
} | ||
} | ||
|
||
func (f InputFile) Bytes() ([]byte, error) { | ||
return f.source.Bytes() | ||
} | ||
|
||
type FileSource interface { | ||
Bytes() ([]byte, error) | ||
} | ||
|
||
type StdinSource struct{} | ||
|
||
var _ FileSource = StdinSource{} | ||
|
||
func NewStdinSource() StdinSource { return StdinSource{} } | ||
func (s StdinSource) Bytes() ([]byte, error) { return ioutil.ReadAll(os.Stdin) } | ||
|
||
type LocalFileSource struct { | ||
path string | ||
} | ||
|
||
var _ FileSource = LocalFileSource{} | ||
|
||
func NewLocalFileSource(path string) LocalFileSource { return LocalFileSource{path} } | ||
func (s LocalFileSource) Bytes() ([]byte, error) { return ioutil.ReadFile(s.path) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.