Skip to content

Commit

Permalink
Merge pull request #542 from WildernessLabs/return_codes
Browse files Browse the repository at this point in the history
Return error codes on failure
  • Loading branch information
ctacke authored Mar 23, 2024
2 parents a4bacb7 + 08f93f9 commit 7839d5f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Source/v2/Meadow.Cli/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
<Authors>Wilderness Labs, Inc</Authors>
<Company>Wilderness Labs, Inc</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.0.30.0</PackageVersion>
<PackageVersion>2.0.31.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageTags>Meadow, Meadow.Foundation, Meadow.CLI</PackageTags>
<PackageTags>Meadow,Meadow.Foundation,Meadow.CLI,CLI,command,line,interface,device,IoT</PackageTags>
<Description>Command-line interface for Meadow</Description>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
Expand Down
41 changes: 13 additions & 28 deletions Source/v2/Meadow.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,13 @@

public class Program
{
public static async Task<int> Main(string[] args)
public static async Task<int> Main(string[] _)
{
var logLevel = LogEventLevel.Information;
var logModifier = args.FirstOrDefault(a => a.Contains("-m"))
?.Count(x => x == 'm') ?? 0;

logLevel -= logModifier;
if (logLevel < 0)
{
logLevel = 0;
}

var outputTemplate = logLevel == LogEventLevel.Verbose
? "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj}{NewLine}{Exception}"
: "{Message:lj}{NewLine}{Exception}";
var outputTemplate = "{Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration().MinimumLevel.Verbose()
.WriteTo.Console(logLevel, outputTemplate)
.WriteTo.Console(LogEventLevel.Information, outputTemplate)
.CreateLogger();

// Log that we're using a log level other than default of Information
if (logLevel != LogEventLevel.Information)
{
Console.WriteLine($"Using log level {logLevel}");
}

var services = new ServiceCollection();

services.AddLogging(
Expand Down Expand Up @@ -89,30 +71,33 @@ public static async Task<int> Main(string[] args)

var serviceProvider = services.BuildServiceProvider();

int returnCode;

try
{
await new CliApplicationBuilder()
returnCode = await new CliApplicationBuilder()
.AddCommandsFromThisAssembly()
.UseTypeActivator(serviceProvider.GetService!)
.SetExecutableName("meadow")
.Build()
.RunAsync();
}
catch (CommandException ce)
{
returnCode = ce.ExitCode;
}
catch (Exception ex)
{
Console.WriteLine($"Operation failed: {ex.Message}");
#if DEBUG
throw; //debug spew for debug builds
#endif
returnCode = 1;
}

Environment.Exit(0);
return 0;
return returnCode;
}

private static void AddCommandsAsServices(IServiceCollection services)
{
var assembly = System.Reflection.Assembly.GetEntryAssembly(); //.GetAssembly(typeof(Program));
var assembly = System.Reflection.Assembly.GetEntryAssembly();
Trace.Assert(assembly != null);
var types = assembly?.GetTypes();

Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Meadow.CLI;

public static class Constants
{
public const string CLI_VERSION = "2.0.30.0";
public const string CLI_VERSION = "2.0.31.0";
}

0 comments on commit 7839d5f

Please sign in to comment.