github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/pkg/init/cmdinit.go (about) 1 // Copyright 2019 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 20 docs "github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs" 21 "github.com/GoogleContainerTools/kpt/internal/pkg" 22 "github.com/GoogleContainerTools/kpt/internal/util/cmdutil" 23 "github.com/GoogleContainerTools/kpt/internal/util/pathutil" 24 "github.com/GoogleContainerTools/kpt/pkg/kptpkg" 25 "github.com/spf13/cobra" 26 "sigs.k8s.io/kustomize/kyaml/filesys" 27 ) 28 29 // NewRunner returns a command runner. 30 func NewRunner(ctx context.Context, parent string) *Runner { 31 r := &Runner{ 32 Ctx: ctx, 33 } 34 c := &cobra.Command{ 35 Use: "init [DIR]", 36 Args: cobra.MaximumNArgs(1), 37 Short: docs.InitShort, 38 Long: docs.InitShort + "\n" + docs.InitLong, 39 Example: docs.InitExamples, 40 RunE: r.runE, 41 } 42 43 c.Flags().StringVar(&r.Description, "description", "sample description", "short description of the package.") 44 c.Flags().StringSliceVar(&r.Keywords, "keywords", []string{}, "list of keywords for the package.") 45 c.Flags().StringVar(&r.Site, "site", "", "link to page with information about the package.") 46 cmdutil.FixDocs("kpt", parent, c) 47 r.Command = c 48 return r 49 } 50 51 func NewCommand(ctx context.Context, parent string) *cobra.Command { 52 return NewRunner(ctx, parent).Command 53 } 54 55 // Runner contains the run function 56 type Runner struct { 57 Command *cobra.Command 58 Keywords []string 59 Name string 60 Description string 61 Site string 62 Ctx context.Context 63 } 64 65 func (r *Runner) runE(_ *cobra.Command, args []string) error { 66 if len(args) == 0 { 67 args = append(args, pkg.CurDir) 68 } 69 70 absPath, _, err := pathutil.ResolveAbsAndRelPaths(args[0]) 71 if err != nil { 72 return err 73 } 74 75 pkgIniter := kptpkg.DefaultInitializer{} 76 initOps := kptpkg.InitOptions{ 77 PkgPath: absPath, 78 RelPath: args[0], 79 Desc: r.Description, 80 Keywords: r.Keywords, 81 Site: r.Site, 82 } 83 84 return pkgIniter.Initialize(r.Ctx, filesys.FileSystemOrOnDisk{}, initOps) 85 }