diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2754525..b951c47 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,10 +23,6 @@ jobs: go-version: '1.20' cache: false - # Generate data for embed files - - name: go-generate - run: go generate ./... - - name: security uses: securego/gosec@master with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b835f4e..fbbc3cd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,8 +9,6 @@ repos: - repo: https://github.com/dnephin/pre-commit-golang rev: v0.5.1 hooks: - - id: go-generate - args: [./...] - id: go-fmt - id: go-imports - id: golangci-lint diff --git a/README.md b/README.md index 0609ccc..5c63899 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ - [Usage](#usage) - [Built-in subcommand](#built-in-subcommand) - [Credits](#credits) +- [TO DO](#to-do) - [Contribution](#contribution) - [LICENSE](#license) @@ -81,12 +82,22 @@ Take a look inside docs [usage](./docs/usage.md) tftools usage ``` +> Requires internet connectivity, as it fetches the [usage.md](https://raw.githubusercontent.com/containerscrew/tftools/main/docs/usage.md) file. + # Credits - [Cobra to build beautiful CLI](https://cobra.dev/) - [Terraform json structs for data parsing](https://github.com/hashicorp/terraform-json) - [Distroless for container build](https://github.com/GoogleContainerTools/distroless) - [Glamour markdown render](https://github.com/charmbracelet/glamour) - [Official issue to solve this concern](https://github.com/hashicorp/terraform/issues/10507) +- [Git leaks](https://github.com/gitleaks/gitleaks-action) + +# TO DO + +* Improve error handling +* Create generic `install.sh` +* Add tests, although I have no experience +* Code refactor is certainly needed! # Contribution diff --git a/cmd/tftools/commands.go b/cmd/commands.go similarity index 60% rename from cmd/tftools/commands.go rename to cmd/commands.go index b5701a4..614b412 100644 --- a/cmd/tftools/commands.go +++ b/cmd/commands.go @@ -1,7 +1,9 @@ package cmd import ( + "fmt" "os" + "runtime" markdownrender "github.com/containerscrew/tftools/internal/utils/markdown_render" @@ -10,16 +12,12 @@ import ( "github.com/spf13/cobra" ) -//func Test() *cobra.Command { -// return &cobra.Command{ -// Use: "test", -// Short: "Test subcommand", -// Long: "Testing subcommand", -// Run: func(cmd *cobra.Command, args []string) { -// fmt.Println("Testing subcommand...") -// }, -// } -//} +var ( + version string + goversion = runtime.Version() + goos = runtime.GOOS + goarch = runtime.GOARCH +) // summarizeCmd will parse the tf plan output json to scrape created|updated|deleted resources in a clear outout var summarizeCmd = &cobra.Command{ @@ -40,8 +38,18 @@ var summarizeCmd = &cobra.Command{ var usageCmd = &cobra.Command{ Use: "usage", Short: "print usage", - Long: "print usage in a pretty markdown render using terminal", + Long: "print usage in a pretty markdown render using terminal. This require internet connection since it fetch usage.md from github url", Run: func(cmd *cobra.Command, args []string) { markdownrender.RenderUsage() }, } + +// versionCmd will print the current installed version in your local +var versionCmd = &cobra.Command{ + Use: "version", + Short: "tftools current version", + Long: "Get the cli tftools version installed", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("tftools: %s with go version %s %s/%s", version, goversion, goos, goarch) + }, +} diff --git a/cmd/tftools/flags.go b/cmd/flags.go similarity index 100% rename from cmd/tftools/flags.go rename to cmd/flags.go diff --git a/cmd/tftools/root.go b/cmd/root.go similarity index 68% rename from cmd/tftools/root.go rename to cmd/root.go index 8fe83a0..bc89e70 100644 --- a/cmd/tftools/root.go +++ b/cmd/root.go @@ -6,7 +6,6 @@ package cmd import ( "fmt" "os" - "runtime" "github.com/dimiro1/banner" "github.com/mattn/go-colorable" @@ -14,13 +13,6 @@ import ( "github.com/spf13/cobra" ) -var ( - version string - goversion = runtime.Version() - goos = runtime.GOOS - goarch = runtime.GOARCH -) - // printBanner will print an ascii banner with colors func printBanner() { templ := `{{ .AnsiColor.BrightMagenta }} {{ .Title "tftools" "" 2 }}{{ .AnsiColor.Default }} @@ -50,13 +42,3 @@ func Execute() { os.Exit(1) } } - -// versionCmd will print the current installed version in your local -var versionCmd = &cobra.Command{ - Use: "version", - Short: "tftools current version", - Long: "Get the cli tftools version installed", - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("tftools: %v with go version %s %s/%s", version, goversion, goos, goarch) - }, -} diff --git a/docs/install.md b/docs/install.md index 98d956c..3b5afe0 100644 --- a/docs/install.md +++ b/docs/install.md @@ -17,22 +17,10 @@ ## Using go -Go install is not a good solution because in order to compile the binary you must first run `go generate ./...` - ```bash go install github.com/containerscrew/tftools ``` -> **NOTE:** this will fail as I mentioned 🛠️ - -You can do the following: - -```bash -git clone https://github.com/containerscrew/tftools.git && cd tftools -go generate ./... -go install . -``` - ## Using brew ```bash diff --git a/internal/utils/markdown_render/pretty_markdown.go b/internal/utils/markdown_render/pretty_markdown.go index ddd1ee5..2e3ca3f 100644 --- a/internal/utils/markdown_render/pretty_markdown.go +++ b/internal/utils/markdown_render/pretty_markdown.go @@ -1,26 +1,43 @@ package markdownrender import ( - "embed" "fmt" + "io" "log" + "net/http" "github.com/charmbracelet/glamour" ) -// Embed usage documentation. -//go:generate cp -r ../../../docs/usage.md ./usage.md -//go:embed usage.md +const ( + usageURL = "https://raw.githubusercontent.com/containerscrew/tftools/main/docs/usage.md" +) -var usage embed.FS +func ReadUsageFile() string { + resp, err := http.Get(usageURL) -func RenderUsage() { - usage, err := usage.ReadFile("docs/usage.md") if err != nil { - log.Fatal(err) + log.Println(err) + } + + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Println(resp.StatusCode) + } + + data, err := io.ReadAll(resp.Body) + if err != nil { + log.Println(err) } - out, err := glamour.Render(string(usage), "dark") + return string(data) +} + +func RenderUsage() { + data := ReadUsageFile() + + out, err := glamour.Render(data, "dark") if err != nil { log.Println(err) } diff --git a/internal/utils/markdown_render/usage.md b/internal/utils/markdown_render/usage.md deleted file mode 100644 index 301498b..0000000 --- a/internal/utils/markdown_render/usage.md +++ /dev/null @@ -1,53 +0,0 @@ - - -**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)* - -- [Usage](#usage) - - [Summarize](#summarize) - - [Function for ~/.zshrc](#function-for-zshrc) - - - -# Usage - -## Summarize - -```bash -terraform plan -out plan.tfplan -terraform show -json plan.tfplan | tftools summarize -``` - -Or if you have the file in json: - -```bash -terraform plan -out plan.tfplan -terraform show -json plan.tfplan > plan.json -cat plan.json | tftools summarize -``` - -## Function for ~/.zshrc - -Copy [this function](../scripts/tfsum.sh) in your `~/.zshrc` or `~/.bashrc` file. - -```bash -function tfsum() { - if [ -z "$1" ]; - then - echo "You should type 'tfsum terraform|terragrunt'" - else - echo -e "Starting tf summary..." - # Don't print output of terraform plan - # If you don't want full plan output: $1 plan -out plan.tfplan 1> /dev/null - $1 plan -out plan.tfplan - echo -e "\n" - $1 show -json plan.tfplan | tftools summarize - # Delete plan out file to avoid git tracking (although is included in .gitignore) - if [ -f "plan.tfplan" ]; then rm plan.tfplan; fi - fi -} -``` - -```bash -source ~/.zshrc -tfsum terragrunt or tfsum terraform -``` diff --git a/main.go b/main.go index ac900ad..4a5b813 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - cmd "github.com/containerscrew/tftools/cmd/tftools" + "github.com/containerscrew/tftools/cmd" ) func main() {