Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WriteConfig should not fail on unknown file extensions when the config type is set #427

Open
mildwonkey opened this issue Dec 20, 2017 · 3 comments

Comments

@mildwonkey
Copy link

This fails with an Unsupported Config Type error:

package main

import (
  "fmt"
  "github.com/spf13/viper"
)

func main(){
  example := viper.New()
  example.SetConfigFile("vars.tfvars")
  example.SetConfigType("hcl")
  example.Set("my_new_variable", "hi_mom")
  if err := example.WriteConfig(); err != nil {
    fmt.Println(err.Error())
  }
}

This works:

package main

import (
  "fmt"
  "github.com/spf13/viper"
)

func main(){
  example = viper.New()
  example.SetConfigFile("vars.hcl")
  example.SetConfigType("hcl")

  example.Set("my_new_variable", "hi_mom")
  if err := example.WriteConfig(); err != nil {
    fmt.Println(err.Error())
  }  
}

WriteConfig should first check if the ConfigType has already been set before looking at file extensions.

It's here-ish, as far as I can tell:

viper/viper.go

Line 1238 in 1a0c4a3

ext := filepath.Ext(filename)

@vermaaakansha
Copy link

@mildwonkey Has this issue got fixed?

@Fuhrmann
Copy link

@mildwonkey have you found a workaround for this problem?

@mildwonkey
Copy link
Author

I worked around it by forking the project, writing the linked PR, and using the fork in my code. I do not recommend using my fork, it's many years out of date, but you could apply that change to your own fork of this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants