Last active
October 12, 2018 01:31
-
-
Save dontlaugh/aee5de3082692f497f69ecbf595fc589 to your computer and use it in GitHub Desktop.
tfdebug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env tclsh | |
variable tfcount 0 | |
variable Data [dict create] | |
variable resources [dict create] | |
variable current_file "" | |
proc analyze_file {filename} { | |
global current_file data | |
set current_file $filename | |
init_dict_for_file $filename | |
# Evaluate the tf file as Tcl! Gnarly! | |
source $filename | |
} | |
# Ensure that nested keys exist for filename. MUST be called before sourcing a | |
# Terraform file as Tcl. | |
proc init_dict_for_file {filename} { | |
global Data | |
# TODO: all keys | |
#set required_keys [list RESOURCE DATA LOCALS MODULE PROVIDER TERRAFORM] | |
set required_keys [list RESOURCE] | |
set nested [dict create] | |
foreach k $required_keys { | |
if {![dict exists $nested $k]} { | |
dict set nested $k [dict create] | |
} | |
} | |
dict set Data $filename $nested | |
} | |
# Terraform procedures: terraform, resource, provider, locals, module, data | |
proc terraform {body} { | |
global tfcount | |
incr tfcount | |
} | |
proc resource {type name body} { | |
global current_file Data | |
if {![dict exists $Data $current_file RESOURCE $type]} { | |
dict set Data $current_file RESOURCE $type [list] | |
} | |
# get the current list at nested path | |
set l [dict get $Data $current_file RESOURCE $type] | |
# add our value to the list | |
lappend l $name | |
# set the list back to the nested path | |
dict set Data $current_file RESOURCE $type $l | |
} | |
# TODO: gather data on all relevant tf keywords | |
proc provider {type body} {} | |
proc locals {body} {} | |
proc module {name body} {} | |
proc data {type name body} {} | |
proc output {name body} {} | |
# Helper to ignore dot dirs . and .. | |
proc dotdir {f} { | |
# can this be more expressiony? | |
if {[lsearch -exact {"." ".."} $f] > -1} { return 1 } else { return 0 } | |
} | |
proc display {} { | |
global Data | |
# We iterate with and additional _ to yield k v pairs, with an unneeded v. | |
foreach {filename _} [dict get $Data] { | |
puts "$filename" | |
foreach {cat _} [dict get $Data $filename] { | |
puts " $cat:" | |
foreach {type names} [dict get $Data $filename $cat] { | |
puts " $type:" | |
foreach {n} $names {puts " $n"} | |
} | |
} | |
} | |
} | |
proc Main {dir} { | |
global tfcount Data current_file resources | |
set files [list] | |
foreach f [exec ls -a $dir] { | |
if {[dotdir $f]} {continue} | |
# debug: single file | |
#if {[string compare $f "service_kubernetes.tf"] != 0} {continue} | |
if {[string match *.tf $f]} { | |
analyze_file $f | |
} | |
} | |
display | |
} | |
if $argc>0 {set dir [lindex $argv 0]} else {set dir "."} | |
Main $dir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment