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

Feature/write config #287

Merged
merged 23 commits into from
Dec 7, 2017
Merged

Feature/write config #287

merged 23 commits into from
Dec 7, 2017

Conversation

theherk
Copy link
Contributor

@theherk theherk commented Dec 9, 2016

This PR is based on earlier code submitted by @g3rk6 in #153. I believe I have satisfied all requests in @spf13's comments.

It adds methods to write the configuration back to the file or to a new file. There are methods to do so only if the file does not exist, as well

This PR has been alive, kept synchronized, and unmerged for a very long time. As a result, for those of you that need this feature, I am maintaining a synchronized fork here. This fork's master branch is based of the current tip here at sp313/viper, but with the addition of merged branches represented in #287 and #342. Please feel free to import github.com/theherk/viper if you need to use those features.

@CLAassistant
Copy link

CLAassistant commented Dec 9, 2016

CLA assistant check
All committers have signed the CLA.

@theherk theherk mentioned this pull request Dec 9, 2016
@theherk theherk changed the title [WIP] Feature/write config Feature/write config Dec 15, 2016
@theherk
Copy link
Contributor Author

theherk commented Dec 15, 2016

This PR is no longer a work in progress. I believe it is ready to go. Based on the number of changes, it probably looks quite a bit bigger than it is. More specifically, that is due to moving the unmarshalReader to viper.go. That enables the storing of properties on the viper object to that comments could be written back to the file.

In summary what this does is add methods to safely or unsafely write the current configuration back to the currently configured config file or to a named config file. For example,

err := v.WriteConfig()
// or
err := v.WriteConfigAs("config.yaml")
// or
err := v.SafeWriteConfigAs("only_if_i_dont_exist.hcl")

In addition, the spelling of marshal is uniform across the library now.

This should resolve each of the issue requested by the author here.

@theherk
Copy link
Contributor Author

theherk commented Dec 15, 2016

I did have a test error that required an update, but everything else seems fine. I cannot determine why some of the builds are failing. It is complaining about some syntax in hcl/printer/nodes.go.

@theherk
Copy link
Contributor Author

theherk commented Dec 15, 2016

It is complaining about this line in hcl/printer/nodes.go. I don't understand that error. It seems defined to me.

Can you look into why this build is failing.

@theherk
Copy link
Contributor Author

theherk commented Dec 15, 2016

Okay, I sorted it out. bytes.ContainsRune was only added in Go 1.7. See the commit and the release notes. I'm going to work on build constraints.

@theherk
Copy link
Contributor Author

theherk commented Dec 15, 2016

Alright. Now I have it set up only to allow the writing of HCL files when using Go 1.7. All builds are passing. We should be good to go.

@moorereason
Copy link
Contributor

I'd recommend that you submit a PR to hashicorp/hcl. ContainsRune is trivial.

-    if bytes.ContainsRune(result, '\n') {
+    if bytes.IndexRune(result, '\n') >= 0 {

@theherk
Copy link
Contributor Author

theherk commented Dec 15, 2016

Are you suggesting I do that in lieu of having some code supported differently based on the version in which it was compiled?

@theherk
Copy link
Contributor Author

theherk commented Dec 15, 2016

Okay, I have submitted the PR suggested: hashicorp/hcl#176. If that is accepted, I'll revert to the much better looking code.

@theherk
Copy link
Contributor Author

theherk commented Dec 16, 2016

Okay, that was a great solution. It was merged in by Hashicorp, so now we are back to the much prettier code, and builds are passing. 🏁

@theherk
Copy link
Contributor Author

theherk commented Dec 19, 2016

Also fixes #33 and fixes #113.

@andygrunwald
Copy link

andygrunwald commented Jan 3, 2017

Any update here @spf13 ?
I am looking forward for this feature, because i got a use case for this :)
Can i help here?

@bep
Copy link
Collaborator

bep commented Jan 5, 2017

@spf13 may have his opinions, but here are my 50 cents.

Most of it looks fine; but I think it is too much boilerplate code about file writing (which we could write a util func for, but it could also easily be written by the user).

The main funcs I want is:

func WriteConfig(f ConfigFormat, w io.Writer) error {
//...
}

It may be sensible in that func to check if that io.Writer is also an io.Closer and close it when done.

If also means that we must create proper and exported string consts for the different ConfigTypes, set that value on init (and export the value), and later we don't have to do the magic guessing by file name.

alessandrozucca added a commit to continuouspipe/remote-environment-client that referenced this pull request Jan 6, 2017
…ill soon enough(spf13/viper/pull/287) In the meanwhile i need to write yaml that viper can read
@theherk
Copy link
Contributor Author

theherk commented Jan 9, 2017

Forgive me @bep, I'm pretty new to Go, so I may not have done this in the most reasonable way, but I tried to follow the guidelines given by @spf13 here. Those include the request for a "safe" write method. It seemed like a good idea to me to allow the user to overwrite the current configuration or write a new one, which is why there are multiple methods.

I am probably misunderstanding your last statements, but I just used the SupportedExts as they were previously used. I didn't think it was too much magic to detect the config type based on the extension, but if you would prefer it another way, I made need further explanation. I thought this was a pretty good improvement over the previous incomplete PR, but I certainly don't want to disrupt the quality of the library.

@bep
Copy link
Collaborator

bep commented Jan 9, 2017

This is @spf13 library -- he should consider this PR. I just chimed in with my "50 cents", and I think that adding a lot of file handling into this feature is a low priority. An io.writer makes it flexible.

@theherk
Copy link
Contributor Author

theherk commented Feb 27, 2017

I'm going to maintain a fork of this with the write methods included. I hope some time @spf13 has time to check out the pull requests on viper again.

@dengwu12
Copy link

Needing this feature +1000000000, online waiting

@theherk
Copy link
Contributor Author

theherk commented Mar 24, 2017

@dengwu12: It looks like @spf13 may have abandoned maintenance entirely since his new gig. The other maintainer, @bep, has merged only two other PRs since this opened. I'm surprised given the popularity of Viper and Cobra how little love they are getting, but c'est la vie.

It seems that some conflicts have been generated that will require action to get this back to merge ready. In the meantime, I am maintaining the fork's master branch, because we required use of this feature. You can use it by importing from github.com/theherk/viper.

@spf13
Copy link
Owner

spf13 commented Apr 21, 2017

@theherk I've had health issues that have required me to seriously limit the time I could spend on things. I'm recovering and picking back up old issues.

I think the feature you've implemented is a must have for Viper. Can you bring the PR up to the latest and I'll do a full review of the code and we will get this merged in?

Thanks for the patience and for implementing this.

@andygrunwald
Copy link

@spf13 I hope you are doing well.

@spf13 spf13 mentioned this pull request Apr 21, 2017
@theherk
Copy link
Contributor Author

theherk commented Apr 22, 2017

@spf13, thank you for following up. I'm sorry for any troubles you had, and I understand completely. I will get this ready for muster by the end of this weekend.

@theherk
Copy link
Contributor Author

theherk commented Apr 23, 2017

All modifications have been rebased onto the tip of master and conflicts are resolved. Tests are passing and everything seems to be working in my usage tests. Please let me know if you'd like me to do squashing. Otherwise, this is good to go.

@DaBlitzStein
Copy link

¿It is working now?

@theherk
Copy link
Contributor Author

theherk commented Jun 6, 2017

It is working, but has not been merged. @spf13 has been out for a while. I am maintaining a fork with this feature working. See this comment above.

@DaBlitzStein
Copy link

Supports remote @theherk ?

@theherk
Copy link
Contributor Author

theherk commented Jun 6, 2017

@DaBlitzStein: All of the commits that exist on master in spf13/viper exist on master in theherk/viper, with the addition of those from this PR. If you mean does it support writing back to a remote configuration, the answer is no. Interesting idea though, but that is a whole other can of worms. I'd rather get this PR in, before that feature is attempted.

@DaBlitzStein
Copy link

Thank you @theherk .

@Fjolnir-Dvorak
Copy link

Thank you @theherk. I am using your branch now. Hope it will get merged, soon, from one of the contributors.

@botherder
Copy link

I just wanted to +1 this feature. Thank you @theherk.

@dimitri
Copy link

dimitri commented Aug 3, 2017

I also need the feature, so I hope @spf13 is feeling better and able to get back to working on Viper. Thanks @theherk for maintaining your fork in the meanwhile!

@robdefeo
Copy link

What is the status of the PR it seems like such and incredibly useful feature that will save a lot of work for the community is there anything we can do to help @spf13

@segmentationtree
Copy link

what's the latest status on this? +1 on this feature.

@damianoneill
Copy link

Hi, is there any update on this PR?

Thanks.

@zaquestion
Copy link

@theherk can you fix the conflict on this? @spf13 is this otherwise good to merge?

@theherk
Copy link
Contributor Author

theherk commented Dec 1, 2017

Most definitely. I'll make sure it is squared away by the end of the weekend. But I haven't seen @spf13 comment on this for 7 months.

@zaquestion
Copy link

Might want to update the description as well, to clarify this is no longer a work in progress. Thanks for jumping on! Surely with some persistence we can get @spf13 to bring this in :)

@theherk
Copy link
Contributor Author

theherk commented Dec 2, 2017

Thank you @zaquestion. Updating the description was a great plan. I have sync'd the PR. Additionally, in answer to those of you needing to use this feature, I am maintaining a fork with those additions. See the PR description for details.

@zaquestion
Copy link

@bketelsen and @bep you 2 have been active most recently and appear to have merge rights. Can you review and merge these changes if appropriate? If not, can you get @spf13's attention?

@bketelsen bketelsen merged commit 1a0c4a3 into spf13:master Dec 7, 2017
RavenZZ added a commit to RavenZZ/viper that referenced this pull request Feb 9, 2018
* 'master' of github.com:RavenZZ/viper:
  travis: update go versions
  Feature/write config (spf13#287)

* 'master' of github.com:RavenZZ/viper:
  travis: update go versions
  Feature/write config (spf13#287)
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 this pull request may close these issues.

None yet