Repository: chanzuckerberg/fogg
Pull request #518 (2021-01-22 00:20:36)

[feature] fogg fmt - standard formatting for fogg.yml
Add `fogg fmt` command which implements a standard formatting for the fogg.yml file (shared with other commands which update fogg.yml.

Various scripts, code editors and tools format yaml files differently, leading to diffs with a lot of noise. This command will help enforce a standardized format.

### Test Plan
* manually tested on all internal repos

### References
*

clubhouse[bot]: This pull request has been linked to [Clubhouse Story #8423: #265 ⁃ fogg fmt - standard formatting for fogg.yml](https://app.clubhouse.io/infraeng/story/8423/265-fogg-fmt-standard-formatting-for-foggyml).
mbarrien: Won't this strip out comments?
ryanking: > Won't this strip out comments?

Hmm yeah, which means we have been stripping comments out for awhile (via the other commands that update fogg.yml).
ryanking: @mbarrien after digging on this it seems that we can't have aggressive formatting (re-ordering of keys) and preservation of comments, so I am simplifying this to deal with just indentation. we can get more aggressive later
codecov-io: # [Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/518?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`main@803e439`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/chanzuckerberg/fogg/pull/518/graphs/tree.svg?width=650&height=150&src=pr&token=57DhtZ7g2A)](https://codecov.io/gh/chanzuckerberg/fogg/pull/518?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##             main     #518   +/-   ##
=======================================
  Coverage        ?   56.95%           
=======================================
  Files           ?       21           
  Lines           ?     1928           
  Branches        ?        0           
=======================================
  Hits            ?     1098           
  Misses          ?      662           
  Partials        ?      168           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/chanzuckerberg/fogg/pull/518?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/chanzuckerberg/fogg/pull/518?src=pr&el=footer). Last update [803e439...23dba4f](https://codecov.io/gh/chanzuckerberg/fogg/pull/518?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/cmd/fmt.go b/cmd/fmt.go
new file mode 100644
index 00000000..8d4810b1
--- /dev/null
+++ b/cmd/fmt.go
@@ -0,0 +1,80 @@
+package cmd
+
+import (
+	"io/ioutil"
+	"os"
+
+	"github.com/chanzuckerberg/fogg/errs"
+	"github.com/spf13/afero"
+	"github.com/spf13/cobra"
+	"gopkg.in/yaml.v3"
+)
+
+func init() {
+	// planCmd.Flags().StringP("config", "c", "fogg.yml", "Use this to override the fogg config file.")
+	rootCmd.AddCommand(fmtCmd)
+}
+
+var fmtCmd = &cobra.Command{
+	Use:           "fmt",
+	Short:         "format",
+	Long:          "",
+	SilenceErrors: true, // If we don't silence here, cobra will print them. But we want to do that in cmd/root.go
+	RunE: func(cmd *cobra.Command, args []string) error {
+
+		fs, err := pwdFs()
+		if err != nil {
+			return err
+		}
+
+		// if fogg.yml exists, read and format it
+		if fileExists("fogg.yml") {
+			n := &yaml.Node{}
+
+			f, err := fs.Open("fogg.yml")
+			if err != nil {
+				return errs.NewUser("could not open fogg.yml")
+			}
+			defer f.Close()
+
+			b, err := ioutil.ReadAll(f)
+			if err != nil {
+				return errs.WrapUser(err, "could not read fogg.yml")
+			}
+
+			err = yaml.Unmarshal(b, n)
+			if err != nil {
+				return errs.WrapUser(err, "unable to parse yaml")
+			}
+
+			out, err := yaml.Marshal(n)
+			if err != nil {
+				return errs.WrapUser(err, "unable to marshal yaml")
+			}
+			err = afero.WriteFile(fs, "fogg.yml", out, 0755)
+			if err != nil {
+				return err
+			}
+		}
+
+		return nil
+	},
+}
+
+func fileExists(filename string) bool {
+	info, err := os.Stat(filename)
+	if os.IsNotExist(err) {
+		return false
+	}
+	return !info.IsDir()
+}
+
+func pwdFs() (afero.Fs, error) {
+	// Set up fs
+	pwd, e := os.Getwd()
+	if e != nil {
+		return nil, errs.WrapUser(e, "can't get pwd")
+	}
+	fs := afero.NewBasePathFs(afero.NewOsFs(), pwd)
+	return fs, nil
+}
diff --git a/cmd/plan.go b/cmd/plan.go
index 07fef307..dbcd47a4 100644
--- a/cmd/plan.go
+++ b/cmd/plan.go
@@ -1,11 +1,8 @@
 package cmd
 
 import (
-	"os"
-
 	"github.com/chanzuckerberg/fogg/errs"
 	"github.com/chanzuckerberg/fogg/plan"
-	"github.com/spf13/afero"
 	"github.com/spf13/cobra"
 )
 
@@ -23,12 +20,10 @@ var planCmd = &cobra.Command{
 
 		var e error
 		// Set up fs
-		pwd, e := os.Getwd()
+		fs, e := pwdFs()
 		if e != nil {
-			return errs.WrapUser(e, "can't get pwd")
+			return e
 		}
-		fs := afero.NewBasePathFs(afero.NewOsFs(), pwd)
-
 		// handle flags
 		configFile, e := cmd.Flags().GetString("config")
 		if e != nil {
diff --git a/templates/repo/Makefile.tmpl b/templates/repo/Makefile.tmpl
index c9647c90..92dfa19a 100644
--- a/templates/repo/Makefile.tmpl
+++ b/templates/repo/Makefile.tmpl
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \


Test patch:
diff --git a/testdata/bless_provider_yaml/Makefile b/testdata/bless_provider_yaml/Makefile
index 957ea31b..25849d72 100644
--- a/testdata/bless_provider_yaml/Makefile
+++ b/testdata/bless_provider_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/circleci/Makefile b/testdata/circleci/Makefile
index 7fcd0b2b..526fab3b 100644
--- a/testdata/circleci/Makefile
+++ b/testdata/circleci/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/github_actions/Makefile b/testdata/github_actions/Makefile
index 7fcd0b2b..526fab3b 100644
--- a/testdata/github_actions/Makefile
+++ b/testdata/github_actions/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/github_provider_yaml/Makefile b/testdata/github_provider_yaml/Makefile
index 0d4e51b3..a688b05a 100644
--- a/testdata/github_provider_yaml/Makefile
+++ b/testdata/github_provider_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/okta_provider_yaml/Makefile b/testdata/okta_provider_yaml/Makefile
index 0d4e51b3..a688b05a 100644
--- a/testdata/okta_provider_yaml/Makefile
+++ b/testdata/okta_provider_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/snowflake_provider_yaml/Makefile b/testdata/snowflake_provider_yaml/Makefile
index 957ea31b..25849d72 100644
--- a/testdata/snowflake_provider_yaml/Makefile
+++ b/testdata/snowflake_provider_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/tfe_provider_yaml/Makefile b/testdata/tfe_provider_yaml/Makefile
index 0d4e51b3..a688b05a 100644
--- a/testdata/tfe_provider_yaml/Makefile
+++ b/testdata/tfe_provider_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/v2_full_yaml/Makefile b/testdata/v2_full_yaml/Makefile
index 27497ffc..efdb7b35 100644
--- a/testdata/v2_full_yaml/Makefile
+++ b/testdata/v2_full_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/v2_minimal_valid_yaml/Makefile b/testdata/v2_minimal_valid_yaml/Makefile
index 7fcd0b2b..526fab3b 100644
--- a/testdata/v2_minimal_valid_yaml/Makefile
+++ b/testdata/v2_minimal_valid_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \
diff --git a/testdata/v2_no_aws_provider_yaml/Makefile b/testdata/v2_no_aws_provider_yaml/Makefile
index cdb4d800..a74f2023 100644
--- a/testdata/v2_no_aws_provider_yaml/Makefile
+++ b/testdata/v2_no_aws_provider_yaml/Makefile
@@ -39,9 +39,13 @@ lint-modules: ## lint the terraform code in terraform/modules
 	done
 .PHONY: lint-modules
 
-fmt: fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
+fmt: fmt-fogg fmt-accounts fmt-envs fmt-global fmt-modules ## format code in this repo
 .PHONY: fmt
 
+fmt-fogg:
+	.fogg/bin/fogg fmt
+.PHONY: fmt-fogg
+
 fmt-accounts: ## format code in terraform/accounts
 	@for account in $(ACCOUNTS); do \
 		$(MAKE) -C terraform/accounts/$$account fmt || exit $$?; \