SideCarCLI- console to dot net tool

Summary links SideCarCLI

NoName + Link 
1Description
2Specifications
3Refactor specifications and code
4Create Release
5Finish interceptors
6Send part of command to interceptors
7Line Interceptors
8Finish process after some time
9Documetation Diagram
10Technical Summary
11Create dotnet tool
( Description : SideCar for CLI applications. Interceptors for Line, Finish, Timer . See Code )

To create a .NET Tool, please read first https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools-how-to-create 

Of course , all the  nuget additional formats for csproj apply: read https://docs.microsoft.com/en-us/dotnet/core/tools/csproj . Of course, the easy way to in Visual Studio to right click the project, properties, Package.

This is the final xml from .csproj:

<PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>netcoreapp3.1</TargetFramework>
     <RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>
     <PackAsTool>true</PackAsTool>
     <ToolCommandName>sidecarcli</ToolCommandName>
     <Title>SideCarCLI</Title>
    
     <PackageOutputPath>./nupkg</PackageOutputPath>
     <PackageId>sidecarcli</PackageId>
     <Version>2020.111.104</Version>
     <Authors>Andrei Ignat</Authors>
     <Company>AOM</Company>
     <Product>sidecarcli</Product>
     <Description>A SideCar for Command Line Applications. Read http://msprogrammer.serviciipeweb.ro/category/sidecar/ . Code source at https://github.com/ignatandrei/sidecarcli</Description>
     <PackageLicenseExpression>MIT</PackageLicenseExpression>
     <PackageProjectUrl>http://msprogrammer.serviciipeweb.ro/category/sidecar/</PackageProjectUrl>
     <RepositoryUrl>https://github.com/ignatandrei/SideCarCLI/</RepositoryUrl>
     <RepositoryType>GIT</RepositoryType>
     <PackageTags>SideCarCLI;Side Car; Command Line</PackageTags>
     <PackageReleaseNotes>First version. Read http://msprogrammer.serviciipeweb.ro/category/sidecar/</PackageReleaseNotes>
   </PropertyGroup>

 

You can create the project with

dotnet pack

command

Now go to https://www.nuget.org/packages/manage/upload and upload the project.

You can find the final result at https://www.nuget.org/packages/sidecarcli/  and you can install it

And this concludes the work for https://github.com/ignatandrei/SideCarCLI/issues/10 .

SideCarCLI–looking at past and at future

Summary links SideCarCLI

NoName + Link 
1Description
2Specifications
3Refactor specifications and code
4Create Release
5Finish interceptors
6Send part of command to interceptors
7Line Interceptors
8Finish process after some time
9Documetation Diagram
10Technical Summary
11Create dotnet tool
( Description : SideCar for CLI applications. Interceptors for Line, Finish, Timer . See Code )

It was interesting to develop the project for command line same as for cloud design pattern, https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar .

The problem were more about

  1. architecture
  2. organizing features to be easy understandable
  3. , testing the application
  4. writing about

 

rather than  technical, about how to make the application.

Other , it was a pretty simple project , that can be useful in  CI / CD operations.

It remains :

Make a dot net tool –  https://github.com/ignatandrei/SideCarCLI/issues/10

Compiling as Windows and Linux https://github.com/ignatandrei/SideCarCLI/issues/11

Make documentation with use case – https://github.com/ignatandrei/SideCarCLI/issues/12

Make Tests  https://github.com/ignatandrei/SideCarCLI/issues/13

SideCarCLI- diagram for how it is working

Summary links SideCarCLI

NoName + Link 
1Description
2Specifications
3Refactor specifications and code
4Create Release
5Finish interceptors
6Send part of command to interceptors
7Line Interceptors
8Finish process after some time
9Documetation Diagram
10Technical Summary
11Create dotnet tool
( Description : SideCar for CLI applications. Interceptors for Line, Finish, Timer . See Code )

Usually , a picture is better than words . So , I can explain how SideCarCLI works – or I can draw an image.

The easiest way to draw an image is with  https://mermaidjs.github.io/ –  that has also a live editor at https://mermaid-js.github.io/mermaid-live-editor .

The code for generating the picture is

sequenceDiagram

participant S as SideCar

participant R as Real Application

participant L as Line Interceptor(s)

participant F as Finish Interceptor(s)

participant T as Timer Interceptor(s)

S->>R: Start Application

loop for each timer in json

S->>T: Start Timer application

end

loop for each line output from real application

R->>S: Send Output  Line to SideCae\r

S->>+L: Start Line Interceptor with line output in command line

L->>-S: execute and returns

end

R->>S: End Application

S->>+F: Send line output

F->>-S: execute and returns

and you can see here the picture

My GITHUB integrations / apps

Github has integrations apps – means bots that automatically interacts with your repository. Those are the bots most used by me:

 

No Name Description
1 All Contributors https://github.com/settings/installations/7389985 Add contributions to the repository. Example at https://github.com/ignatandrei/netcoreblockly , see contributions added automatically
2 Img Bot https://github.com/settings/installations/310858 Automatically optimizes the images and creates a PR for them . Example at https://github.com/ignatandrei/NETCoreBlockly/pull/125
3 Todo Bot https://github.com/settings/installations/73483 Allow  to transfer TODO comments in the code to create a new issue. Example : https://github.com/ignatandrei/NETCoreBlockly/issues/132
4 Triage New Issues https://github.com/settings/installations/7390132  When create a new issue without labels, put the triage label to be easy found later. For example, https://github.com/ignatandrei/NETCoreBlockly/issues/65
5 Weekly Digest https://github.com/settings/installations/7390079  If interested what’s new with your repositories. For example, https://github.com/ignatandrei/NETCoreBlockly/issues/73
6 Depend A Bot https://github.com/settings/installations/652713 Scans the repository for outdated

 

Also, I am a big fan of GitHub Actions – easy to use. See an example at https://github.com/ignatandrei/NETCoreBlockly/blob/master/.github/workflows/dotnetTools.yml

SideCARCLI–Finish process after some time

Summary links SideCarCLI

NoName + Link 
1Description
2Specifications
3Refactor specifications and code
4Create Release
5Finish interceptors
6Send part of command to interceptors
7Line Interceptors
8Finish process after some time
9Documetation Diagram
10Technical Summary
11Create dotnet tool
( Description : SideCar for CLI applications. Interceptors for Line, Finish, Timer . See Code )

One of the feature is to let the original process execute just a limited amount of time. For this, I have defined an option

var maxSeconds = app.Option(“-mS|–maxSeconds”, “max seconds for the StartApp to run”, CommandOptionType.SingleOrNoValue);

and the code for waiting is

Process p = new Process()
{
StartInfo = pi
};

//code

var res=p.WaitForExit(this.MaxSeconds);

//code

if (res)
{
exitCode = p.ExitCode;
RunFinishInterceptors(exitCode);
}
else
{
Console.WriteLine($”timeout {MaxSeconds} elapsed”);
exitCode = int.MinValue;
}

 

where the default value for this.MaxSeconds is –1 – wait undefinetely

SideCarCLI – Line interceptors

Summary links SideCarCLI

NoName + Link 
1Description
2Specifications
3Refactor specifications and code
4Create Release
5Finish interceptors
6Send part of command to interceptors
7Line Interceptors
8Finish process after some time
9Documetation Diagram
10Technical Summary
11Create dotnet tool
( Description : SideCar for CLI applications. Interceptors for Line, Finish, Timer . See Code )

For the SideCarCLI I have the concept of Line Interceptors. That means for each line from the original program another program will be start .

The json looks like this

“LineInterceptors”: [
{
“Name”: “WindowsStandardWindowsOutputInterceptor”,
“Arguments”: “/c echo \”{site} {line}\””,
“FullPath”: “cmd.exe”,
“FolderToExecute”: null,
“InterceptOutput”: true
},
{
“Name”: “NextInterceptor”,
“Arguments”: “/c echo {line}”,
“FullPath”: “cmd.exe”,
“FolderToExecute”: null,
“InterceptOutput”: true
}
]

and you can activate by command line

-aLi WindowsStandardWindowsOutputInterceptor

Pretty simple , right ? For the code , means intercepting each line

p.OutputDataReceived += P_OutputDataReceived;
p.ErrorDataReceived += P_ErrorDataReceived;

and then run the process

foreach(var item in runningInterceptors.LineInterceptors)
{
try
{

var local = item;
var dataToBeParsed=new Dictionary<string, string>();

if(this.argsRegex.Count>0)
{
foreach (var reg in argsRegex)
{
dataToBeParsed.Add(“{“+reg.Key+”}”, reg.Value);
}
}
dataToBeParsed[“{line}”]= e.Data;
allProcesses.TryAdd(local.Name, local.RunLineInterceptor(local.Name,dataToBeParsed));

}
catch (Exception ex)
{
Console.WriteLine($”LineInterceptor:{item?.Name} error:!!!” + ex.Message);
}
}

Every time it is interesting how a simple specification means multiple lines of code….

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.