Im curious if there is any way to shorten this code, or make it in one line / statement,
var terraformProcess = new Process();
terraformProcess.StartInfo.FileName = "terraform";
terraformProcess.StartInfo.UseShellExecute = false;
terraformProcess.StartInfo.WorkingDirectory = Path.Combine(repoPath,"terraform");
Console.WriteLine(terraformProcess.StartInfo.WorkingDirectory);
terraformProcess.StartInfo.RedirectStandardOutput = true;
terraformProcess.StartInfo.Arguments = "init";
terraformProcess.Start();
terraformProcess.WaitForExit();
terraformProcess.StartInfo.Arguments =
$"destroy --var-file=current.tfvars -auto-approve -var vcd_user={_terraformConf.vdcUsername} -var vcd_pass={CryptoServices.Decode(_terraformConf.encryptedVdcPass)} -no-color";
terraformProcess.Start();
var output = terraformProcess.StandardOutput.ReadToEnd();
terraformProcess.WaitForExit();
Ive tried:
var startInfo = new StartInfo {
FileName = "terraform",
WorkingDirectory = Path.Combine(repoPath,"terraform");
RedirectStandardOutput = true;
Arguments = "init";
};
then assigning it to Process.StartInfo, but it looks like there is only getter on Process.StartInfo.
Im just curious if there is a shorter way of doing that, without repeating so many lines.
CodePudding user response:
You can use object initializer syntax on multiple levels, i.e
var terraformProcess = new Process
{
StartInfo =
{
FileName = "terraform",
UseShellExecute = true,
// etc ...
}
};
You can read up more on object and collection initializers here, it's an incredibly handy piece syntactic sugar
CodePudding user response:
I would tend to do it like this:
var terraformProcess = Process.Start(new ProcessStartInfo("terraform", "init")
{
UseShellExecute = false,
// ...
});
That will alleviate the need for the separate Start
call. That line could be the straw that breaks the camel's back.