-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Add a command to migrate from v1 config to v2 configs (#270)
[feature] Add a command to migrate from v1 config to v2 configs### Summary `fogg upgrade` ### Test Plan Say unittests ### References
- Loading branch information
1 parent
f7e9f73
commit 45439ea
Showing
10 changed files
with
215 additions
and
55 deletions.
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,57 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/chanzuckerberg/fogg/util" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUpgradeV2(t *testing.T) { | ||
r := require.New(t) | ||
confPath := "config" | ||
conf := []byte(`{"version": 1}`) | ||
fs, _, err := util.TestFs() | ||
r.Nil(err) | ||
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.