github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/fn/doc/cmdfndoc.go (about) 1 // Copyright 2019 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 doc 16 17 import ( 18 "bytes" 19 "context" 20 "errors" 21 "fmt" 22 "os" 23 "os/exec" 24 25 "github.com/GoogleContainerTools/kpt/internal/docs/generated/fndocs" 26 "github.com/GoogleContainerTools/kpt/internal/fnruntime" 27 "github.com/GoogleContainerTools/kpt/internal/printer" 28 "github.com/GoogleContainerTools/kpt/internal/util/cmdutil" 29 "github.com/spf13/cobra" 30 ) 31 32 func NewRunner(ctx context.Context, parent string) *Runner { 33 r := &Runner{ 34 Ctx: ctx, 35 } 36 c := &cobra.Command{ 37 Use: "doc --image=IMAGE", 38 Args: cobra.MaximumNArgs(0), 39 Short: fndocs.DocShort, 40 Long: fndocs.DocShort + "\n" + fndocs.DocLong, 41 Example: fndocs.DocExamples, 42 RunE: r.runE, 43 } 44 r.Command = c 45 c.Flags().StringVarP(&r.Image, "image", "i", "", "kpt function image name") 46 _ = r.Command.RegisterFlagCompletionFunc("image", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 47 return cmdutil.SuggestFunctions(cmd), cobra.ShellCompDirectiveDefault 48 }) 49 cmdutil.FixDocs("kpt", parent, c) 50 return r 51 } 52 53 func NewCommand(ctx context.Context, parent string) *cobra.Command { 54 return NewRunner(ctx, parent).Command 55 } 56 57 type Runner struct { 58 Image string 59 Command *cobra.Command 60 Ctx context.Context 61 } 62 63 func (r *Runner) runE(c *cobra.Command, _ []string) error { 64 if r.Image == "" { 65 return errors.New("image must be specified") 66 } 67 // TODO: We probably should be going through the runner 68 image, err := fnruntime.ResolveToImageForCLI(c.Context(), r.Image) 69 if err != nil { 70 return err 71 } 72 var out, errout bytes.Buffer 73 dockerRunArgs := []string{ 74 "run", 75 "--rm", // delete the container afterward 76 image, 77 "--help", 78 } 79 // If the env var is empty, stringToContainerRuntime defaults it to docker. 80 runtime, err := fnruntime.StringToContainerRuntime(os.Getenv(fnruntime.ContainerRuntimeEnv)) 81 if err != nil { 82 return err 83 } 84 85 err = fnruntime.ContainerRuntimeAvailable(runtime) 86 if err != nil { 87 return err 88 } 89 90 cmd := exec.Command(runtime.GetBin(), dockerRunArgs...) 91 cmd.Stdout = &out 92 cmd.Stderr = &errout 93 err = cmd.Run() 94 pr := printer.FromContextOrDie(r.Ctx) 95 if err != nil { 96 pr.Printf(errout.String()) 97 return fmt.Errorf("please ensure the container has an entrypoint and it supports --help flag: %w", err) 98 } 99 fmt.Fprintln(pr.OutStream(), out.String()) 100 return nil 101 }