Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ready for spectators! #1

Merged
merged 7 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.17.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
50 changes: 50 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Contributing to tfrefactor
We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:

- Reporting a bug
- Discussing the current state of the code
- Submitting a fix
- Proposing new features
- Becoming a maintainer

## We Develop with Github
We use github to host code, to track issues and feature requests, as well as accept pull requests.

## Pull Requests
Pull requests are the best way to propose changes to the codebase (we use [Github Flow](https://guides.github.com/introduction/flow/index.html)). We actively welcome your pull requests:

1. Fork the repo and create your branch from `main`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. Issue that pull request!

## Any contributions you make will be under the MIT Software License
In short, when you submit code changes, your submissions are understood to be under the same [MPL 2.0 License](https://www.mozilla.org/en-US/MPL/2.0) that covers the project. Feel free to contact the maintainers if that's a concern.

## Report bugs using Github's [issues](https://github.com/craftvscruft/tfrefactor/issues)
We use GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/craftvscruft/tfrefactor/issues); it's that easy!

**Great Bug Reports** tend to have:

- A quick summary and/or background
- Steps to reproduce
- Be specific!
- Give sample code if you can.
- What you expected would happen
- What actually happens
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

People *love* thorough bug reports. I'm not even kidding.

## Use a Consistent Coding Style

* Format with `go fmt`
* Lint with `go vet`

## License
By contributing, you agree that your contributions will be licensed under its MPL2 License.

## References
This document was adapted from the open-source contribution guidelines for [Facebook's Draft](https://github.com/facebook/draft-js/blob/a9316a723f9e918afde44dea68b5f9f39b7d9b00/CONTRIBUTING.md)
72 changes: 66 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
# TF Refactor
Automated refactoring for Terraform.
# Welcome to tfrefactor 👋
![Version](https://img.shields.io/badge/version-0.0.1-blue.svg?cacheSeconds=2592000)
[![Documentation](https://img.shields.io/badge/documentation-yes-brightgreen.svg)](https://refactor.tf)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/kefranabg/readme-md-generator/graphs/commit-activity)
[![License: MPL2](https://img.shields.io/github/license/raymyers/tfrefactor)](https://github.com/craftvscruft/tfrefactor/blob/main/LICENSE)
[![Twitter: lambdapocalypse](https://img.shields.io/twitter/follow/lambdapocalypse.svg?style=social)](https://twitter.com/lambdapocalypse)

Coming soon (hopefully)!
> Automated refactoring for Terraform.

## Credits
Inspiration and test helper code from [hcledit](https://github.com/minamijoyo/hcledit) by
Masayuki Morita.
### 🏠 [Homepage](https://github.com/craftvscruft/tfrefactor)

## Install

Substitute ~/.local/bin for a prefered dir the is in your $PATH.

```sh
git clone [email protected]:craftvscruft/tfrefactor.git
cd tfrefactor
go build

cp bin/tfrefactor ~/.local/bin
```

## Usage

```sh
tfrefactor
```

## Run tests

```sh
make test
```

## Author

👤 **Ray Myers**

* Website: https://www.youtube.com/channel/UC4nEbAo5xFsOZDk2v0RIGHA
* Twitter: [@lambdapocalypse](https://twitter.com/lambdapocalypse)
* GitHub: [@raymyers](https://github.com/raymyers)
* LinkedIn: [@cadrlife](https://linkedin.com/in/cadrlife)

## 🤝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check [issues page](https://github.com/craftvscruft/tfrefactor/issues). You can also take a look at the [contributing guide](https://github.com/craftvscruft/tfrefactor/blob/main/CONTRIBUTING.md).

## Show your support

Give a ⭐️ if this project helped you!

[![support us](https://img.shields.io/badge/become-a%20patreon%20us-orange.svg?cacheSeconds=2592000)](https://www.patreon.com/craftvscruft)

## Acknowledgement

Inspiration and test helper code from [hcledit](https://github.com/minamijoyo/hcledit) by Masayuki Morita.

## 📝 License

Copyright © 2022 [Ray Myers](https://github.com/raymyers).

This project is [MPL2](https://github.com/craftvscruft/tfrefactor/blob/main/LICENSE) licensed.

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_
55 changes: 54 additions & 1 deletion cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"

Expand All @@ -28,6 +29,7 @@ Arguments:
}
flags := cmd.Flags()
flags.StringP("config", "c", "-", "Path of terraform to modify, defaults to current.")
flags.BoolP("force", "f", false, "Skip interactive approval of update before applying")

return cmd
}
Expand All @@ -48,12 +50,63 @@ func runRenameCmd(cmd *cobra.Command, args []string) error {
toAddress := args[1]
configPath, err := cmd.Flags().GetString("config")
CheckFatal(err)

if configPath == "-" {
configPath, err = os.Getwd()
CheckFatal(err)
}

CheckFatal(err)
_, err = fmt.Fprintf(cmd.OutOrStdout(), "Renaming '%v' -> '%v' in %v\n", fromAddress, toAddress, configPath)
return refactor.Rename(fromAddress, toAddress, configPath)
if err != nil {
return err
}
plan, err := refactor.Rename(fromAddress, toAddress, configPath)
if err != nil {
return err
}
err = approveAndApplyUpdate(cmd, plan)
return err
}

func approveAndApplyUpdate(cmd *cobra.Command, plan *refactor.UpdatePlan) error {
autoApprove, err := cmd.Flags().GetBool("force")
CheckFatal(err)
if len(plan.FileUpdates) > 0 {
if autoApprove {
err = applyUpdate(plan)
CheckFatal(err)
_, err = fmt.Fprintln(cmd.OutOrStdout(), "Done.")
CheckFatal(err)
} else {
_, err = fmt.Fprintf(cmd.OutOrStdout(), "Update %v file(s)? [y/N]: ", len(plan.FileUpdates))
CheckFatal(err)
var in string
_, _ = fmt.Fscanln(cmd.InOrStdin(), &in)
// Ignore Fscanln err because empty input is OK.
if in == "Y" || in == "y" {
err = applyUpdate(plan)
CheckFatal(err)
_, err = fmt.Fprintln(cmd.OutOrStdout(), "Done.")
CheckFatal(err)
} else {
_, err = fmt.Fprintf(cmd.OutOrStdout(), "\nAborted.\n")
CheckFatal(err)
}
}
} else {
_, err = fmt.Fprintf(cmd.OutOrStdout(), "\nNo updates required.\n")
CheckFatal(err)
}
return nil
}

func applyUpdate(plan *refactor.UpdatePlan) error {
for _, update := range plan.FileUpdates {
err := ioutil.WriteFile(update.Filename, []byte(update.AfterText), 0644)
if err != nil {
return err
}
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/craftvscruft/tfrefactor
go 1.17

require (
github.com/hashicorp/hcl/v2 v2.11.1
github.com/pmezard/go-difflib v1.0.0
github.com/raymyers/hcl/v2 v2.11.1-set-attr-name-fork
github.com/spf13/cobra v1.3.0
)

Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc=
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.1/go.mod h1:4gW7WsVCke5TE7EPeYliwHlRUyBtfCwuFwuMg2DmyNY=
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
Expand Down Expand Up @@ -312,6 +309,8 @@ github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8b
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/raymyers/hcl/v2 v2.11.1-set-attr-name-fork h1:k4kbA/GigFGpzqNy9DjQiXIco/Amth1UBzJV9USBql0=
github.com/raymyers/hcl/v2 v2.11.1-set-attr-name-fork/go.mod h1:XDvt8jUh7HjUiJVkGKpmg+SfETbW/rdMb8HPZNlsqck=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
46 changes: 23 additions & 23 deletions refactor/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package refactor

import "strings"

type ElementType string
type TypeName string

const (
Resource ElementType = "resource"
Output = "output"
Var = "var"
Local = "local"
Data = "data"
Module = "module"
TypeResource TypeName = "resource"
TypeOutput = "output"
TypeVar = "var"
TypeLocal = "local"
TypeData = "data"
TypeModule = "module"
)

type Address struct {
elementType ElementType
elementType TypeName
labels []string
}

func (a *Address) RefNameArray() []string {
if a.elementType == Resource {
if a.elementType == TypeResource {
return a.labels
}
return append([]string{string(a.elementType)}, a.labels...)
Expand All @@ -30,7 +30,7 @@ func (a *Address) RefName() string {
}

func (a *Address) BlockType() string {
if a.elementType == Var {
if a.elementType == TypeVar {
return "variable"
}
return string(a.elementType)
Expand All @@ -39,39 +39,39 @@ func (a *Address) BlockType() string {
func ParseAddress(addr string) *Address {
parts := strings.Split(addr, ".")
switch parts[0] {
case string(Resource), "resources":
case string(TypeResource), "resources":
return &Address{
elementType: Resource,
elementType: TypeResource,
labels: parts[1:],
}
case string(Output), "outputs":
case string(TypeOutput), "outputs":
return &Address{
elementType: Output,
elementType: TypeOutput,
labels: parts[1:],
}
case string(Var), "vars", "variable", "variables":
case string(TypeVar), "vars", "variable", "variables":
return &Address{
elementType: Var,
elementType: TypeVar,
labels: parts[1:],
}
case string(Local), "locals":
case string(TypeLocal), "locals":
return &Address{
elementType: Local,
elementType: TypeLocal,
labels: parts[1:],
}
case string(Data):
case string(TypeData):
return &Address{
elementType: Data,
elementType: TypeData,
labels: parts[1:],
}
case string(Module):
case string(TypeModule):
return &Address{
elementType: Module,
elementType: TypeModule,
labels: parts[1:],
}
default:
return &Address{
elementType: Resource,
elementType: TypeResource,
labels: parts,
}
}
Expand Down
4 changes: 2 additions & 2 deletions refactor/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"os"
"runtime/debug"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/raymyers/hcl/v2"
"github.com/raymyers/hcl/v2/hclwrite"
)

func ParseHclFile(filename string) (*hclwrite.File, error) {
Expand Down
Loading