Skip to content

Commit

Permalink
🎉 start new module
Browse files Browse the repository at this point in the history
  • Loading branch information
ptavares committed Dec 10, 2023
1 parent fbba19f commit 6c02fee
Show file tree
Hide file tree
Showing 11 changed files with 941 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Test PowerShell
on: [push, pull_request]

jobs:
lint-with-PSScriptAnalyzer:
name: Install and run PSScriptAnalyzer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install PSScriptAnalyzer module
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module PSScriptAnalyzer -ErrorAction Stop
- name: Lint ps1 scripts with PSScriptAnalyzer
shell: pwsh
run: |
Invoke-ScriptAnalyzer -Path *.ps1 -Recurse -Outvariable issues
$errors = $issues.Where({$_.Severity -eq 'Error'})
$warnings = $issues.Where({$_.Severity -eq 'Warning'})
if ($errors) {
Write-Error "There were $($errors.Count) errors and $($warnings.Count) warnings total." -ErrorAction Stop
} else {
Write-Output "There were $($errors.Count) errors and $($warnings.Count) warnings total."
}
- name: Lint psm1 scripts with PSScriptAnalyzer
shell: pwsh
run: |
Invoke-ScriptAnalyzer -Path *.ps1 -Recurse -Outvariable issues
$errors = $issues.Where({$_.Severity -eq 'Error'})
$warnings = $issues.Where({$_.Severity -eq 'Warning'})
if ($errors) {
Write-Error "There were $($errors.Count) errors and $($warnings.Count) warnings total." -ErrorAction Stop
} else {
Write-Output "There were $($errors.Count) errors and $($warnings.Count) warnings total."
}
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish PowerShell Module
on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
publish-to-gallery:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Publish
# API key generated in PSGallery
env:
NUGET_KEY: ${{ secrets.NUGGET_KEY }}
shell: pwsh
run: |
Publish-Module -path ./terraform-tools -NuGetApiKey $env:NUGET_KEY -Verbose
Empty file added CHANGELOG.md
Empty file.
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# PowerShell Terraform Tools

[![license](https://img.shields.io/github/license/ptavares/powershell-terraform-tools.svg?style=flat-square)](./LICENSE)

## Description

A [PowerShell](https://microsoft.com/powershell) module for [Terraform](https://www.terraform.io/), a tool from [Hashicorp](https://www.hashicorp.com/) for managing infrastructure safely and efficiently.

It will install the following [Terraform](https://www.terraform.io/) tools :
- [terraform-docs](https://github.com/terraform-docs/terraform-docs)
- [tfsec](https://github.com/aquasecurity/tfsec)
- [tflint](https://github.com/terraform-linters/tflint)
- [tfautomv](https://github.com/busser/tfautomv)

It also provides some useful terraform aliases for everyday use.

## Table of content

## ⚙️ Installation

Install from [PowerShell Gallery](https://www.powershellgallery.com/packages)

```powershell
Install-Module terraform-tools -Scope CurrentUser -AllowClobber
```

Or from [Scoop](https://github.com/ScoopInstaller/Extras/blob/master/bucket/git-aliases.json)

```powershell
scoop bucket add extras
scoop install terraform-tools
```

---

⚠️ If you haven't allowed script execution policy, set your script execution policy to `RemoteSigned` or `Unrestricted`.

```powershell
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
```

## 🛂 Usage

You have to import the module to use `terraform-tools`.

Add below command into your PowerShell profile.

```powershell
Import-Module terraform-tools -DisableNameChecking
```

Then restart your PowerShell.

- First time : will download, install and add all Terraform tools managed by this module to your user `$PATH`
- Then : will only add tools to your `$PATH`

Now you can use wanted tool or uses wanted [aliases](#aliases).

> 🛈 Install terraform command using tfswitch command
```powershell
tfswitch
```


---

⚠️ If you don't have PowerShell profile yet, create it with below command!

```powershell
New-Item -ItemType File $profile
```

### Aliases

Here is the list of availabe aliases provides by this module :

| Alias | Command |
| ----------- | -------------------- |
| `tf` | `terraform` |
| `tff` | `tf fmt` |
| `tfv` | `tf validate` |
| `tfi` | `tf init` |
| `tfp` | `tf plan` |
| `tfa` | `tf apply` |
| `tfd` | `tf destroy` |
| `tfo` | `tf output` |
| `tfr` | `tf refresh` |
| `tfs` | `tf show` |
| `tfw` | `tf workspace` |
| `tffr` | `tff -recursive` |
| `tfip` | `tfi & tfp` |
| `tfia` | `tfi & tfa` |
| `tfid` | `tfi & tfd` |
| `tfa!` | `tfa -auto-approve` |
| `tfia!` | `tfi && tfa!` |
| `tfd!` | `tfd -auto-approve` |
| `tfid!` | `tfi && tfd!` |
| `tfversion` | `tf version` |

### Function

`tfws [workspace_name]`

Will execute command :

`tfw select -or-create [workspace_name]`

### Updating Terraform tools

The module comes with a PowerShell function to update all Terraform tools when you want

```powershell
Update-TerraformTools
```

## License

[MIT](./LICENCE)
115 changes: 115 additions & 0 deletions make-release.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
param (
[Parameter(Mandatory)]
[string]$versionBump
)

###############################################################################
# Contants
###############################################################################

# Define the path to module manifest file
$manifestPath = "terraform-tools\terraform-tools.psd1"

###############################################################################
# Check Scripts Inputs
###############################################################################

# Check if version bump is provided
if (-not $versionBump) {
$scriptName = $MyInvocation.MyCommand.Name
Write-Output "Usage: .\$scriptName -versionBump <major|minor|patch>"
Write-Output "Please provide a version bump parameter."
exit
}

# Validate user input
if ($versionBump -notin @('major', 'minor', 'patch')) {
Write-Output "Invalid input. Please enter 'major', 'minor', or 'patch'."
exit
}

###############################################################################
# Check Code
###############################################################################

# Run PSScriptAnalyzer on ps1 files
$analysisResultsPS1 = Invoke-ScriptAnalyzer -Path *.ps1 -Recurse
# Run PSScriptAnalyzer on psm1 files
$analysisResultsPSM1 = Invoke-ScriptAnalyzer -Path *.psm1 -Recurse

# Check if there are any issues found
if ($analysisResultsPS1.Count -gt 0) {
Write-Output "PS1 Script contains code style issues:"
foreach ($issue in $analysisResultsPS1) {
Write-Output "$($issue.RuleName): $($issue.Message) [Line $($issue.Line)]"
}
}

# Check if there are any issues found
if ($analysisResultsPSM1.Count -gt 0) {
Write-Output "PSM1 Script contains code style issues:"
foreach ($issue in $analysisResultsPSM1) {
Write-Output "$($issue.RuleName): $($issue.Message) [Line $($issue.Line)]"
}
}

if ($analysisResultsPS1.Count -gt 0 -or $analysisResultsPSM1.Count -gt 0) { exit 1 }

# Run Test-ModuleManifest on module manifest
$manifestTestResult = Test-ModuleManifest -Path $manifestPath

# Check if result is OK
if ($manifestTestResult) {
Write-Output "Module manifest test passed."
} else {
Write-Output "Module manifest test failed. Please check the manifest file for errors."
exit 1
}

###############################################################################
# Update and Tag with new version
###############################################################################

# Get the current version from the module manifest
$currentVersion = (Get-Content $manifestPath | Select-String -Pattern '^\s*ModuleVersion\s*=\s*\''.*\''') -replace '.*(\d+\.\d+\.\d+).*', '$1'

# Assuming $currentVersion is in the format 'major.minor.patch'
$tagVersionComponents = $currentVersion -split '\.'
$major = [int]$tagVersionComponents[0]
$minor = [int]$tagVersionComponents[1]
$patch = [int]$tagVersionComponents[2]

# Increment the appropriate component
switch ($versionBump) {
'major' { $major++; $minor = 0; $patch = 0 }
'minor' { $minor++; $patch = 0 }
'patch' { $patch++ }
}

# Create the new version string
$newVersion = "$major.$minor.$patch"

$tagExists = git tag -l $newVersion

if ($tagExists) {
Write-Output "Tag $newVersion already exists. Please check your Git history"
exit 1
}

# Update the ModuleVersion in the manifest
(Get-Content $manifestPath) -replace '^\s*ModuleVersion\s*=\s*\''.*\''', "ModuleVersion = '$newVersion'" | Set-Content $manifestPath

# Create CHANGELOG.md
docker container run -it -v ${PWD}:/app --rm yvonnick/gitmoji-changelog:latest update $newVersion

# Commit the change
git add git add --all
git commit -m ":bookmark: Bump version to $newVersion"

# Create a new Git tag
git tag -a $newVersion -m ":bookmark: Version $newVersion"

# Push changes to the remote repository
git push origin master --tags

Write-Output "ModuleVersion updated to $newVersion. Changes committed, tagged, and pushed."
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "0.0.0"
}

70 changes: 70 additions & 0 deletions terraform-tools/terraform-tools-aliases.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
. $PSScriptRoot\terraform-tools-utils.ps1

# =========================================================
# Define all lower levels aliases
# =========================================================

function tf { terraform }
function tff { tf fmt }
function tfv { tf validate }
function tfi { tf init }
function tfp { tf plan }
function tfa { tf apply }
function tfd { tf destroy }
function tfo { tf output }
function tfr { tf refresh }
function tfs { tf show }
function tfw { tf workspace }

# =========================================================
# All others aliases
# =========================================================
# Basics
function tffr { tff -recursive }
function tfip { tfi ; tfp }
function tfia { tfi ; tfa }
function tfid { tfi ; tfd }
# Warning: with auto-approve
function tfa! { tfa -auto-approve }
function tfia! { tfi ; tfa! }
function tfd! { tfd -auto-approve }
function tfid! { tfi ; tfd! }

# Utils
function tfversion { tf version }


<#
.SYNOPSIS
tf workspace select -or-create $WorkspaceName
#>
function tfws {
param (
[Parameter(Mandatory)]
[string] $WorkspaceName
)

if (-not $WorkspaceName) {
Write-Output "> Usage: tfws [workspace_name]"
}
elseif ($Argument.Count -gt 1) {
Write-Output "> Usage: tfws [workspace_name]"
}
else {
$count = (Get-ChildItem -Path $PWD -Filter "*.tf" -File | Measure-Object).Count
if ($count -le 0) {
Write-Output "> Not in terraform directory"
}
else {
tf workspace select -or-create $WorkspaceName
}
}
}

# =========================================================
# For Tools
# =========================================================
# Keep tfswitch and terraform binaries in USERPROFILE
function tfswitch {
tfswitch.exe --bin=$env:TF_TOOLS_HOME\tfswitch\terraform.exe
}
Loading

0 comments on commit 6c02fee

Please sign in to comment.