Skip to content

Commit

Permalink
Use semver to sort migration versions
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
  • Loading branch information
awrichar committed Aug 8, 2022
1 parent fde766f commit ab1f049
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
28 changes: 18 additions & 10 deletions cmd/ffconfig/migrate/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package migrate
import (
"fmt"
"io/ioutil"
"sort"

"github.com/blang/semver/v4"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -127,6 +127,21 @@ var migrations = map[string]func(root *ConfigItem){
},
}

func getVersions() []semver.Version {
versions := make([]semver.Version, 0, len(migrations))
for k := range migrations {
versions = append(versions, semver.MustParse(k))
}
semver.Sort(versions)
return versions
}

func migrateVersion(root *ConfigItem, version string) {
fmt.Printf("Version %s\n", version)
migrations[version](root)
fmt.Println()
}

func Run(cfgFile string) error {
yfile, err := ioutil.ReadFile(cfgFile)
if err != nil {
Expand All @@ -137,16 +152,9 @@ func Run(cfgFile string) error {
if err != nil {
return err
}
versions := make([]string, 0, len(migrations))
for k := range migrations {
versions = append(versions, k)
}
sort.Strings(versions)
root := &ConfigItem{value: data}
for _, version := range versions {
fmt.Printf("Version %s\n", version)
migrations[version](root)
fmt.Println()
for _, version := range getVersions() {
migrateVersion(root, version.String())
}
out, err := yaml.Marshal(data)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Masterminds/squirrel v1.5.3
github.com/aidarkhanov/nanoid v1.0.8
github.com/blang/semver/v4 v4.0.0
github.com/docker/go-units v0.4.0
github.com/getkin/kin-openapi v0.96.0
github.com/ghodss/yaml v1.0.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edY
github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwjwegp5jy4=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/blang/semver v3.1.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
Expand Down

0 comments on commit ab1f049

Please sign in to comment.