github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/config/get/get.go (about) 1 package get 2 3 import ( 4 "fmt" 5 6 "github.com/MakeNowJust/heredoc" 7 "github.com/cli/cli/internal/config" 8 "github.com/cli/cli/pkg/cmdutil" 9 "github.com/cli/cli/pkg/iostreams" 10 "github.com/spf13/cobra" 11 ) 12 13 type GetOptions struct { 14 IO *iostreams.IOStreams 15 Config config.Config 16 17 Hostname string 18 Key string 19 } 20 21 func NewCmdConfigGet(f *cmdutil.Factory, runF func(*GetOptions) error) *cobra.Command { 22 opts := &GetOptions{ 23 IO: f.IOStreams, 24 } 25 26 cmd := &cobra.Command{ 27 Use: "get <key>", 28 Short: "Print the value of a given configuration key", 29 Example: heredoc.Doc(` 30 $ gh config get git_protocol 31 https 32 `), 33 Args: cobra.ExactArgs(1), 34 RunE: func(cmd *cobra.Command, args []string) error { 35 config, err := f.Config() 36 if err != nil { 37 return err 38 } 39 opts.Config = config 40 opts.Key = args[0] 41 42 if runF != nil { 43 return runF(opts) 44 } 45 46 return getRun(opts) 47 }, 48 } 49 50 cmd.Flags().StringVarP(&opts.Hostname, "host", "h", "", "Get per-host setting") 51 52 return cmd 53 } 54 55 func getRun(opts *GetOptions) error { 56 val, err := opts.Config.Get(opts.Hostname, opts.Key) 57 if err != nil { 58 return err 59 } 60 61 if val != "" { 62 fmt.Fprintf(opts.IO.Out, "%s\n", val) 63 } 64 return nil 65 }