refactor: simplify code
parent
cc9a882fc2
commit
78601df930
|
@ -81,7 +81,7 @@
|
||||||
{{if (gt (len .Alerts) 0)}}
|
{{if (gt (len .Alerts) 0)}}
|
||||||
<button type="button" role="tab" aria-selected="false" aria-controls="alerts-tab" id="alerts"
|
<button type="button" role="tab" aria-selected="false" aria-controls="alerts-tab" id="alerts"
|
||||||
tabindex="-1">
|
tabindex="-1">
|
||||||
Alerts (<span data-count="resource-alerts">{{len .Alerts}}</span>)
|
Alerts (<span data-count="resource-alerts">0</span>)
|
||||||
</button>
|
</button>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
@ -278,6 +278,8 @@
|
||||||
}
|
}
|
||||||
refreshCounters();
|
refreshCounters();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetResources()
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
// Enhance accessibility
|
// Enhance accessibility
|
||||||
|
|
|
@ -7,10 +7,12 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws/awsutil"
|
||||||
"github.com/cloudskiff/driftctl/pkg/alerter"
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
||||||
"github.com/cloudskiff/driftctl/pkg/analyser"
|
"github.com/cloudskiff/driftctl/pkg/analyser"
|
||||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
"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))
|
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 {
|
"jsonDiff": func(ch analyser.Changelog) template.HTML {
|
||||||
var buf bytes.Buffer
|
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))
|
_, _ = fmt.Fprintf(&buf, "%s%s<br>%s%s<br>", whiteSpace, prefix, whiteSpace, jsonDiff(change.From, change.To, whiteSpace))
|
||||||
continue
|
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 {
|
if change.Computed {
|
||||||
|
@ -171,28 +167,10 @@ func distinctResourceTypes(resources []resource.Resource) []string {
|
||||||
return types
|
return types
|
||||||
}
|
}
|
||||||
|
|
||||||
func prettifyPaths(paths []string) string {
|
func htmlPrettify(resource interface{}) string {
|
||||||
content := ""
|
res := reflect.ValueOf(resource)
|
||||||
for i, v := range paths {
|
if resource == nil || res.Kind() == reflect.Ptr && res.IsNil() {
|
||||||
var isArrayKey bool
|
return "<null>"
|
||||||
|
|
||||||
// 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(" ", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := strconv.Atoi(v); err == nil {
|
|
||||||
content += "- "
|
|
||||||
} else {
|
|
||||||
content += fmt.Sprintf("%s:", v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return awsutil.Prettify(resource)
|
||||||
return content
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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> to:<br>  field:",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "test object with array of string",
|
|
||||||
paths: []string{"properties", "tags", "0"},
|
|
||||||
expected: "properties:<br> tags:<br>  - ",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "test object with array of objects",
|
|
||||||
paths: []string{"path", "to", "0", "field"},
|
|
||||||
expected: "path:<br> to:<br>  - 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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -412,7 +412,7 @@ input[type="search"], select {
|
||||||
|
|
||||||
<button type="button" role="tab" aria-selected="false" aria-controls="alerts-tab" id="alerts"
|
<button type="button" role="tab" aria-selected="false" aria-controls="alerts-tab" id="alerts"
|
||||||
tabindex="-1">
|
tabindex="-1">
|
||||||
Alerts (<span data-count="resource-alerts">1</span>)
|
Alerts (<span data-count="resource-alerts">0</span>)
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -493,7 +493,7 @@ input[type="search"], select {
|
||||||
}</span> => <span class="code-box-line-create">{
|
}</span> => <span class="code-box-line-create">{
|
||||||
Id: "093cd6ba-cf6d-4800-b252-6a50ca8903cd",
|
Id: "093cd6ba-cf6d-4800-b252-6a50ca8903cd",
|
||||||
Type: "aws_iam_policy"
|
Type: "aws_iam_policy"
|
||||||
}</span><br> + Tags.[0].Name: <span class="code-box-line-create">"test"</span><br> ~ InstanceInitiatedShutdownBehavior: <span class="code-box-line-delete">""</span> => <span class="code-box-line-create"><null></span><br></code>
|
}</span><br> + Tags.[0].Name: <span class="code-box-line-create">"test"</span><br> ~ InstanceInitiatedShutdownBehavior: <span class="code-box-line-delete">""</span> => <span class="code-box-line-create"><null></span><br></code>
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -676,6 +676,8 @@ input[type="search"], select {
|
||||||
}
|
}
|
||||||
refreshCounters();
|
refreshCounters();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetResources()
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
|
|
@ -493,6 +493,8 @@ input[type="search"], select {
|
||||||
}
|
}
|
||||||
refreshCounters();
|
refreshCounters();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetResources()
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
|
|
@ -469,6 +469,8 @@ input[type="search"], select {
|
||||||
}
|
}
|
||||||
refreshCounters();
|
refreshCounters();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resetResources()
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue