github.com/vmware/govmomi@v0.51.0/cli/vm/guest/auth.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package guest 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "os" 12 "strings" 13 14 "github.com/vmware/govmomi/vim25/types" 15 ) 16 17 type AuthFlag struct { 18 auth types.NamePasswordAuthentication 19 proc bool 20 } 21 22 func newAuthFlag(ctx context.Context) (*AuthFlag, context.Context) { 23 return &AuthFlag{}, ctx 24 } 25 26 func (flag *AuthFlag) String() string { 27 return fmt.Sprintf("%s:%s", flag.auth.Username, strings.Repeat("x", len(flag.auth.Password))) 28 } 29 30 func (flag *AuthFlag) Set(s string) error { 31 c := strings.SplitN(s, ":", 2) 32 if len(c) > 0 { 33 flag.auth.Username = c[0] 34 if len(c) > 1 { 35 flag.auth.Password = c[1] 36 } 37 } 38 39 return nil 40 } 41 42 func (flag *AuthFlag) Register(ctx context.Context, f *flag.FlagSet) { 43 env := "GOVC_GUEST_LOGIN" 44 value := os.Getenv(env) 45 err := flag.Set(value) 46 if err != nil { 47 fmt.Printf("could not set guest login values: %v", err) 48 } 49 usage := fmt.Sprintf("Guest VM credentials (<user>:<password>) [%s]", env) 50 f.Var(flag, "l", usage) 51 if flag.proc { 52 f.BoolVar(&flag.auth.GuestAuthentication.InteractiveSession, "i", false, "Interactive session") 53 } 54 } 55 56 func (flag *AuthFlag) Process(ctx context.Context) error { 57 if flag.auth.Username == "" { 58 return fmt.Errorf("guest login username must not be empty") 59 } 60 61 return nil 62 } 63 64 func (flag *AuthFlag) Auth() types.BaseGuestAuthentication { 65 return &flag.auth 66 }