github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/live/installrg/cmdinstallrg.go (about) 1 // Copyright 2022 Google LLC 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 installrg 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/GoogleContainerTools/kpt/internal/docs/generated/livedocs" 22 "github.com/GoogleContainerTools/kpt/pkg/live" 23 "github.com/spf13/cobra" 24 "k8s.io/cli-runtime/pkg/genericclioptions" 25 cmdutil "k8s.io/kubectl/pkg/cmd/util" 26 ) 27 28 // NewRunner returns a command runner 29 func NewRunner( 30 ctx context.Context, 31 factory cmdutil.Factory, 32 ioStreams genericclioptions.IOStreams, 33 ) *Runner { 34 r := &Runner{ 35 ctx: ctx, 36 ioStreams: ioStreams, 37 factory: factory, 38 } 39 c := &cobra.Command{ 40 Use: "install-resource-group", 41 RunE: r.runE, 42 PreRunE: r.preRunE, 43 Short: livedocs.InstallResourceGroupShort, 44 Long: livedocs.InstallResourceGroupShort + "\n" + livedocs.InstallResourceGroupLong, 45 Example: livedocs.InstallResourceGroupExamples, 46 } 47 r.Command = c 48 return r 49 } 50 51 func NewCommand( 52 ctx context.Context, 53 factory cmdutil.Factory, 54 ioStreams genericclioptions.IOStreams, 55 ) *cobra.Command { 56 return NewRunner(ctx, factory, ioStreams).Command 57 } 58 59 // Runner contains the run function 60 type Runner struct { 61 ctx context.Context 62 Command *cobra.Command 63 ioStreams genericclioptions.IOStreams 64 factory cmdutil.Factory 65 } 66 67 func (r *Runner) preRunE(_ *cobra.Command, _ []string) error { 68 return nil 69 } 70 71 func (r *Runner) runE(_ *cobra.Command, args []string) error { 72 // Validate the number of arguments. 73 if len(args) > 0 { 74 return fmt.Errorf("too many arguments; install-resource-group takes no arguments") 75 } 76 fmt.Fprint(r.ioStreams.Out, "installing inventory ResourceGroup CRD...") 77 78 err := (&live.ResourceGroupInstaller{ 79 Factory: r.factory, 80 }).InstallRG(r.ctx) 81 82 if err == nil { 83 fmt.Fprintln(r.ioStreams.Out, "success") 84 } else { 85 fmt.Fprintln(r.ioStreams.Out, "failed") 86 } 87 return err 88 }