Skip to content

Commit

Permalink
fix(code generator): correctly overwrite files and zero them when ope…
Browse files Browse the repository at this point in the history
…ning (fixes #13).
  • Loading branch information
buehler committed May 8, 2020
1 parent e09b45f commit 4dd0d1c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/KubeOps/Operator/Commands/Generators/CrdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
},
}, Format));
await using var kustomizationFile =
File.OpenWrite(Path.Join(OutputPath, $"kustomization.{Format.ToString().ToLower()}"));
File.Open(Path.Join(OutputPath, $"kustomization.{Format.ToString().ToLower()}"), FileMode.Create);
await kustomizationFile.WriteAsync(kustomizeOutput);
}

Expand All @@ -70,8 +70,8 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)

if (!string.IsNullOrWhiteSpace(OutputPath))
{
await using var file = File.OpenWrite(Path.Join(OutputPath,
$"{crd.Metadata.Name.Replace('.', '_')}.{Format.ToString().ToLower()}"));
await using var file = File.Open(Path.Join(OutputPath,
$"{crd.Metadata.Name.Replace('.', '_')}.{Format.ToString().ToLower()}"), FileMode.Create);
await file.WriteAsync(Encoding.UTF8.GetBytes(output));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
var dockerfile = GenerateDockerfile();
if (!string.IsNullOrWhiteSpace(OutputPath))
{
await using var file = File.OpenWrite(OutputPath);
await using var file = File.Open(OutputPath, FileMode.Create);
await file.WriteAsync(Encoding.UTF8.GetBytes(dockerfile));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
if (!string.IsNullOrWhiteSpace(OutputPath))
{
Directory.CreateDirectory(OutputPath);
await using var nsFile = File.OpenWrite(Path.Join(OutputPath,
$"namespace.{Format.ToString().ToLower()}"));
await using var nsFile = File.Open(Path.Join(OutputPath,
$"namespace.{Format.ToString().ToLower()}"), FileMode.Create);
await nsFile.WriteAsync(Encoding.UTF8.GetBytes(ns));
await using var kustomizeFile = File.OpenWrite(Path.Join(OutputPath,
$"kustomization.{Format.ToString().ToLower()}"));
await using var kustomizeFile = File.Open(Path.Join(OutputPath,
$"kustomization.{Format.ToString().ToLower()}"), FileMode.Create);
await kustomizeFile.WriteAsync(Encoding.UTF8.GetBytes(kustomize));
}
else
Expand Down
7 changes: 4 additions & 3 deletions src/KubeOps/Operator/Commands/Generators/OperatorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
if (!string.IsNullOrWhiteSpace(OutputPath))
{
Directory.CreateDirectory(OutputPath);
await using var file = File.OpenWrite(Path.Join(OutputPath,
$"deployment.{Format.ToString().ToLower()}"));
await using var file = File.Open(Path.Join(OutputPath,
$"deployment.{Format.ToString().ToLower()}"), FileMode.Create);

await file.WriteAsync(Encoding.UTF8.GetBytes(output));

var kustomize = new KustomizationConfig
Expand All @@ -76,7 +77,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
};
var kustomizeOutput = Encoding.UTF8.GetBytes(_serializer.Serialize(kustomize, Format));
await using var kustomizationFile =
File.OpenWrite(Path.Join(OutputPath, $"kustomization.{Format.ToString().ToLower()}"));
File.Open(Path.Join(OutputPath, $"kustomization.{Format.ToString().ToLower()}"), FileMode.Create);
await kustomizationFile.WriteAsync(kustomizeOutput);
}
else
Expand Down
6 changes: 3 additions & 3 deletions src/KubeOps/Operator/Commands/Generators/RbacGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
if (!string.IsNullOrWhiteSpace(OutputPath))
{
Directory.CreateDirectory(OutputPath);
await using var file = File.OpenWrite(Path.Join(OutputPath,
$"operator.{Format.ToString().ToLower()}"));
await using var file = File.Open(Path.Join(OutputPath,
$"operator.{Format.ToString().ToLower()}"), FileMode.Create);
await file.WriteAsync(Encoding.UTF8.GetBytes(output));

var kustomize = new KustomizationConfig
Expand All @@ -44,7 +44,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
};
var kustomizeOutput = Encoding.UTF8.GetBytes(_serializer.Serialize(kustomize, Format));
await using var kustomizationFile =
File.OpenWrite(Path.Join(OutputPath, $"kustomization.{Format.ToString().ToLower()}"));
File.Open(Path.Join(OutputPath, $"kustomization.{Format.ToString().ToLower()}"), FileMode.Create);
await kustomizationFile.WriteAsync(kustomizeOutput);
}
else
Expand Down

0 comments on commit 4dd0d1c

Please sign in to comment.