Skip to content

Commit

Permalink
Update docs & split pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed May 6, 2024
1 parent afb402b commit 32b3d5b
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 84 deletions.
24 changes: 2 additions & 22 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
name: Build 🛠️
name: Build
on:
pull_request:
push:
# tags:
# - '*'
branches:
- '*'

permissions:
contents: write

jobs:
build:
name: Go build 🚀
name: Build 🚀
runs-on: ubuntu-latest
steps:
- name: checkout
Expand All @@ -24,18 +17,5 @@ jobs:
go-version: '1.22'
cache: false

- name: security
uses: securego/gosec@master
with:
args: -exclude=G104,G204,G107 ./...

- name: lint
uses: golangci/golangci-lint-action@v5
with:
version: latest

- name: test
run: go test -v ./...

- name: build
run: go build
1 change: 1 addition & 0 deletions .github/workflows/gitleaks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
# workflow_dispatch:
# schedule:
# - cron: "0 4 * * *" # run once a day at 4 AM

jobs:
scan:
name: gitleaks
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/gosec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build 🛠️
on:
pull_request:
push:
# tags:
# - '*'
branches:
- '*'

permissions:
contents: write

jobs:
build:
name: Go build 🚀
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- name: setup-go
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false

- name: security
uses: securego/gosec@master
with:
args: -exclude=G104,G204,G107 ./...
24 changes: 24 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lint

on:
pull_request:
push:

jobs:
lint:
name: Lint 🚀
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- name: setup-go
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false

- name: lint
uses: golangci/golangci-lint-action@v5
with:
version: latest
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# There are not tests, but... just prepare the pipeline for the future
name: Test
on:
pull_request:
push:

jobs:
test:
name: Test 🚀
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- name: setup-go
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false

- name: test
run: go test -v ./...
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
![Release Status](https://github.com/containerscrew/tftools/actions/workflows/release.yml/badge.svg)
![Build Status](https://github.com/containerscrew/tftools/actions/workflows/build.yml/badge.svg)
![Git Leaks Status](https://github.com/containerscrew/tftools/actions/workflows/gitleaks.yml/badge.svg)
![Lint Status](https://github.com/containerscrew/tftools/actions/workflows/lint.yml/badge.svg)
![Gosec Status](https://github.com/containerscrew/tftools/actions/workflows/gosec.yml/badge.svg)
![Test Status](https://github.com/containerscrew/tftools/actions/workflows/test.yml/badge.svg)
[![License](https://img.shields.io/github/license/containerscrew/tftools)](/LICENSE)
[![Release](https://img.shields.io/github/release/containerscrew/tftools)](https://github.com/containerscrew/tftools/releases/latest)
[![GitHub Releases Stats](https://img.shields.io/github/downloads/containerscrew/tftools/total.svg?logo=github)](https://somsubhra.github.io/github-release-stats/?username=containerscrew&repository=tftools)
Expand Down Expand Up @@ -93,7 +96,6 @@ tftools usage

# TO DO

* When there are updates add flag --show-updates-output
* Improve error handling
* Add tests, although I have no experience
* Code refactor is certainly needed!
Expand Down
Binary file modified assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 0 additions & 20 deletions build/Dockerfile

This file was deleted.

13 changes: 0 additions & 13 deletions docs/develop.md

This file was deleted.

51 changes: 23 additions & 28 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@

```shell
terraform plan -out plan.tfplan
terraform show -json plan.tfplan | tftools summarize --show-tags
terraform show -json plan.tfplan | tftools summarize --show-tags --compact
```

Or if you have the file in json:
Or if you have the file already in json:

```shell
terraform plan -out plan.tfplan
terraform show -json plan.tfplan > plan.json
terraform show -json plan.tfplan > demo.json
cat plan.json | tftools summarize
# or
tftools summarize --compact --show-tags <demo.json
```

## Function for zsh
Expand Down Expand Up @@ -79,33 +81,27 @@ Edit your `~/.bashrc`
```shell
tfsum() {
if [ -z "$1" ]; then
echo "You should type 'tfsum terraform|terragrunt'"
exit 1
fi

echo -en "Starting tf summary... Please wait\n"

if [ -n "$2" ] && [ "$2" == "-v" ]; then
"$1" plan -out plan.tfplan
else
"$1" plan -out plan.tfplan 1> /dev/null
fi

"$1" show -json plan.tfplan | tftools summarize --show-tags
if [ -f "plan.tfplan" ]; then rm plan.tfplan; fi
(
# Enable pipefail within the subshell
set -o pipefail

# Create plan and pass through any arguments
# Make a random tfplan filename in /tmp
TMP_FILE=$(mktemp /tmp/tfplan.XXXXXX)

# Execute terraform plan and other commands
terraform plan -lock=false -compact-warnings -out=${TMP_FILE} "$@" |
# Remove the line mentioning where the plan was saved
awk '!/Saved the plan to/{print;next} /Saved the plan to/{exit}' &&
terraform show -json ${TMP_FILE} |
tftools summarize --show-tags --show-unchanged --compact &&
rm ${TMP_FILE}
)
}
```
> [!NOTE]
> Note that the bash function has the possibility of activating the output or not using the `-v` flag
```shell
tfsum terraform -v # show full output
```
> [!WARNING]
> Adapt the rest of zsh or fish functions according to your needs.
> Adapt the rest of zsh, fish or bash functions according to your needs.
## Load new functions
Expand All @@ -125,7 +121,6 @@ sudo cp scripts/tfsum.sh /usr/local/bin/tfsum
> `/usr/local/bin` or other directory included in your path
Inside the container image of [infratools](https://github.com/containerscrew/infratools), this custom binary is already contemplated.
# Example
Expand All @@ -142,7 +137,7 @@ The example:
> The following example is using the full output command
```shell
tftools summarize --show-tags --show-unchanged <Documents/plan.json
tftools summarize --show-tags --show-unchanged --compact <demo.json
```
![example](../assets/example.png)
Expand Down
74 changes: 74 additions & 0 deletions scripts/tfsum_old.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash

set -eu

echo -en "\e[32m> Starting tfsum...\e[0m\n"

# Default variables
tf_command="terraform"
tf_action="plan"
tf_params='-parallelism=100'

# Function to display help text
usage() {
echo "Usage: $0 [-c] [-a] [-p] [-h]"
echo "Options:"
echo " -c Command. Execute terragrunt or terraform. (Required)"
echo " -a Action. Execute plan, apply or destroy. (Optional)(Default: plan)"
echo " -p Params. Terraform params like: -auto-approve, -parallelism ...etc. (Optional)(Default: -parallelism=100)"
echo " -h Display the help message"
echo -en "\e[32m> Example: $ tfsum -c terraform -a plan -p '-parallelism=50 -no-color'\e[0m\n"
}

# Parse options using getopts
while getopts "c:a:p:h" option; do
case "${option}" in
c) # Set command
tf_command=${OPTARG}
if [ "${OPTARG}" != "terraform" ] && [ "${OPTARG}" != "terragrunt" ]; then
echo -en "\e[31m> Invalid command ${OPTARG}. Supported commands are: terraform, terragrunt\e[0m\n"
exit 1
fi
;;
a) # Set action
tf_action=${OPTARG}
if [ "${OPTARG}" != "plan" ] && [ "${OPTARG}" != "apply" ] && [ "${OPTARG}" != "delete" ]; then
echo -en "\e[31m> Invalid command. Supported commands are: plan, apply, delete\e[0m\n"
exit 1
fi
;;
p) # Terraform params
tf_params="${OPTARG//\'/}"
;;
h) # Help option
usage
exit 0
;;
\?) # Invalid option
echo "Invalid option: -${OPTARG}"
usage
exit 1
;;
esac
done


# If no flags, print usage
if [ $# -eq 0 ]; then
echo -en "\e[31m> You should type \$ tfsum -c terraform/terragrunt at least.\e[0m\n"
usage
exit 1
fi

# Start program
echo -en "\e[32m> Executing $tf_command $tf_action $tf_params\e[0m\n"
if [ "$tf_action" == "plan" ]; then
tf_params+=" -out plan.tfplan"
# shellcheck disable=SC2086
$tf_command "$tf_action" $tf_params
# Print summarized plan
$tf_command show -json plan.tfplan | tftools summarize --show-tags
else
# shellcheck disable=SC2086
$tf_command "$tf_action" $tf_params
fi

0 comments on commit 32b3d5b

Please sign in to comment.