title: Config file with cobra and viper
body: <p><strong>Basic information:</strong> I have created a go application and used Cobra for it. Cobra uses Viper for command line parameters and flags.</p>
  
  <p>I have a command listen with a flag bind and I want to configure it in a yaml file.</p>
  
  <p><strong>Code:</strong></p>
  
  <p>The init function of the listen command looks like this:</p>
  
  <pre><code>func init() {
      RootCmd.AddCommand(listenCmd)
      listenCmd.Flags().StringP("bind", "b", ":50051", "Provide bind definition")
      viper.BindPFlag("bind", listenCmd.Flags().Lookup("bind"))
  }
  </code></pre>
  
  <p>Code of my application is at <a href="https://github.com/sascha-andres/go-logsink" rel="noreferrer">https://github.com/sascha-andres/go-logsink</a></p>
  
  <p><strong>Problem:</strong> </p>
  
  <p>When I call the app with <code>listen --bind "bla"</code> the flag is set correctly to <code>bla</code>, but I have not found a way to achieve this using a YAML file located in my home directory.</p>
  
  <p><strong>Config files tried:</strong></p>
  
  <pre><code>---
  
  connect:
    bind: "bla"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>---
  
  bind: "bla"
  </code></pre>
  
  <p>In both cases the config file was found but the flag had not the expected value but the default value.</p>
  
  <p>How do I have to write the config file to have the flag populated correctly?</p>