Repository: editorconfig-checker/editorconfig-checker
Pull request #140 (2021-01-09 11:18:00)

feat(config): Add version fild on config

codecov[bot]: # [Codecov](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140?src=pr&el=h1) Report
> Merging [#140](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140?src=pr&el=desc) (0c307b7) into [master](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/commit/cc55c0a48aa9430880d3047210fa97cc46cd4bca?el=desc) (cc55c0a) will **increase** coverage by `0.09%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140/graphs/tree.svg?width=650&height=150&src=pr&token=sohJctRkEB)](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #140      +/-   ##
==========================================
+ Coverage   88.85%   88.94%   +0.09%     
==========================================
  Files           7        7              
  Lines         368      371       +3     
==========================================
+ Hits          327      330       +3     
  Misses         31       31              
  Partials       10       10              
```


| [Impacted Files](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140/diff?src=pr&el=tree#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140?src=pr&el=footer). Last update [cc55c0a...0c307b7](https://codecov.io/gh/editorconfig-checker/editorconfig-checker/pull/140?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.ecrc b/.ecrc
index 2bb627e..44b442d 100644
--- a/.ecrc
+++ b/.ecrc
@@ -1,4 +1,5 @@
 {
+  "Version": "2.2.0",
   "Verbose": false,
   "Debug": false,
   "IgnoreDefaults": false,
diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml
index 39fdfb4..6d2a098 100644
--- a/.github/workflows/nix.yml
+++ b/.github/workflows/nix.yml
@@ -5,6 +5,6 @@ jobs:
   build:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v1
-      - uses: cachix/install-nix-action@v12
-      - run: nix-build
+    - uses: actions/checkout@v1
+    - uses: cachix/install-nix-action@v3
+    - run: nix-build -E 'with import <nixpkgs> { }; callPackage ./default.nix {}'
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ee0223f..1dd2b0c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@
 
 ### Added
 
+- Add `version` field on config [#140](https://github.com/editorconfig-checker/editorconfig-checker/pull/140)
+
 ### Deprecated
 
 ### Removed
diff --git a/Makefile b/Makefile
index 74a12e0..a6311f2 100644
--- a/Makefile
+++ b/Makefile
@@ -68,6 +68,7 @@ _tag_version:
 	@read -p "Enter version to release: " version && \
 	echo $${version} > VERSION && \
 	sed -i "s/VERSION=".*"/VERSION=\"$${version}\"/" ./README.md && \
+	sed -i "s/\"Version\": \".*\",/\"Version\": \"$${version}\",/" .ecrc && \
 	git add . && git commit -m "chore(release): $${version}" && git tag "$${version}" && \
 	git push origin master && git push origin master --tags
 
diff --git a/README.md b/README.md
index 5cb8f16..729196b 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,6 @@
 6.2.5 [via arguments](#via-arguments)  
 6.2.6 [Generally](#generally)  
 7. [Docker](#docker)
-8. [Continuous Integration](#continous-integration)
 8. [Support](#support)
 
 
@@ -272,21 +271,6 @@ To do this you need to have Docker installed and run this command in your reposi
 
 Dockerhub: [mstruebing/editorconfig-checker](https://hub.docker.com/r/mstruebing/editorconfig-checker)
 
-## Continuous Integration
-
-### Mega-Linter
-
-Instead of installing and configuring `editorconfig-checker` and all other linters in your project CI workflows (GitHub Actions & others), you can use [Mega-Linter](https://nvuillam.github.io/mega-linter/) which does all that for you with a single [assisted installation](https://nvuillam.github.io/mega-linter/installation/)
-
-Mega-Linter embeds [editorconfig-checker](https://nvuillam.github.io/mega-linter/descriptors/editorconfig_editorconfig_checker/) by default in all its [flavors](https://nvuillam.github.io/mega-linter/flavors/), meaning that it will be run at each commit or Pull Request to detect any issue related to .`editorconfig`
-
-If you want to use only `editorconfig-checker` and not the 70+ other linters, you can use the following `.mega-linter.yml` configuration file
-
-```yaml
-ENABLE:
-- EDITORCONFIG
-```
-
 ## Support
 If you have any questions, suggestions, need a wrapper for a programming language or just want to chat join #editorconfig-checker on 
 freenode(IRC).
diff --git a/cmd/editorconfig-checker/main.go b/cmd/editorconfig-checker/main.go
index f2837ca..783b132 100644
--- a/cmd/editorconfig-checker/main.go
+++ b/cmd/editorconfig-checker/main.go
@@ -35,7 +35,7 @@ func init() {
 	flag.StringVar(&tmpExclude, "exclude", "", "a regex which files should be excluded from checking - needs to be a valid regular expression")
 	flag.BoolVar(&c.IgnoreDefaults, "ignore-defaults", false, "ignore default excludes")
 	flag.BoolVar(&c.DryRun, "dry-run", false, "show which files would be checked")
-	flag.BoolVar(&c.Version, "version", false, "print the version number")
+	flag.BoolVar(&c.ShowVersion, "version", false, "print the version number")
 	flag.BoolVar(&c.Help, "help", false, "print the help")
 	flag.BoolVar(&c.Help, "h", false, "print the help")
 	flag.BoolVar(&c.Verbose, "verbose", false, "print debugging information")
@@ -58,7 +58,7 @@ func init() {
 	currentConfig, _ = config.NewConfig(configFilePath)
 
 	if init {
-		err := currentConfig.Save()
+		err := currentConfig.Save(version)
 		if err != nil {
 			logger.Error(err.Error())
 			os.Exit(1)
@@ -90,6 +90,12 @@ func main() {
 	config.Logger.Debug(config.GetAsString())
 	config.Logger.Verbose("Exclude Regexp: %s", config.GetExcludesAsRegularExpression())
 
+	if config.Version != version {
+		config.Logger.Error("Version from config file is not the same as the version of the binary")
+		config.Logger.Error(fmt.Sprintf("Binary: %s, Config %s", version, config.Version))
+		os.Exit(1)
+	}
+
 	// Check for returnworthy arguments
 	shouldExit := ReturnableFlags(config)
 	if shouldExit {
@@ -132,12 +138,12 @@ func main() {
 // ReturnableFlags returns wether a flag passed should exit the program
 func ReturnableFlags(config config.Config) bool {
 	switch {
-	case config.Version:
+	case config.ShowVersion:
 		config.Logger.Output(version)
 	case config.Help:
 		config.Logger.Output("USAGE:")
 		flag.PrintDefaults()
 	}
 
-	return config.Version || config.Help
+	return config.ShowVersion || config.Help
 }
diff --git a/pkg/config/config.go b/pkg/config/config.go
index d4a2498..b4f4f73 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -57,12 +57,13 @@ var defaultAllowedContentTypes = []string{
 // Config struct, contains everything a config can contain
 type Config struct {
 	// CLI
-	Version bool
-	Help    bool
-	DryRun  bool
-	Path    string
+	ShowVersion bool
+	Help        bool
+	DryRun      bool
+	Path        string
 
 	// CONFIG FILE
+	Version             string
 	Verbose             bool
 	Debug               bool
 	IgnoreDefaults      bool
@@ -129,7 +130,11 @@ func (c *Config) Merge(config Config) {
 		c.DryRun = config.DryRun
 	}
 
-	if config.Version {
+	if config.ShowVersion {
+		c.ShowVersion = config.ShowVersion
+	}
+
+	if len(config.Version) > 0 {
 		c.Version = config.Version
 	}
 
@@ -218,12 +223,13 @@ func (c Config) GetExcludesAsRegularExpression() string {
 }
 
 // Save saves the config to it's Path
-func (c Config) Save() error {
+func (c Config) Save(version string) error {
 	if utils.IsRegularFile(c.Path) {
 		return fmt.Errorf("File `%v` already exists", c.Path)
 	}
 
 	type writtenConfig struct {
+		Version             string
 		Verbose             bool
 		Debug               bool
 		IgnoreDefaults      bool
@@ -235,7 +241,7 @@ func (c Config) Save() error {
 		Disable             DisabledChecks
 	}
 
-	configJSON, _ := json.MarshalIndent(writtenConfig{}, "", "  ")
+	configJSON, _ := json.MarshalIndent(writtenConfig{Version: version}, "", "  ")
 	configString := strings.Replace(string(configJSON[:]), "null", "[]", -1)
 	err := ioutil.WriteFile(c.Path, []byte(configString), 0644)
 


Test patch:
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index 7d71fed..e82f52a 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -79,7 +79,8 @@ func TestMerge(t *testing.T) {
 	}
 
 	mergeConfig = Config{
-		Version:             true,
+		ShowVersion:         true,
+		Version:             "2.2.0",
 		Help:                true,
 		DryRun:              true,
 		Path:                "some-other",
@@ -156,11 +157,11 @@ func TestSave(t *testing.T) {
 	defer os.RemoveAll(dir)
 	configFile := filepath.Join(dir, "config")
 	c, _ := NewConfig(configFile)
-	if c.Save() != nil {
+	if c.Save("VERSION") != nil {
 		t.Error("Should create the config")
 	}
 
-	if c.Save() == nil {
+	if c.Save("VERSION") == nil {
 		t.Error("Should produce an error")
 	}
 }
@@ -170,7 +171,7 @@ func TestGetAsString(t *testing.T) {
 	_ = c.Parse()
 
 	actual := c.GetAsString()
-	expected := "Config: {Version:false Help:false DryRun:false Path:../../.ecrc Verbose:false Debug:false IgnoreDefaults:false SpacesAftertabs:false NoColor:false Exclude:[testfiles] AllowedContentTypes:[text/ application/octet-stream] PassedFiles:[] Disable:{EndOfLine:false Indentation:false InsertFinalNewline:false TrimTrailingWhitespace:false IndentSize:false MaxLineLength:false} Logger:{Verbosee:false Debugg:false NoColor:false}}"
+	expected := "Config: {ShowVersion:false Help:false DryRun:false Path:../../.ecrc Version:2.2.0 Verbose:false Debug:false IgnoreDefaults:false SpacesAftertabs:false NoColor:false Exclude:[testfiles] AllowedContentTypes:[text/ application/octet-stream] PassedFiles:[] Disable:{EndOfLine:false Indentation:false InsertFinalNewline:false TrimTrailingWhitespace:false IndentSize:false MaxLineLength:false} Logger:{Verbosee:false Debugg:false NoColor:false}}"
 
 	if actual != expected {
 		t.Errorf("Expected: %v, got: %v ", expected, actual)
diff --git a/testfiles/generated-config.json b/testfiles/generated-config.json
index 1eba2a7..7acad0a 100644
--- a/testfiles/generated-config.json
+++ b/testfiles/generated-config.json
@@ -1,4 +1,5 @@
 {
+  "Version": "2.2.0",
   "Verbose": false,
   "Debug": false,
   "IgnoreDefaults": false,