Skip to content

Commit a6c8d06

Browse files
authored
Merge pull request crossplane#6119 from twobiers/crank-completion-3
feat: Autocompletion for Crossplane CLI
2 parents 23f3c2a + 8332924 commit a6c8d06

File tree

17 files changed

+379
-82
lines changed

17 files changed

+379
-82
lines changed

cmd/crank/beta/convert/compositionenvironment/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import (
3636
// Cmd arguments and flags for converting a Composition to use function-environment-configs.
3737
type Cmd struct {
3838
// Arguments.
39-
InputFile string `arg:"" default:"-" help:"The Composition file to be converted. If not specified or '-', stdin will be used." optional:"" type:"path"`
39+
InputFile string `arg:"" default:"-" help:"The Composition file to be converted. If not specified or '-', stdin will be used." optional:"" predictor:"file" type:"path"`
4040

4141
// Flags.
42-
OutputFile string `help:"The file to write the generated Composition to. If not specified, stdout will be used." placeholder:"PATH" short:"o" type:"path"`
42+
OutputFile string `help:"The file to write the generated Composition to. If not specified, stdout will be used." placeholder:"PATH" predictor:"file" short:"o" type:"path"`
4343

4444
FunctionEnvironmentConfigRef string `default:"function-environment-configs" help:"Name of the existing function-environment-configs Function, to be used to reference it." name:"function-environment-configs-ref"`
4545

cmd/crank/beta/convert/deploymentruntime/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ import (
3434
// Cmd arguments and flags for convert deployment-runtime subcommand.
3535
type Cmd struct {
3636
// Arguments.
37-
InputFile string `arg:"" default:"-" help:"The ControllerConfig file to be Converted. If not specified or '-', stdin will be used." optional:"" type:"path"`
37+
InputFile string `arg:"" default:"-" help:"The ControllerConfig file to be Converted. If not specified or '-', stdin will be used." optional:"" predictor:"file" type:"path"`
3838

3939
// Flags.
40-
OutputFile string `help:"The file to write the generated DeploymentRuntimeConfig to. If not specified, stdout will be used." placeholder:"PATH" short:"o" type:"path"`
40+
OutputFile string `help:"The file to write the generated DeploymentRuntimeConfig to. If not specified, stdout will be used." placeholder:"PATH" predictor:"file" short:"o" type:"path"`
4141

4242
fs afero.Fs
4343
}

cmd/crank/beta/convert/pipelinecomposition/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import (
3636
// Cmd arguments and flags for converting a patch-and-transform to a function pipeline composition.
3737
type Cmd struct {
3838
// Arguments.
39-
InputFile string `arg:"" default:"-" help:"The Composition file to be converted. If not specified or '-', stdin will be used." optional:"" type:"path"`
39+
InputFile string `arg:"" default:"-" help:"The Composition file to be converted. If not specified or '-', stdin will be used." optional:"" predictor:"file" type:"path"`
4040

4141
// Flags.
42-
OutputFile string `help:"The file to write the generated Composition to. If not specified, stdout will be used." placeholder:"PATH" short:"o" type:"path"`
42+
OutputFile string `help:"The file to write the generated Composition to. If not specified, stdout will be used." placeholder:"PATH" predictor:"file" short:"o" type:"path"`
4343

4444
FunctionPatchAndTransformRef string `default:"function-patch-and-transform" help:"Name of the existing function-patch-and-transform Function, to be used to reference it." name:"function-patch-and-transform-ref"`
4545
FunctionEnvironmentConfigRef string `default:"function-environment-configs" help:"Name of the existing function-environment-configs Function, to be used to reference it." name:"function-environment-configs-ref"`

cmd/crank/beta/top/top.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const (
5252
// Cmd represents the top command.
5353
type Cmd struct {
5454
Summary bool `help:"Adds summary header for all Crossplane pods." name:"summary" short:"s"`
55-
Namespace string `default:"crossplane-system" help:"Show pods from a specific namespace, defaults to crossplane-system." name:"namespace" short:"n"`
55+
Namespace string `default:"crossplane-system" help:"Show pods from a specific namespace, defaults to crossplane-system." name:"namespace" predictor:"namespace" short:"n"`
5656
}
5757

5858
// Help returns help instructions for the top command.

cmd/crank/beta/trace/internal/resource/client.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,19 @@ package resource
1818

1919
import (
2020
"context"
21-
"fmt"
2221

2322
v1 "k8s.io/api/core/v1"
24-
"k8s.io/apimachinery/pkg/api/meta"
2523
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26-
"k8s.io/apimachinery/pkg/runtime/schema"
2724
"sigs.k8s.io/controller-runtime/pkg/client"
2825

2926
xpmeta "github.com/crossplane/crossplane-runtime/pkg/meta"
3027
)
3128

32-
const (
33-
errFmtResourceTypeNotFound = "the server doesn't have a resource type %q"
34-
)
35-
3629
// TreeClient is the interface to get a Resource with all its children.
3730
type TreeClient interface {
3831
GetResourceTree(ctx context.Context, root *Resource) (*Resource, error)
3932
}
4033

41-
// MappingFor returns the RESTMapping for the given resource or kind argument.
42-
// Copied over from cli-runtime pkg/resource Builder,
43-
// https://github.com/kubernetes/cli-runtime/blob/9a91d944dd43186c52e0162e12b151b0e460354a/pkg/resource/builder.go#L768
44-
func MappingFor(rmapper meta.RESTMapper, resourceOrKindArg string) (*meta.RESTMapping, error) {
45-
// TODO(phisco): actually use the Builder.
46-
fullySpecifiedGVR, groupResource := schema.ParseResourceArg(resourceOrKindArg)
47-
gvk := schema.GroupVersionKind{}
48-
if fullySpecifiedGVR != nil {
49-
gvk, _ = rmapper.KindFor(*fullySpecifiedGVR)
50-
}
51-
if gvk.Empty() {
52-
gvk, _ = rmapper.KindFor(groupResource.WithVersion(""))
53-
}
54-
if !gvk.Empty() {
55-
return rmapper.RESTMapping(gvk.GroupKind(), gvk.Version)
56-
}
57-
fullySpecifiedGVK, groupKind := schema.ParseKindArg(resourceOrKindArg)
58-
if fullySpecifiedGVK == nil {
59-
gvk := groupKind.WithVersion("")
60-
fullySpecifiedGVK = &gvk
61-
}
62-
if !fullySpecifiedGVK.Empty() {
63-
if mapping, err := rmapper.RESTMapping(fullySpecifiedGVK.GroupKind(), fullySpecifiedGVK.Version); err == nil {
64-
return mapping, nil
65-
}
66-
}
67-
mapping, err := rmapper.RESTMapping(groupKind, gvk.Version)
68-
if err != nil {
69-
// if we error out here, it is because we could not match a resource or a kind
70-
// for the given argument. To maintain consistency with previous behavior,
71-
// announce that a resource type could not be found.
72-
// if the error is _not_ a *meta.NoKindMatchError, then we had trouble doing discovery,
73-
// so we should return the original error since it may help a user diagnose what is actually wrong
74-
if meta.IsNoMatchError(err) {
75-
return nil, fmt.Errorf(errFmtResourceTypeNotFound, groupResource.Resource)
76-
}
77-
return nil, err
78-
}
79-
return mapping, nil
80-
}
81-
8234
// GetResource returns the requested Resource, setting any error as Resource.Error.
8335
func GetResource(ctx context.Context, client client.Client, ref *v1.ObjectReference) *Resource {
8436
result := unstructured.Unstructured{}

cmd/crank/beta/trace/trace.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/crossplane/crossplane/cmd/crank/beta/trace/internal/resource"
4040
"github.com/crossplane/crossplane/cmd/crank/beta/trace/internal/resource/xpkg"
4141
"github.com/crossplane/crossplane/cmd/crank/beta/trace/internal/resource/xrm"
42+
"github.com/crossplane/crossplane/cmd/crank/internal"
4243
)
4344

4445
const (
@@ -58,12 +59,12 @@ const (
5859

5960
// Cmd builds the trace tree for a Crossplane resource.
6061
type Cmd struct {
61-
Resource string `arg:"" help:"Kind of the Crossplane resource, accepts the 'TYPE[.VERSION][.GROUP][/NAME]' format."`
62-
Name string `arg:"" help:"Name of the Crossplane resource, can be passed as part of the resource too." optional:""`
62+
Resource string `arg:"" help:"Kind of the Crossplane resource, accepts the 'TYPE[.VERSION][.GROUP][/NAME]' format." predictor:"k8s_resource"`
63+
Name string `arg:"" help:"Name of the Crossplane resource, can be passed as part of the resource too." optional:"" predictor:"k8s_resource_name"`
6364

6465
// TODO(phisco): add support for all the usual kubectl flags; configFlags := genericclioptions.NewConfigFlags(true).AddFlags(...)
65-
Context string `default:"" help:"Kubernetes context." name:"context" short:"c"`
66-
Namespace string `default:"" help:"Namespace of the resource." name:"namespace" short:"n"`
66+
Context string `default:"" help:"Kubernetes context." name:"context" predictor:"context" short:"c"`
67+
Namespace string `default:"" help:"Namespace of the resource." name:"namespace" predictor:"namespace" short:"n"`
6768
Output string `default:"default" enum:"default,wide,json,dot" help:"Output format. One of: default, wide, json, dot." name:"output" short:"o"`
6869
ShowConnectionSecrets bool `help:"Show connection secrets in the output." name:"show-connection-secrets" short:"s"`
6970
ShowPackageDependencies string `default:"unique" enum:"unique,all,none" help:"Show package dependencies in the output. One of: unique, all, none." name:"show-package-dependencies"`
@@ -165,7 +166,7 @@ func (c *Cmd) Run(k *kong.Context, logger logging.Logger) error {
165166
return errors.Wrap(err, errInvalidResourceAndName)
166167
}
167168

168-
mapping, err := resource.MappingFor(rmapper, res)
169+
mapping, err := internal.MappingFor(rmapper, res)
169170
if err != nil {
170171
return errors.Wrap(err, errGetMapping)
171172
}

cmd/crank/beta/validate/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Cmd struct {
4040
Resources string `arg:"" help:"Resources source which can be a file, directory, or '-' for standard input."`
4141

4242
// Flags. Keep them in alphabetical order.
43-
CacheDir string `default:"~/.crossplane/cache" help:"Absolute path to the cache directory where downloaded schemas are stored."`
43+
CacheDir string `default:"~/.crossplane/cache" help:"Absolute path to the cache directory where downloaded schemas are stored." predictor:"directory"`
4444
CleanCache bool `help:"Clean the cache directory before downloading package schemas."`
4545
SkipSuccessResults bool `help:"Skip printing success results."`
4646
CrossplaneImage string `help:"Specify the Crossplane image to be used for validating the built-in schemas."`

0 commit comments

Comments
 (0)