Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWoolfenden committed May 29, 2023
1 parent 1e44dfc commit b0f2a29
Show file tree
Hide file tree
Showing 164 changed files with 1,248 additions and 563 deletions.
104 changes: 100 additions & 4 deletions .idea/golinter.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion .markdownlint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"MD013": false
"MD013": false,
"MD033": {
"allowed_elements": [
"small"
]
}
}
10 changes: 4 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#file: noinspection SpellCheckingInspection,SpellCheckingInspection
---
# yamllint disable rule:line-length
default_language_version:
Expand Down Expand Up @@ -31,23 +32,20 @@ repos:
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.34.0
hooks:
- id: markdownlint
- # noinspection SpellCheckingInspection
id: markdownlint
exclude: src/testdata|testdata
- repo: https://github.com/jameswoolfenden/pre-commit
rev: v0.1.50
hooks:
- id: terraform-fmt
language_version: python3.9
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.21
rev: v0.1.22
hooks:
- id: gofmt
- id: goimports
- id: golint
# - repo: https://github.com/golangci/golangci-lint
# rev: v1.51.1
# hooks:
# - id: golangci-lint
- repo: https://github.com/syntaqx/git-hooks
rev: v0.0.17
hooks:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ azurerm_storage_account
```

or
```

```bash
$sato see -r AWS::EC2::Instance
aws_instance%
```
Expand Down
19 changes: 13 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
_ "embed" //required for embed
_ "embed" // required for embed
"fmt"
"os"
sato "sato/src"
"sato/src/arm"
"sato/src/cf"
"sato/src/see"
"sort"
"time"
Expand All @@ -15,10 +15,14 @@ import (
"github.com/urfave/cli/v2"
)

//goland:noinspection GoLinter
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

var file string

var destination string

var resource string

app := &cli.App{
Expand All @@ -29,7 +33,8 @@ func main() {
Name: "parse",
Usage: "translate CFN to Terraform",
Action: func(*cli.Context) error {
err := sato.Parse(file, destination)
err := cf.Parse(file, destination)

return err
},
Flags: []cli.Flag{
Expand All @@ -55,7 +60,8 @@ func main() {
Usage: "Outputs the application version",
UsageText: "sato version",
Action: func(*cli.Context) error {
fmt.Println(sato.Version)
fmt.Println(cf.Version)

return nil
},
},
Expand All @@ -64,6 +70,7 @@ func main() {
Usage: "translate ARM to Terraform",
Action: func(*cli.Context) error {
err := arm.Parse(file, destination)

return err
},
Flags: []cli.Flag{
Expand Down Expand Up @@ -91,6 +98,7 @@ func main() {
if result != nil {
fmt.Print(*result)
}

return err
},
Flags: []cli.Flag{
Expand All @@ -108,13 +116,12 @@ func main() {
Usage: "Translate Cloudformation to Terraform",
Compiled: time.Time{},
Authors: []*cli.Author{{Name: "James Woolfenden", Email: "jim.wolf@duck.com"}},
Version: sato.Version,
Version: cf.Version,
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))

if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("Sato failure")
}

}
62 changes: 50 additions & 12 deletions src/arm/helpers.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package arm

import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/rs/zerolog/log"
)

// IsLocal looks up whether a variable is a local or not
const typeNumber = "number"
const typeString = "string"
const typeListString = "list(string)"

// IsLocal looks up whether a variable is a local or not.
func IsLocal(target string, result map[string]interface{}) bool {
locals := result["locals"].(map[string]interface{})
locals, ok := result["locals"].(map[string]interface{})

if !ok {
return false
}

for name := range locals {
if name == target {
return true
}
}

return false
}

Expand All @@ -34,15 +45,20 @@ func fixType(myItem map[string]interface{}) (map[string]interface{}, error) {
return myItem, fmt.Errorf("object type is nil %s", myItem)
}

myType := myItem["type"].(string)
myType, ok := myItem["type"].(string)

if !ok {
return myItem, errors.New("type is not string")
}

switch myType {
case "object":
{
var types string
var result string

for name, item := range myItem["defaultValue"].(map[string]interface{}) {

//goland:noinspection GoLinter
switch item.(type) {
case []interface{}:
{
Expand Down Expand Up @@ -90,7 +106,6 @@ func fixType(myItem map[string]interface{}) (map[string]interface{}, error) {
result = temp + ",\n\t" + name + " = " + strconv.FormatBool(item.(bool))
types = types + ",\n\t" + name + " = " + "bool"
}

}
default:
{
Expand All @@ -103,43 +118,66 @@ func fixType(myItem map[string]interface{}) (map[string]interface{}, error) {
}
case "int", "float":
{
myItem["type"] = "number"
myItem["type"] = typeNumber
}
case "map[string]interface{}":
{
myItem["type"] = "string"
myItem["type"] = typeString
}
case "string", "securestring":
case typeString, "securestring":
{
myItem["default"] = escapeQuote(myItem["default"])
}
case "list(string)", "number":
case typeListString, typeNumber:
{
//do nothing
// do nothing
}
default:
{
log.Warn().Msgf("missed type %s", myType)
}
}

return myItem, nil
}

func escapeQuote(item interface{}) string {
if item != nil {
return strings.Replace(item.(string), "\"", "\\\"", -1)
return strings.ReplaceAll(item.(string), "\"", "\\\"")
}

return ""
}

func arrayToString(defaultValue []interface{}) string {
var newValue = "["
newValue := "["

for count, value := range defaultValue {
if count == len(defaultValue)-1 {
newValue = newValue + "\"" + value.(string) + "\""
} else {
newValue = newValue + "\"" + value.(string) + "\"" + ","
}
}

return newValue + "]"
}

func tags(tags map[string]interface{}) string {
tagged := "{\n"
for item, name := range tags {
tagged = tagged + "\t\"" + item + "\"" + " = " + "\"" + name.(string) + "\"\n"
}

tagged += "\t}"

return tagged
}

func notNil(unknown interface{}) bool {
if unknown == nil {
return false
}

return true
}
Loading

0 comments on commit b0f2a29

Please sign in to comment.