Skip to content

Commit

Permalink
Fix cmd version env && refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed May 25, 2023
1 parent 57274eb commit d7b821d
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 110 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [Usage](#usage)
- [Built-in subcommand](#built-in-subcommand)
- [Credits](#credits)
- [TO DO](#to-do)
- [Contribution](#contribution)
- [LICENSE](#license)

Expand Down Expand Up @@ -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

Expand Down
30 changes: 19 additions & 11 deletions cmd/tftools/commands.go → cmd/commands.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"fmt"
"os"
"runtime"

markdownrender "github.com/containerscrew/tftools/internal/utils/markdown_render"

Expand All @@ -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{
Expand All @@ -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)
},
}
File renamed without changes.
18 changes: 0 additions & 18 deletions cmd/tftools/root.go → cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ package cmd
import (
"fmt"
"os"
"runtime"

"github.com/dimiro1/banner"
"github.com/mattn/go-colorable"

"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 }}
Expand Down Expand Up @@ -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)
},
}
12 changes: 0 additions & 12 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 26 additions & 9 deletions internal/utils/markdown_render/pretty_markdown.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
53 changes: 0 additions & 53 deletions internal/utils/markdown_render/usage.md

This file was deleted.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
cmd "github.com/containerscrew/tftools/cmd/tftools"
"github.com/containerscrew/tftools/cmd"
)

func main() {
Expand Down

0 comments on commit d7b821d

Please sign in to comment.