-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
157 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 cmdrpkgedit | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/GoogleContainerTools/kpt/internal/errors" | ||
"github.com/GoogleContainerTools/kpt/internal/util/porch" | ||
porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" | ||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
command = "cmdrpkgedit" | ||
longMsg = ` | ||
kpt alpha rpkg edit SOURCE_PACKAGE TARGET | ||
Creates a new copy of a source package in the target repository. | ||
Args: | ||
SOURCE_PACKAGE: | ||
Source package. Must be a package resource name: <repository>-<hash> | ||
NAME: | ||
Target package revision name | ||
Example: package-name | ||
Flags: | ||
--repository | ||
Repository to which package will be copied. | ||
--revision | ||
Revision of the downstream package. | ||
` | ||
) | ||
|
||
func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command { | ||
return newRunner(ctx, rcg).Command | ||
} | ||
|
||
func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner { | ||
r := &runner{ | ||
ctx: ctx, | ||
cfg: rcg, | ||
} | ||
c := &cobra.Command{ | ||
Use: "edit SOURCE_PACKAGE NAME", | ||
Short: "Creates a copy of a source package in the target repository.", | ||
Long: longMsg, | ||
Example: "kpt alpha rpkg edit upstream-package-name target-package-name --repository target-repository --revision v1", | ||
PreRunE: r.preRunE, | ||
RunE: r.runE, | ||
Hidden: porch.HidePorchCommands, | ||
} | ||
r.Command = c | ||
|
||
c.Flags().StringVar(&r.repository, "repository", "", "Repository in which the copy will be created.") | ||
c.Flags().StringVar(&r.revision, "revision", "v1", "Revision of the copied package.") | ||
|
||
return r | ||
} | ||
|
||
type runner struct { | ||
ctx context.Context | ||
cfg *genericclioptions.ConfigFlags | ||
client client.Client | ||
Command *cobra.Command | ||
|
||
edit porchapi.PackageEditTaskSpec | ||
|
||
repository string // Target repository | ||
revision string // Target package revision | ||
target string // Target package name | ||
} | ||
|
||
func (r *runner) preRunE(cmd *cobra.Command, args []string) error { | ||
const op errors.Op = command + ".preRunE" | ||
client, err := porch.CreateClient(r.cfg) | ||
if err != nil { | ||
return errors.E(op, err) | ||
} | ||
r.client = client | ||
|
||
if len(args) < 2 { | ||
return errors.E(op, fmt.Errorf("SOURCE_PACKAGE and NAME are required positional arguments; %d provided", len(args))) | ||
} | ||
|
||
if r.repository == "" { | ||
return errors.E(op, fmt.Errorf("--repository is required to specify target repository")) | ||
} | ||
|
||
source := args[0] | ||
target := args[1] | ||
|
||
r.edit.Source = &porchapi.PackageRevisionRef{ | ||
Name: source, | ||
} | ||
|
||
r.target = target | ||
|
||
return nil | ||
} | ||
|
||
func (r *runner) runE(cmd *cobra.Command, args []string) error { | ||
const op errors.Op = command + ".runE" | ||
|
||
pr := &porchapi.PackageRevision{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "PackageRevision", | ||
APIVersion: porchapi.SchemeGroupVersion.Identifier(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: *r.cfg.Namespace, | ||
}, | ||
Spec: porchapi.PackageRevisionSpec{ | ||
PackageName: r.target, | ||
Revision: r.revision, | ||
RepositoryName: r.repository, | ||
Tasks: []porchapi.Task{ | ||
{ | ||
Type: porchapi.TaskTypeEdit, | ||
Edit: &r.edit, | ||
}, | ||
}, | ||
}, | ||
} | ||
if err := r.client.Create(r.ctx, pr); err != nil { | ||
return errors.E(op, err) | ||
} | ||
|
||
fmt.Fprintf(cmd.OutOrStdout(), "%s created", pr.Name) | ||
return nil | ||
} |