-
Notifications
You must be signed in to change notification settings - Fork 20
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] Add a command to migrate from v1 config to v2 configs #270
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d42c959
Add a command to migrate from v1 config to v2 configs
9a73742
Not necessary
0703e32
More omitempty
324118e
More omitempty
7da30ae
Omit more fields
a957795
Cleaner marshal
14f1127
Fix the backend situation
9ac1004
Merge master into edu/migrate-config
czimergebot a91d6e4
Tests
413615e
Code review
a4068ac
Fix comments
d15348d
imports
9ee0fe1
Merge master into edu/migrate-config
czimergebot 848b537
Merge branch 'master' of github.com:chanzuckerberg/fogg into edu/migr…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/chanzuckerberg/fogg/config" | ||
"github.com/chanzuckerberg/fogg/errs" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
upgrade.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.") | ||
rootCmd.AddCommand(upgrade) | ||
} | ||
|
||
var upgrade = &cobra.Command{ | ||
Use: "upgrade", | ||
Short: "Upgrades a fogg config", | ||
Long: `This command will upgrade a fogg config. | ||
Note that this might be a lossy transformation.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// Set up fs | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return errs.WrapUser(err, "can't get pwd") | ||
} | ||
fs := afero.NewBasePathFs(afero.NewOsFs(), pwd) | ||
|
||
// handle flags | ||
configFile, err := cmd.Flags().GetString("config") | ||
if err != nil { | ||
return errs.WrapInternal(err, "couldn't parse config flag") | ||
} | ||
|
||
// check that we are at root of initialized git repo | ||
openGitOrExit(fs) | ||
return config.Upgrade(fs, configFile) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
v1 "github.com/chanzuckerberg/fogg/config/v1" | ||
"github.com/chanzuckerberg/fogg/errs" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
// Upgrade applies in-place upgrades to a configFile | ||
func Upgrade(fs afero.Fs, configFile string) error { | ||
bytes, version, err := FindConfig(fs, configFile) | ||
if err != nil { | ||
return err | ||
} | ||
switch version { | ||
case 1: | ||
c1, err := v1.ReadConfig(bytes) | ||
if err != nil { | ||
return err | ||
} | ||
c2, err := UpgradeConfigVersion(c1) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
marshalled, err := json.MarshalIndent(c2, "", " ") | ||
if err != nil { | ||
return errs.WrapInternal(err, "Could not serialize config to json.") | ||
} | ||
err = afero.WriteFile(fs, configFile, marshalled, 0644) | ||
return errs.WrapInternal(err, "Could not write config to disk") | ||
case 2: | ||
logrus.Infof("config already v%d, nothing to do", version) | ||
return nil | ||
|
||
default: | ||
return errs.NewUserf("config version %d unrecognized", version) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/chanzuckerberg/fogg/util" | ||
) | ||
|
||
func TestUpgradeV2(t *testing.T) { | ||
r := require.New(t) | ||
confPath := "config" | ||
conf := []byte(`{"version": 1}`) | ||
fs, _, err := util.TestFs() | ||
r.Nil(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it matters, but this might be better as NoError |
||
err = afero.WriteFile(fs, confPath, conf, 0644) | ||
r.Nil(err) | ||
err = Upgrade(fs, confPath) | ||
r.Nil(err) | ||
} | ||
|
||
func TestUpgradeUnknownVersion(t *testing.T) { | ||
r := require.New(t) | ||
confPath := "config" | ||
conf := []byte(`{"version": 100}`) | ||
fs, _, err := util.TestFs() | ||
r.Nil(err) | ||
err = afero.WriteFile(fs, confPath, conf, 0644) | ||
r.Nil(err) | ||
err = Upgrade(fs, confPath) | ||
r.Error(err, "config version 100 unrecognized") | ||
} | ||
|
||
func TestUpgradeV1(t *testing.T) { | ||
r := require.New(t) | ||
confPath := "config" | ||
fs, _, err := util.TestFs() | ||
r.Nil(err) | ||
|
||
v1, err := util.TestFile("v1_full") | ||
r.NoError(err) | ||
|
||
err = afero.WriteFile(fs, confPath, v1, 0644) | ||
r.NoError(err) | ||
|
||
_, v, err := FindConfig(fs, confPath) | ||
r.NoError(err) | ||
r.Equal(1, v) | ||
|
||
err = Upgrade(fs, confPath) | ||
r.NoError(err) | ||
|
||
_, v, err = FindConfig(fs, confPath) | ||
r.NoError(err) | ||
r.Equal(2, v) // now upgraded | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do this so we don't end up including a bunch of
bucket: {}
in the output