|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "github.com/mickep76/iodatafmt" |
| 8 | +) |
| 9 | + |
| 10 | +func main() { |
| 11 | + if len(os.Args) < 2 { |
| 12 | + fmt.Print("Missing first parameter: file config.{toml,json,yml} path") |
| 13 | + os.Exit(1) |
| 14 | + } |
| 15 | + path := os.Args[1] |
| 16 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 17 | + panic(fmt.Errorf("Unable to find config file using path \"%s\", %s", path, err)) |
| 18 | + } |
| 19 | + f, err := iodatafmt.FileFormat(path) |
| 20 | + if err != nil { |
| 21 | + panic(fmt.Errorf("Unable to determine format of given config file \"%s\", %s", path, err)) |
| 22 | + } |
| 23 | + d, err := iodatafmt.Load(path, f) |
| 24 | + if err != nil { |
| 25 | + panic(fmt.Errorf("Unable to read config from given file \"%s\", %s", path, err)) |
| 26 | + } |
| 27 | + if val, ok := d.(map[string]interface {})["googleAnalytics"]; ok { |
| 28 | + fmt.Printf("googleAnalytics value already exists with: \"%s\"! No migration applied", val) |
| 29 | + os.Exit(0) |
| 30 | + } |
| 31 | + if _, ok := d.(map[string]interface {})["params"]; ok { |
| 32 | + if val, ok := d.(map[string]interface {})["params"].(map[string]interface {})["google_analytics_id"]; ok { |
| 33 | + fmt.Printf("Migrate\n=======\n\n[params]\n\tgoogle_analytics_id = %s\n\nto\n\ngoogleAnalytics = %s\n\n", val, val) |
| 34 | + d.(map[string]interface {})["googleAnalytics"] = val |
| 35 | + delete(d.(map[string]interface {})["params"].(map[string]interface {}), "google_analytics_id") |
| 36 | + } |
| 37 | + } |
| 38 | + m, err := iodatafmt.Marshal(d, f) |
| 39 | + if err != nil { |
| 40 | + panic(fmt.Errorf("Unable to marshal config, %s", err)) |
| 41 | + } |
| 42 | + ioutil.WriteFile(path + ".new", m, 0644) |
| 43 | + fmt.Printf("New configuration has been generated on %s", path + ".new") |
| 44 | +} |
0 commit comments