refactor: simplify code

main
sundowndev 2021-06-17 17:59:44 +02:00
parent cc9a882fc2
commit 78601df930
6 changed files with 19 additions and 68 deletions

View File

@ -81,7 +81,7 @@
{{if (gt (len .Alerts) 0)}}
<button type="button" role="tab" aria-selected="false" aria-controls="alerts-tab" id="alerts"
tabindex="-1">
Alerts (<span data-count="resource-alerts">{{len .Alerts}}</span>)
Alerts (<span data-count="resource-alerts">0</span>)
</button>
{{end}}
</div>
@ -278,6 +278,8 @@
}
refreshCounters();
}
resetResources()
</script>
<script>
// Enhance accessibility

View File

@ -7,10 +7,12 @@ import (
"html/template"
"math"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/cloudskiff/driftctl/pkg/alerter"
"github.com/cloudskiff/driftctl/pkg/analyser"
"github.com/cloudskiff/driftctl/pkg/resource"
@ -83,12 +85,6 @@ func (c *HTML) Write(analysis *analyser.Analysis) error {
}
return math.Round(100 * float64(count) / float64(analysis.Summary().TotalResources))
},
//"prettify": func(res interface{}) string {
// return prettify(res)
//},
//"prettifyPaths": func(paths []string) template.HTML {
// return template.HTML(prettifyPaths(paths))
//},
"jsonDiff": func(ch analyser.Changelog) template.HTML {
var buf bytes.Buffer
@ -114,7 +110,7 @@ func (c *HTML) Write(analysis *analyser.Analysis) error {
_, _ = fmt.Fprintf(&buf, "%s%s<br>%s%s<br>", whiteSpace, prefix, whiteSpace, jsonDiff(change.From, change.To, whiteSpace))
continue
}
_, _ = fmt.Fprintf(&buf, "%s%s <span class=\"code-box-line-delete\">%s</span> => <span class=\"code-box-line-create\">%s</span>", whiteSpace, prefix, prettify(change.From), prettify(change.To))
_, _ = fmt.Fprintf(&buf, "%s%s <span class=\"code-box-line-delete\">%s</span> => <span class=\"code-box-line-create\">%s</span>", whiteSpace, prefix, htmlPrettify(change.From), htmlPrettify(change.To))
}
if change.Computed {
@ -171,28 +167,10 @@ func distinctResourceTypes(resources []resource.Resource) []string {
return types
}
func prettifyPaths(paths []string) string {
content := ""
for i, v := range paths {
var isArrayKey bool
// If the previous path is an integer, it means the current path is part of an array
if j := i - 1; j >= 0 && len(paths) >= j {
_, err := strconv.Atoi(paths[j])
isArrayKey = err == nil
}
if i > 0 && !isArrayKey {
content += "<br>"
content += strings.Repeat("&emsp;", i)
}
if _, err := strconv.Atoi(v); err == nil {
content += "- "
} else {
content += fmt.Sprintf("%s:", v)
}
func htmlPrettify(resource interface{}) string {
res := reflect.ValueOf(resource)
if resource == nil || res.Kind() == reflect.Ptr && res.IsNil() {
return "&lt;null&gt;"
}
return content
return awsutil.Prettify(resource)
}

View File

@ -232,38 +232,3 @@ func TestHTML_DistinctResourceTypes(t *testing.T) {
})
}
}
func TestHTML_PrettifyPaths(t *testing.T) {
tests := []struct {
name string
paths []string
expected string
}{
{
name: "test simple object",
paths: []string{"path", "to", "field"},
expected: "path:<br>&emsp;to:<br>&emsp;&emsp;field:",
},
{
name: "test object with array of string",
paths: []string{"properties", "tags", "0"},
expected: "properties:<br>&emsp;tags:<br>&emsp;&emsp;- ",
},
{
name: "test object with array of objects",
paths: []string{"path", "to", "0", "field"},
expected: "path:<br>&emsp;to:<br>&emsp;&emsp;- field:",
},
{
name: "test with simple array",
paths: []string{"0", "field"},
expected: "- field:",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := prettifyPaths(tt.paths)
assert.Equal(t, tt.expected, got)
})
}
}

View File

@ -412,7 +412,7 @@ input[type="search"], select {
<button type="button" role="tab" aria-selected="false" aria-controls="alerts-tab" id="alerts"
tabindex="-1">
Alerts (<span data-count="resource-alerts">1</span>)
Alerts (<span data-count="resource-alerts">0</span>)
</button>
</div>
@ -493,7 +493,7 @@ input[type="search"], select {
}</span> => <span class="code-box-line-create">{
Id: "093cd6ba-cf6d-4800-b252-6a50ca8903cd",
Type: "aws_iam_policy"
}</span><br>&emsp;+ Tags.[0].Name: <span class="code-box-line-create">"test"</span><br>&emsp;~ InstanceInitiatedShutdownBehavior: <span class="code-box-line-delete">""</span> => <span class="code-box-line-create"><null></span><br></code>
}</span><br>&emsp;+ Tags.[0].Name: <span class="code-box-line-create">"test"</span><br>&emsp;~ InstanceInitiatedShutdownBehavior: <span class="code-box-line-delete">""</span> => <span class="code-box-line-create">&lt;null&gt;</span><br></code>
</pre>
</div>
@ -676,6 +676,8 @@ input[type="search"], select {
}
refreshCounters();
}
resetResources()
</script>
<script>

View File

@ -493,6 +493,8 @@ input[type="search"], select {
}
refreshCounters();
}
resetResources()
</script>
<script>

View File

@ -469,6 +469,8 @@ input[type="search"], select {
}
refreshCounters();
}
resetResources()
</script>
<script>