Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenaldinger committed Dec 15, 2019
1 parent 6cffcb1 commit 06d01a7
Show file tree
Hide file tree
Showing 29 changed files with 429 additions and 184 deletions.
149 changes: 5 additions & 144 deletions cmd/decker/main.go
Original file line number Diff line number Diff line change
@@ -1,154 +1,15 @@
package main

import (
"encoding/json"
"fmt"
"github.com/zclconf/go-cty/cty"
"os"

"github.com/stevenaldinger/decker/internal/pkg/dependencies"
"github.com/stevenaldinger/decker/internal/pkg/hcl"
"github.com/stevenaldinger/decker/internal/pkg/paths"
"github.com/stevenaldinger/decker/internal/pkg/reports"
"github.com/stevenaldinger/decker/pkg/gocty"
"github.com/stevenaldinger/decker/pkg/plugins"
"github.com/stevenaldinger/decker/pkg/decker"
)

// loop over list of strings and return true if list contains a given string
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func main() {
// get the path to a given config file passed in via CLI
hclConfigFile := paths.GetConfigFilePath()

blocks := hcl.GetBlocksFromConfig(hclConfigFile)

dependencies.ValidateConfig(blocks)

varBlockNames := dependencies.GetVariableNames(blocks)
resBlocksSorted := dependencies.Sort(blocks)

// get environment variable values for each declared variable block
var envVarCtx = hcl.GetHCLEvalContextVarsFromEnv(varBlockNames)

// map to keep track of all the values returned from plugins
resultsMapCty := map[string]*map[string]cty.Value{}
resultsMapCtyNested := map[string]*map[string]*map[string]cty.Value{}

decoder := gocty.Decoder{}
encoder := gocty.Encoder{}

for index, block := range resBlocksSorted {
resourcePlugin, resourceName := block.Labels[0], block.Labels[1]

pluginHCLPath := paths.GetPluginHCLFilePath(resourcePlugin)

pluginAttrs := hcl.GetPluginAttributes(block)

containsForEach := contains(pluginAttrs, "for_each")

hclConfig, pluginContent := hcl.GetPluginContent(containsForEach, block, pluginHCLPath)

if containsForEach {
// returns JSON, not sure why
forEachDecoded := hcl.DecodeHCLListAttribute(pluginContent.Attributes["for_each"], envVarCtx, &resultsMapCty, &resultsMapCtyNested)

var forEachList []string

jsonUnmarshalErr := json.Unmarshal([]byte(forEachDecoded), &forEachList)

if jsonUnmarshalErr != nil {
fmt.Println("Error unmarshaling json", jsonUnmarshalErr)
os.Exit(1)
}

for _, eachKey := range forEachList {
var forEachMap = &map[string]cty.Value{
"key": encoder.StringVal(string(eachKey)),
}
app := decker.Init()

resultsMapCty["each"] = forEachMap

inputsMap := hcl.CreateInputsMapCty(hclConfig.Inputs, pluginContent.Attributes, envVarCtx, &resultsMapCty, &resultsMapCtyNested)

// declare a new empty map to be passed into the plugin
var resultsMap = map[string]cty.Value{}
var resultsListMap = map[string][]cty.Value{}

pluginEnabled := plugins.RunIfEnabled(resourcePlugin, &inputsMap, &resultsMap, &resultsListMap)

if pluginEnabled {
fmt.Println(fmt.Sprintf("DECKER: Ran plugin %d[%s] of %d: %s (%s)", index+1, string(eachKey), len(resBlocksSorted), resourcePlugin, resourceName))
} else {
fmt.Println(fmt.Sprintf("DECKER: [Disabled] Did not run plugin %d of %d: %s (%s)", index+1, len(resBlocksSorted), resourcePlugin, resourceName))

resultsMap["raw_output"] = encoder.StringVal("")
}

// initialize if map doesn't exist yet
if _, ok := resultsMapCtyNested[resourceName]; !ok {
var initMap = &map[string]*map[string]cty.Value{}
// var initMap = &map[string]cty.Value {}
resultsMapCtyNested[resourceName] = initMap
}

// build eval context from plugin results and add it to the ongoing map
(*resultsMapCtyNested[resourceName])[string(eachKey)] = &resultsMap

if pluginEnabled {
reports.WriteStringToFile(paths.GetReportFilePath(resourceName+"["+string(eachKey)+"]", "txt"), decoder.GetString(resultsMap["raw_output"]))
}
}
} else {
inputsMap := hcl.CreateInputsMapCty(hclConfig.Inputs, pluginContent.Attributes, envVarCtx, &resultsMapCty, &resultsMapCtyNested)

// declare a new empty map to be passed into the plugin
var resultsMap = map[string]cty.Value{}
var resultsListMap = map[string][]cty.Value{}

pluginEnabled := plugins.RunIfEnabled(resourcePlugin, &inputsMap, &resultsMap, &resultsListMap)

if pluginEnabled {
fmt.Println(fmt.Sprintf("DECKER: Ran plugin %d of %d: %s (%s)", index+1, len(resBlocksSorted), resourcePlugin, resourceName))

// build eval context from plugin results and add it to the ongoing map
resultsMapCty[resourceName] = &resultsMap

reports.WriteStringToFile(paths.GetReportFilePath(resourceName, "txt"), decoder.GetString(resultsMap["raw_output"]))
} else {
fmt.Println(fmt.Sprintf("DECKER: [Disabled] Did not run plugin %d of %d: %s (%s)", index+1, len(resBlocksSorted), resourcePlugin, resourceName))

resultsMap["raw_output"] = encoder.StringVal("")

// build eval context from plugin results and add it to the ongoing map
resultsMapCty[resourceName] = &resultsMap
}
}
}
// run plugins
app.RunPlugins()

// run outputs plugin
outputsResults := plugins.RunOutputsPlugin(&resultsMapCty)

if os.Getenv("DECKER_OUTPUTS_XML") == "true" {
reports.WriteStringToFile(paths.GetReportFilePath("All", "xml"), outputsResults.AllXML)

for uniqueName, outputVal := range outputsResults.Results {
reports.WriteStringToFile(paths.GetReportFilePath(uniqueName, "xml"), outputVal.XML)
}
}

if os.Getenv("DECKER_OUTPUTS_JSON") == "true" {
reports.WriteStringToFile(paths.GetReportFilePath("All", "json"), outputsResults.AllJSON)

for uniqueName, outputVal := range outputsResults.Results {
reports.WriteStringToFile(paths.GetReportFilePath(uniqueName, "json"), outputVal.JSON)
}
}
app.GetResults()
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/stevenaldinger/decker

go 1.13

require (
github.com/agext/levenshtein v1.2.1
github.com/apparentlymart/go-textseg v1.0.0
github.com/clbanning/mxj v1.8.3
github.com/google/go-cmp v0.2.0
github.com/hashicorp/hcl2 v0.0.0-20181111172936-0467c0c38ca2
github.com/mitchellh/go-wordwrap v1.0.0
github.com/sirupsen/logrus v1.4.2
github.com/t94j0/array v0.0.0-20180426153242-68930562a6bd
github.com/t94j0/nmap v0.0.0-20180329153021-17cccc8c9739
github.com/zclconf/go-cty v0.0.0-20181017232614-01c5aba823a6
golang.org/x/text v0.3.0
gonum.org/v1/gonum v0.0.0-20181124210156-b3c4e4046788
)
58 changes: 58 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
github.com/DHowett/go-plist v0.0.0-20180609054337-500bd5b9081b/go.mod h1:5paT5ZDrOm8eAJPem2Bd+q3FTi3Gxm/U4tb2tH8YIUQ=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2mtRGSCPsat1kze3CUtvJN3/jTXlp29k=
github.com/clbanning/mxj v1.8.3 h1:2r/KCJi52w2MRz+K+UMa/1d7DdCjnLqYJfnbr7dYNWI=
github.com/clbanning/mxj v1.8.3/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/hashicorp/errwrap v0.0.0-20180715044906-d6c0cd880357/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v0.0.0-20180717150148-3d5d8f294aa0/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I=
github.com/hashicorp/hcl2 v0.0.0-20181111172936-0467c0c38ca2 h1:uNBHbPmzVQJHlOPwdj/2Ssi9WMXBYaPfaq5pylaJJuQ=
github.com/hashicorp/hcl2 v0.0.0-20181111172936-0467c0c38ca2/go.mod h1:4nBvwJRETsbpa0LQ7FbXXVFmo0Crvhya1Dmpbm7cVow=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/t94j0/array v0.0.0-20180426153242-68930562a6bd h1:RXtQ+gpgNU68cTZDcDTiP3OvInFVPVV1H/KJDd4B6VE=
github.com/t94j0/array v0.0.0-20180426153242-68930562a6bd/go.mod h1:SApqaBuVRHQRjvoIbN++3aFXJjnWr0UkZM72QpvdFZA=
github.com/t94j0/nmap v0.0.0-20180329153021-17cccc8c9739 h1:X3gxkZ908d0M5nQmYFpX41hRJt0nnD9s5bG1sR7j7Lk=
github.com/t94j0/nmap v0.0.0-20180329153021-17cccc8c9739/go.mod h1:ChQqulPXGzYchkOwWqN0ZqGFs+K4UlQJfSVVokOmUpo=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/zclconf/go-cty v0.0.0-20180815031001-58bb2bc0302a/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v0.0.0-20181017232614-01c5aba823a6 h1:Y9SzKuDy2J5QLFPmFk7/ZIzbu4/FrQK9Zwv4vjivNiM=
github.com/zclconf/go-cty v0.0.0-20181017232614-01c5aba823a6/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
golang.org/x/crypto v0.0.0-20180816225734-aabede6cba87/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gonum.org/v1/gonum v0.0.0-20181124210156-b3c4e4046788 h1:6B8ijSEkkZPGYNtseQTtzS4X3apTG4CVgA7Cn9gXxtI=
gonum.org/v1/gonum v0.0.0-20181124210156-b3c4e4046788/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
howett.net/plist v0.0.0-20180609054337-500bd5b9081b/go.mod h1:jInWmjR7JRkkon4jlLXDZGVEeY/wo3kOOJEWYhNE+9Y=
40 changes: 40 additions & 0 deletions pkg/decker/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package decker

import (
"github.com/zclconf/go-cty/cty"

"github.com/stevenaldinger/decker/pkg/gocty"
"github.com/stevenaldinger/decker/pkg/plugins"

hashicorpHCL "github.com/hashicorp/hcl2/hcl"
)

// Status is the resulting latency on a specific queue.
type Status struct {
Queue string
Latency float64
}

type Env struct {
Outputs Outputs
}

type Outputs struct {
XML bool
JSON bool
}

// App is the main object for decker
type App struct {
// map to keep track of all the values returned from plugins
ResultsMap map[string]*map[string]cty.Value
ResultsMapNested map[string]*map[string]*map[string]cty.Value
Channel chan Status
Decoder gocty.Decoder
Encoder gocty.Encoder
Environment *map[string]cty.Value
Env Env
Blocks []*hashicorpHCL.Block
OutputsResults plugins.OutputsResults
ActiveBlock Block
}
36 changes: 36 additions & 0 deletions pkg/decker/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package decker

import (
"github.com/stevenaldinger/decker/pkg/hcl"
"github.com/stevenaldinger/decker/pkg/paths"

hashicorpHCL "github.com/hashicorp/hcl2/hcl"
)

type Block struct {
ResourceName string
PluginName string
PluginConfigPath string
ForEach bool
HCLConfig *hcl.PluginConfig
PluginContent *hashicorpHCL.BodyContent
Plugins []Plugin
}

// NewBlock initializes a block.
func NewBlock(block *hashicorpHCL.Block) Block {
b := Block{
PluginName: block.Labels[0],
ResourceName: block.Labels[1],
}

b.PluginConfigPath = paths.GetPluginHCLFilePath(b.PluginName)

pluginAttrs := hcl.GetPluginAttributes(block)

b.ForEach = contains(pluginAttrs, "for_each")

b.HCLConfig, b.PluginContent = hcl.GetPluginContent(b.ForEach, block, b.PluginConfigPath)

return b
}
Loading

0 comments on commit 06d01a7

Please sign in to comment.