Skip to content

Commit

Permalink
can now take user input and process the diff
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen committed Oct 28, 2024
1 parent b292d35 commit 1d158d3
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/CIM.Differ.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
using System.CommandLine;
using CIM.Cson;
using CIM.Cson;
using CIM.PhysicalNetworkModel;
using System.CommandLine;

namespace CIM.Differ.CLI;

internal static class Program
{
public static async Task<int> Main(string[] args)
{
var previousStateFileOption = new Option<string>(
name: "--previous-state-file",
description: "The path to the previous state file. Example: './my-previous-state-file.jsonl'."
) { IsRequired = true };

var newStateFileOption = new Option<string>(
name: "--new-state-file",
description: "The path to the new state file. Example: './my-new-state-file.jsonl'."
) { IsRequired = true };

var outputFileOption = new Option<string>(
name: "--output-file",
description: "The path and filename of the output file. Example: '/home/notation/my-new-outputfile.jsonl'."
) { IsRequired = true };

var rootCommand = new RootCommand("CIM Differ CLI.");
rootCommand.Add(previousStateFileOption);
rootCommand.Add(newStateFileOption);
rootCommand.Add(outputFileOption);

var firstFileAbsolutePath = "";
var secondFileAbsolutePath = "";
var outputFileAbsolutePath = "";
rootCommand.SetHandler(async (previousStateFilePath, newStateFilePath, outputFilePath) =>
{
await ProcessDiffAsync(previousStateFilePath, newStateFilePath, outputFilePath).ConfigureAwait(false);
},
previousStateFileOption, newStateFileOption, outputFileOption);

return await rootCommand.InvokeAsync(args).ConfigureAwait(false);
}

private static async Task ProcessDiffAsync(string previousStateFilePath, string newStateFilePath, string outputFilePath)
{
var serializer = new CsonSerializer();
var differ = new CimDiffer();

IEnumerable<IdentifiedObject> firstFileIdentifiedObjects = ReadIdentifiedObjectFile(serializer, firstFileAbsolutePath);
IEnumerable<IdentifiedObject> secondFileIdentifiedObjects = ReadIdentifiedObjectFile(serializer, secondFileAbsolutePath);
var firstFileIdentifiedObjects = ReadIdentifiedObjectFile(serializer, previousStateFilePath);
var secondFileIdentifiedObjects = ReadIdentifiedObjectFile(serializer, newStateFilePath);

var differ = new CimDiffer();
using (var destination = File.Open(outputFileAbsolutePath, FileMode.CreateNew))
using (var destination = File.Open(outputFilePath, FileMode.CreateNew))
{
using (var source = serializer.SerializeObjects(differ.GetDiff(firstFileIdentifiedObjects, secondFileIdentifiedObjects)))
{
await source.CopyToAsync(destination).ConfigureAwait(false);
}
}

return await rootCommand.InvokeAsync(args).ConfigureAwait(false);
}

private static IEnumerable<IdentifiedObject> ReadIdentifiedObjectFile(CsonSerializer serializer, string filePath)
Expand Down

0 comments on commit 1d158d3

Please sign in to comment.