| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "os" |
| 8 | |
| 9 | "gopkg.in/ini.v1" |
| 10 | |
| 11 | "github.com/editorconfig/editorconfig-core-go/v2" |
| 12 | ) |
| 13 | |
| 14 | // version indicates the current version number. |
| 15 | var version = "dev" |
| 16 | |
| 17 | func main() { |
| 18 | var ( |
| 19 | configName string |
| 20 | configVersion string |
| 21 | showVersionFlag bool |
| 22 | ) |
| 23 | |
| 24 | flag.StringVar(&configName, "f", editorconfig.ConfigNameDefault, "Specify conf filename other than '.editorconfig'") |
| 25 | flag.StringVar(&configVersion, "b", "", "Specify version (used by devs to test compatibility)") |
| 26 | flag.BoolVar(&showVersionFlag, "v", false, "Display version information") |
| 27 | flag.BoolVar(&showVersionFlag, "version", false, "Display version information") |
| 28 | flag.Parse() |
| 29 | |
| 30 | if showVersionFlag { |
| 31 | fmt.Printf("EditorConfig Core Go, Version %s\n", version) //nolint:forbidigo |
| 32 | os.Exit(0) |
| 33 | } |
| 34 | |
| 35 | rest := flag.Args() |
| 36 | |
| 37 | if len(rest) < 1 { |
| 38 | flag.Usage() |
| 39 | os.Exit(1) |
| 40 | } |
| 41 | |
| 42 | config := &editorconfig.Config{ |
| 43 | Name: configName, |
| 44 | Version: configVersion, |
| 45 | Graceful: false, |
| 46 | } |
| 47 | |
| 48 | if len(rest) > 1 { |
| 49 | config.Parser = editorconfig.NewCachedParser() |
| 50 | } |
| 51 | |
| 52 | for _, file := range rest { |
| 53 | def, err := config.Load(file) |
| 54 | if err != nil { |
| 55 | log.Fatal(err) |
| 56 | } |
| 57 | |
| 58 | iniFile := ini.Empty() |
| 59 | ini.PrettyFormat = false |
| 60 | |
| 61 | if len(rest) < 2 { |
| 62 | def.Selector = ini.DefaultSection |
| 63 | } else { |
| 64 | def.Selector = file |
| 65 | } |
| 66 | |
| 67 | def.InsertToIniFile(iniFile) |
| 68 | |
| 69 | _, err = iniFile.WriteTo(os.Stdout) |
| 70 | if err != nil { |
| 71 | log.Fatal(err) |
| 72 | } |
| 73 | } |
| 74 | } |