github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/alpha/rpkg/init/command.go (about) 1 // Copyright 2022 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package init 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/util" 22 "github.com/GoogleContainerTools/kpt/internal/docs/generated/rpkgdocs" 23 "github.com/GoogleContainerTools/kpt/internal/errors" 24 "github.com/GoogleContainerTools/kpt/internal/util/porch" 25 porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 26 "github.com/spf13/cobra" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/cli-runtime/pkg/genericclioptions" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 ) 31 32 const ( 33 command = "cmdrpkginit" 34 ) 35 36 func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command { 37 return newRunner(ctx, rcg).Command 38 } 39 40 func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner { 41 r := &runner{ 42 ctx: ctx, 43 cfg: rcg, 44 } 45 c := &cobra.Command{ 46 Use: "init PACKAGE_NAME", 47 Short: rpkgdocs.InitShort, 48 Long: rpkgdocs.InitShort + "\n" + rpkgdocs.InitLong, 49 Example: rpkgdocs.InitExamples, 50 PreRunE: r.preRunE, 51 RunE: r.runE, 52 Hidden: porch.HidePorchCommands, 53 } 54 r.Command = c 55 56 c.Flags().StringVar(&r.Description, "description", "sample description", "short description of the package.") 57 c.Flags().StringSliceVar(&r.Keywords, "keywords", []string{}, "list of keywords for the package.") 58 c.Flags().StringVar(&r.Site, "site", "", "link to page with information about the package.") 59 c.Flags().StringVar(&r.repository, "repository", "", "Repository to which package will be created.") 60 c.Flags().StringVar(&r.workspace, "workspace", "", "Workspace name of the package.") 61 62 return r 63 } 64 65 type runner struct { 66 ctx context.Context 67 cfg *genericclioptions.ConfigFlags 68 client client.Client 69 Command *cobra.Command 70 71 // Flags 72 Keywords []string 73 Description string 74 Site string 75 name string // Target package name 76 repository string // Target repository 77 workspace string // Target workspace name 78 } 79 80 func (r *runner) preRunE(_ *cobra.Command, args []string) error { 81 const op errors.Op = command + ".preRunE" 82 83 client, err := porch.CreateClientWithFlags(r.cfg) 84 if err != nil { 85 return errors.E(op, err) 86 } 87 r.client = client 88 89 if len(args) < 1 { 90 return errors.E(op, "PACKAGE_NAME is a required positional argument") 91 } 92 93 if r.repository == "" { 94 return errors.E(op, fmt.Errorf("--repository is required to specify target repository")) 95 } 96 97 if r.workspace == "" { 98 return errors.E(op, fmt.Errorf("--workspace is required to specify workspace name")) 99 } 100 101 r.name = args[0] 102 pkgExists, err := util.PackageAlreadyExists(r.ctx, r.client, r.repository, r.name, *r.cfg.Namespace) 103 if err != nil { 104 return err 105 } 106 if pkgExists { 107 return fmt.Errorf("`init` cannot create a new revision for package %q that already exists in repo %q; make subsequent revisions using `copy`", 108 r.name, r.repository) 109 } 110 return nil 111 } 112 113 func (r *runner) runE(cmd *cobra.Command, _ []string) error { 114 const op errors.Op = command + ".runE" 115 116 pr := &porchapi.PackageRevision{ 117 TypeMeta: metav1.TypeMeta{ 118 Kind: "PackageRevision", 119 APIVersion: porchapi.SchemeGroupVersion.Identifier(), 120 }, 121 ObjectMeta: metav1.ObjectMeta{ 122 Namespace: *r.cfg.Namespace, 123 }, 124 Spec: porchapi.PackageRevisionSpec{ 125 PackageName: r.name, 126 WorkspaceName: porchapi.WorkspaceName(r.workspace), 127 RepositoryName: r.repository, 128 Tasks: []porchapi.Task{ 129 { 130 Type: porchapi.TaskTypeInit, 131 Init: &porchapi.PackageInitTaskSpec{ 132 Description: r.Description, 133 Keywords: r.Keywords, 134 Site: r.Site, 135 }, 136 }, 137 }, 138 }, 139 Status: porchapi.PackageRevisionStatus{}, 140 } 141 if err := r.client.Create(r.ctx, pr); err != nil { 142 return errors.E(op, err) 143 } 144 145 fmt.Fprintf(cmd.OutOrStdout(), "%s created\n", pr.Name) 146 return nil 147 }