Пример #1
0
        static int Main(string[] args)
        {
            if (!Utils.CheckAdministrator())
            {
                return(0);
            }

            return(Parser.Default.ParseArguments <ListOptions, DefaultOptions, InstallOptions, RegisterOptions, UninstallOptions, UnregisterOptions, MoveOptions, DuplicateOptions, RunOptions, DirOptions, ConfigEnvOptions, ConfigUidOptions, ConfigKernelCmdOptions, ConfigFlagOptions>(args).MapResult(
                       (ListOptions opts) => {
                Console.WriteLine(string.Join("\n", Wsl.ListDistros().ToArray()));
                return 0;
            },
                       (DefaultOptions opts) => {
                if (opts.Name == null)
                {
                    Console.WriteLine(Wsl.GetDefaultDistro());
                }
                else
                {
                    Wsl.SetDefaultDistro(opts.Name);
                }
                return 0;
            },
                       (InstallOptions opts) => {
                Utils.CheckCaseInsensitive();
                Wsl.InstallDistro(opts.Name, opts.TarFile, opts.TarRootDirectory, opts.TargetDirectory);
                return 0;
            },
                       (RegisterOptions opts) => {
                Wsl.RegisterDistro(opts.Name, opts.InstallationDirectory);
                return 0;
            },
                       (UninstallOptions opts) => {
                Wsl.UninstallDistro(opts.Name);
                return 0;
            },
                       (UnregisterOptions opts) => {
                Wsl.UnregisterDistro(opts.Name);
                return 0;
            },
                       (MoveOptions opts) => {
                Utils.CheckCaseInsensitive();
                Wsl.MoveDistro(opts.Name, opts.TargetDirectory);
                return 0;
            },
                       (DuplicateOptions opts) => {
                Utils.CheckCaseInsensitive();
                Wsl.DuplicateDistro(opts.OldName, opts.NewName, opts.TargetDirectory);
                return 0;
            },
                       (RunOptions opts) => {
                return (int)Wsl.LaunchDistro(opts.Name, opts.Command, !opts.NoCwd);
            },
                       (DirOptions opts) => {
                Console.WriteLine(Wsl.GetInstallationDirectory(opts.Name));
                return 0;
            },
                       (ConfigEnvOptions opts) => {
                var envVars = Wsl.GetDefaultEnvironment(opts.Name).ToList();
                if (opts.VariableToAdd != null)
                {
                    if (!opts.VariableToAdd.Contains('='))
                    {
                        Utils.Error($"Environment variable must contain \"=\": \"{opts.VariableToAdd}\".");
                    }
                    envVars.Add(opts.VariableToAdd);
                    Wsl.SetDefaultEnvironment(opts.Name, envVars.ToArray());
                }
                else if (opts.VariableToRemove != null)
                {
                    if (opts.VariableToRemove.Contains('='))
                    {
                        Utils.Error($"Environment variable name should not contain \"=\": \"{opts.VariableToRemove}\".");
                    }
                    var newEnvVars = envVars.Where(s => !s.StartsWith($"{opts.VariableToRemove}="));
                    if (envVars.Count == newEnvVars.Count())
                    {
                        Utils.Error($"Environment variable not found: {opts.VariableToRemove}.");
                    }
                    Wsl.SetDefaultEnvironment(opts.Name, newEnvVars.ToArray());
                }
                else
                {
                    Console.Write(string.Join("\n", envVars));
                }
                return 0;
            },
                       (ConfigUidOptions opts) => {
                if (opts.Uid == null)
                {
                    Console.WriteLine(Wsl.GetDefaultUid(opts.Name));
                }
                else
                {
                    Wsl.SetDefaultUid(opts.Name, opts.Uid.Value);
                }
                return 0;
            },
                       (ConfigKernelCmdOptions opts) => {
                if (opts.Commandline == null)
                {
                    Console.WriteLine(Wsl.GetKernelCommandLine(opts.Name));
                }
                else
                {
                    Wsl.SetKernelCommandLine(opts.Name, opts.Commandline);
                }
                return 0;
            },
                       (ConfigFlagOptions opts) => {
                switch (opts.FlagName)
                {
                case "WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP":
                    if (opts.Flag == null)
                    {
                        Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableInterop) ? 1 : 0);
                    }
                    else
                    {
                        Wsl.SetFlag(opts.Name, DistroFlags.EnableInterop, opts.Flag != 0);
                    }
                    return 0;

                case "WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH":
                    if (opts.Flag == null)
                    {
                        Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.AppendNtPath) ? 1 : 0);
                    }
                    else
                    {
                        Wsl.SetFlag(opts.Name, DistroFlags.AppendNtPath, opts.Flag != 0);
                    }
                    return 0;

                case "WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING":
                    if (opts.Flag == null)
                    {
                        Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableDriveMounting) ? 1 : 0);
                    }
                    else
                    {
                        Wsl.SetFlag(opts.Name, DistroFlags.EnableDriveMounting, opts.Flag != 0);
                    }
                    return 0;

                default:
                    Utils.Error("Flag name not found.");
                    return 1;                             // Never reached.
                }
            },
                       errs => 1
                       ));
        }
Пример #2
0
        static int Main(string[] args)
        {
            return(Parser.Default.ParseArguments <ListOptions, DefaultOptions, InstallOptions, RegisterOptions, UninstallOptions, UnregisterOptions, MoveOptions, RunOptions, DirOptions, ConfigEnvOptions, ConfigUidOptions, ConfigKernelCmdOptions, ConfigFlagOptions>(args).MapResult(
                       (ListOptions opts) => {
                Console.WriteLine(string.Join("\n", Wsl.ListDistros().ToArray()));
                return 0;
            },
                       (DefaultOptions opts) => {
                if (opts.Name == null)
                {
                    Console.WriteLine(Wsl.GetDefaultDistro());
                }
                else
                {
                    Wsl.SetDefaultDistro(opts.Name);
                }
                return 0;
            },
                       (InstallOptions opts) => {
                Wsl.InstallDistro(opts.Name, opts.TarGzFile, opts.TargetDirectory);
                return 0;
            },
                       (RegisterOptions opts) => {
                Wsl.RegisterDistro(opts.Name, opts.InstallationDirectory);
                return 0;
            },
                       (UninstallOptions opts) => {
                Wsl.UninstallDistro(opts.Name);
                return 0;
            },
                       (UnregisterOptions opts) => {
                Wsl.UnregisterDistro(opts.Name);
                return 0;
            },
                       (MoveOptions opts) => {
                Wsl.MoveDistro(opts.Name, opts.TargetDirectory);
                return 0;
            },
                       (RunOptions opts) => {
                return (int)Wsl.LaunchDistro(opts.Name, opts.Command);
            },
                       (DirOptions opts) => {
                Console.WriteLine(Wsl.GetInstallationDirectory(opts.Name));
                return 0;
            },
                       (ConfigEnvOptions opts) => {
                if (opts.EnvironmentVariables == null)
                {
                    Console.WriteLine(string.Join("\n", Wsl.GetDefaultEnvironment(opts.Name)));
                }
                else
                {
                    Wsl.SetDefaultEnvironment(opts.Name, opts.EnvironmentVariables);
                }
                return 0;
            },
                       (ConfigUidOptions opts) => {
                if (opts.Uid == null)
                {
                    Console.WriteLine(Wsl.GetDefaultUid(opts.Name));
                }
                else
                {
                    Wsl.SetDefaultUid(opts.Name, opts.Uid.Value);
                }
                return 0;
            },
                       (ConfigKernelCmdOptions opts) => {
                if (opts.Commandline == null)
                {
                    Console.WriteLine(Wsl.GetKernelCommandLine(opts.Name));
                }
                else
                {
                    Wsl.SetKernelCommandLine(opts.Name, opts.Commandline);
                }
                return 0;
            },
                       (ConfigFlagOptions opts) => {
                switch (opts.FlagName)
                {
                case "WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP":
                    if (opts.Flag == null)
                    {
                        Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableInterop) ? 1 : 0);
                    }
                    else
                    {
                        Wsl.SetFlag(opts.Name, DistroFlags.EnableInterop, opts.Flag != 0);
                    }
                    return 0;

                case "WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH":
                    if (opts.Flag == null)
                    {
                        Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.AppendNtPath) ? 1 : 0);
                    }
                    else
                    {
                        Wsl.SetFlag(opts.Name, DistroFlags.AppendNtPath, opts.Flag != 0);
                    }
                    return 0;

                case "WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING":
                    if (opts.Flag == null)
                    {
                        Console.WriteLine(Wsl.GetFlag(opts.Name, DistroFlags.EnableDriveMounting) ? 1 : 0);
                    }
                    else
                    {
                        Wsl.SetFlag(opts.Name, DistroFlags.EnableDriveMounting, opts.Flag != 0);
                    }
                    return 0;

                default:
                    Console.Error.WriteLine("Flag name not found.");
                    return 1;
                }
            },
                       errs => 1
                       ));
        }