Skip to content

Instantly share code, notes, and snippets.

@donaldgray
Created December 1, 2017 11:20
Show Gist options
  • Save donaldgray/49b5f269267f8f66b4d80094680087b4 to your computer and use it in GitHub Desktop.
Save donaldgray/49b5f269267f8f66b4d80094680087b4 to your computer and use it in GitHub Desktop.
Iterates through tfvars file and replaces values with corresponding values from Octopus Deploy.
var varsFile = Octopus.Parameters["tfvarsfile"];
Console.WriteLine("Parsing tfVarsFile: " + varsFile);
if (string.IsNullOrEmpty(varsFile))
throw new ApplicationException("You must provide a tf vars file location");
if (!File.Exists(varsFile))
throw new ApplicationException(string.Format("Specified tf vars file, {0}, not found", varsFile));
string tempFile = string.Concat(varsFile, "-temp");
using (var input = File.OpenText(varsFile))
using (var output = new StreamWriter(tempFile))
{
Console.WriteLine("##octopus[stdout-verbose]");
string line;
while (null != (line = input.ReadLine()))
{
var equalsIndex = line.IndexOf("=");
if (equalsIndex < 0)
{
output.WriteLine(line);
continue;
}
string variableName = line.Substring(0, equalsIndex).Trim();
Console.WriteLine(string.Format("Got variable name: {0}", variableName));
string value;
if (Octopus.Parameters.TryGetValue(variableName, out value))
{
Console.WriteLine(string.Format("Found matching value {0} for variable {1}: ", value, variableName));
var replacement = value.StartsWith("[") ? value : string.Format("\"{0}\"", value);
output.WriteLine(string.Format("{0} = {1}", variableName, replacement));
}
else
{
Console.WriteLine(string.Format("No matching value for variable: {0}", variableName));
output.WriteLine(line);
}
}
}
// Replace the original with the temp
File.Delete(varsFile);
File.Move(tempFile, varsFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment