Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
Added distro selection switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
cerebrate committed Feb 24, 2021
1 parent d12aa93 commit 3378cdd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 40 deletions.
111 changes: 72 additions & 39 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// RunInGenie - Program.cs
//
// Created by: Alistair J R Young (avatar) at 2021/01/17 1:07 PM.
// Created by: Alistair J R Young (avatar) at 2021/02/24 1:21 AM.

#endregion

Expand All @@ -12,15 +12,49 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

#endregion

namespace ArkaneSystems.RunInGenie
{
internal static class Program
{
private static IConfiguration Configuration
{
get
{
IConfiguration config = new ConfigurationBuilder ()
.AddJsonFile (path: "$.json", optional: true, reloadOnChange: false)
.Build ();

return config;
}
}

private static string Shell
{
get
{
string shell = Program.Configuration[key: "shell"];
if (shell == null || shell.Equals (value: string.Empty)) shell = "sh";

return shell;
}
}

private static string Distro
{
get
{
string distro = Program.Configuration[key: "distro"];

return distro ?? string.Empty;
}
}

private static bool IsWindowsPath (string arg)
{
try
Expand All @@ -37,8 +71,10 @@ private static bool IsWindowsPath (string arg)

private static string TranslatePath (string path)
{
// ReSharper disable once VariableHidesOuterVariable
string InvokeWslpath (string path)
{
// ReSharper disable once UseObjectOrCollectionInitializer
Process wp = new ();
wp.StartInfo.UseShellExecute = false;
wp.StartInfo.RedirectStandardOutput = true;
Expand Down Expand Up @@ -77,38 +113,6 @@ string InvokeWslpath (string path)
throw new InvalidOperationException (message: "What the path is this?");
}

private static IConfiguration Configuration {

get
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("$.json", true, false)
.Build();
return config;
}
}

private static string Shell
{
get
{
string shell = Configuration["shell"];
if(shell == null || shell.Equals(String.Empty)) {
shell = "sh";
}
return shell;
}
}

private static string Distro
{
get
{
string distro = Configuration["distro"];
return (distro != null) ? distro : String.Empty;
}
}

private static void PrintHelp ()
{
ConsoleColor oldColor = Console.ForegroundColor;
Expand All @@ -125,30 +129,59 @@ private static void PrintHelp ()
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine (value: "$ systemctl status\n");
Console.ForegroundColor = oldColor;

Console.WriteLine (value: "$ without a following command starts a (non-login) shell.\n");

Console.WriteLine (value: "A WSL distribution other than the default can be supplied using the -d/--distro option:");

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine (value: "$ -d alpine systemctl status\n");
Console.ForegroundColor = oldColor;
}

private static int Main (string[] args)
{
// First check if help has been requested; if so, print it.
// Set default distro and shell
string distro = Program.Distro == string.Empty ? string.Empty : "-d {Program.Distro}";
string shell = Program.Shell == string.Empty ? "sh" : Program.Shell;

// Cheap and nasty manual command parsing.
// First check if help has been requested; if so, print it and exit.
if (args.Length == 1 && (args[0] == "-h" || args[0] == "--help"))
{
Program.PrintHelp ();

return 0;
}

// Second, check if a distro was specified (must be first argument).
if (args.Length >= 1 && (args[0] == "-d" || args[0] == "--distro"))
{
if (args.Length == 1)
{
Console.WriteLine (value: "If specifying a distro, you must specify a distro.");

return 1;
}

distro = $"-d {args[1]}";

args = args.Skip (count: 2).ToArray ();
}

try
{
// Set chosen distro and shell
string distro = (Program.Distro == String.Empty) ? String.Empty : "-d {Program.Distro}";
string arguments = $"{distro} genie -c {Program.Shell}";
string arguments = $"{distro} genie -c {shell}";

Process ps;
if(args.Length > 0) {

if (args.Length > 0)
{
// Check through additional arguments, one by one.
var param = new List<string> ();

foreach (var arg in args)

// Identify those which are probably Windows paths.
// And perform path translation.
param.Add (item: Program.IsWindowsPath (arg: arg) ? Program.TranslatePath (path: arg) : arg);
Expand Down
8 changes: 8 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"RunInGenie": {
"commandName": "Project",
"workingDirectory": "C:\\Working"
}
}
}
6 changes: 5 additions & 1 deletion RunInGenie.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
<RootNamespace>ArkaneSystems.RunInGenie</RootNamespace>
<AssemblyName>$</AssemblyName>
<Nullable>enable</Nullable>
<Version>0.1</Version>
<Version>0.2</Version>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>false</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Authors>Alistair J. R. Young</Authors>
<Company>Arkane Systems</Company>
<Product />
<Copyright>Copyright (c) 2021 Arkane Systems</Copyright>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
Expand Down
2 changes: 2 additions & 0 deletions RunInGenie.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Wslpath/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 comments on commit 3378cdd

Please sign in to comment.