Skip to content

Commit

Permalink
feat: initial commit, includes init commands and module command
Browse files Browse the repository at this point in the history
  • Loading branch information
fitz7 committed Dec 10, 2023
0 parents commit c45f07b
Show file tree
Hide file tree
Showing 19 changed files with 3,143 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CI
on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: go-with-cache
uses: magnetikonline/action-golang-cache@v4
with:
go-version-file: go.mod
- run: make install-tools
- run: make lint

test:
name: Test
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- name: go-with-cache
uses: magnetikonline/action-golang-cache@v4
with:
go-version-file: go.mod
- run: make install-tools
- run: make test

release:
name: Release
runs-on: ubuntu-latest
needs: test
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: go-with-cache
uses: magnetikonline/action-golang-cache@v4
with:
go-version-file: go.mod
- run: make install-tools
- uses: go-semantic-release/action@v1
with:
bin: ./bin/semantic-release
hooks: goreleaser
allow-initial-development-versions: true
args: --allow-initial-development-versions --allow-no-changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# local binaries
bin/*

# idea things
.idea/

# ci tools
.semrel/*
24 changes: 24 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
linters:
enable:
- errorlint
- forbidigo
- gocritic
- goconst
- gocyclo
- gofumpt
- goimports
- misspell
- revive
- unconvert
- unparam
- wastedassign

linters-settings:
gocyclo:
min-complexity: 12
gofumpt:
extra-rules: true
govet:
enable-all: true
disable:
- fieldalignment
21 changes: 21 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
builds:
- env:
- CGO_ENABLED=0
targets:
- linux_amd64
- linux_arm64
- darwin_amd64
- darwin_arm64
- linux_arm
- windows_amd64
main: ./
ldflags:
- -extldflags '-static'
- -s -w -X 'github.com/fitz7/tfnew/cmd.Version={{.Version}}'

archives:
- format: binary
name_template: '{{ .Binary }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}'

checksum:
name_template: '{{ .ProjectName }}_v{{ .Version }}_checksums.txt'
23 changes: 23 additions & 0 deletions .semrelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"plugins": {
"commit-analyzer": {
"name": "github:faceit/commit-analyzer-regex",
"options": {
"minor": "feat",
"patch": "build|ci|fix|perf|refactor|test|chore"
}
},
"changelog-generator": {
"name": "default",
"options": {
"emojis": "true"
}
},
"provider": {
"name": "github",
"options": {
"slug": "fitz7/tfnew"
}
}
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Andrew Fitzpatrick

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export GOBIN := $(PWD)/bin
export PATH := $(GOBIN):$(PATH)

update-dependencies:
go get -u ./...
go mod tidy

install-tools:
@echo Installing tools from tools.go
@cat tools/tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % go install %

lint:
golangci-lint run ./... --timeout 300s

test:
gotestsum ./...

semantic-release:
semantic-release
25 changes: 25 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Version: rootCmd.Version,
Short: "creates a config file in the repository",
RunE: func(cmd *cobra.Command, args []string) error {
err := viper.SafeWriteConfig()
if err != nil {
return err
}

return nil
},
}

func init() {
rootCmd.AddCommand(initCmd)
}
67 changes: 67 additions & 0 deletions cmd/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cmd

import (
"errors"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/fitz7/tfnew/internal/tfmodule"
)

// moduleCmd represents the module command
var moduleCmd = &cobra.Command{
Use: "module",
Version: rootCmd.Version,
Short: "Creates a new terraform module",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("requires a single arg containing the path of the new module")
}
return validatePath(args[0])
},
RunE: func(cmd *cobra.Command, args []string) error {
requiredProviders, err := cmd.Flags().GetStringSlice("required_providers")
if err != nil {
return err
}

root, err := cmd.Flags().GetBool("root")
if err != nil {
return err
}

backend := viper.GetString("backend.type")

createModuleOptions := tfmodule.CreateModuleOptions{
Name: args[0],
RootModule: root,
RequiredProviders: requiredProviders,
BackendType: backend,
}

err = tfmodule.CreateModule(createModuleOptions)
if err != nil {
return err
}
return nil
},
}

func validatePath(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}

return fmt.Errorf("new module directory already exists: %s", path)
}

func init() {
rootCmd.AddCommand(moduleCmd)

moduleCmd.Flags().StringSliceP("required_providers", "p", []string{}, "")

moduleCmd.Flags().BoolP("root", "r", false, "")
}
25 changes: 25 additions & 0 deletions cmd/reinit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// initCmd represents the init command
var reinitCmd = &cobra.Command{
Use: "reinit",
Version: rootCmd.Version,
Short: "reinitialise your config file to the defaults",
RunE: func(cmd *cobra.Command, args []string) error {
err := viper.WriteConfig()
if err != nil {
return err
}

return nil
},
}

func init() {
rootCmd.AddCommand(reinitCmd)
}
Loading

0 comments on commit c45f07b

Please sign in to comment.