Skip to content

Commit

Permalink
Add ckan mark auto/user commands
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed May 7, 2019
1 parent 6b1d95d commit 5fa1603
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cmdline/Action/AuthToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public AuthToken() { }
/// <summary>
/// Run the subcommand
/// </summary>
/// <param name="mgr">Manager to provide game instances</param>
/// <param name="opts">Command line parameters paritally handled by parser</param>
/// <param name="unparsed">Command line parameters not yet handled by parser</param>
/// <returns>
/// Exit code
Expand Down
158 changes: 158 additions & 0 deletions Cmdline/Action/Mark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System.Collections.Generic;
using CommandLine;
using CommandLine.Text;
using log4net;

namespace CKAN.CmdLine
{
/// <summary>
/// Subcommand for setting flags on modules,
/// currently the auto-installed flag
/// </summary>
public class Mark : ISubCommand
{
/// <summary>
/// Initialize the subcommand
/// </summary>
public Mark() { }

/// <summary>
/// Run the subcommand
/// </summary>
/// <param name="mgr">Manager to provide game instances</param>
/// <param name="opts">Command line parameters paritally handled by parser</param>
/// <param name="unparsed">Command line parameters not yet handled by parser</param>
/// <returns>
/// Exit code
/// </returns>
public int RunSubCommand(KSPManager mgr, CommonOptions opts, SubCommandOptions unparsed)
{
string[] args = unparsed.options.ToArray();
int exitCode = Exit.OK;
// Parse and process our sub-verbs
Parser.Default.ParseArgumentsStrict(args, new MarkSubOptions(), (string option, object suboptions) =>
{
// ParseArgumentsStrict calls us unconditionally, even with bad arguments
if (!string.IsNullOrEmpty(option) && suboptions != null)
{
CommonOptions options = (CommonOptions)suboptions;
options.Merge(opts);
user = new ConsoleUser(options.Headless);
manager = mgr ?? new KSPManager(user);
exitCode = options.Handle(manager, user);
if (exitCode != Exit.OK)
return;

switch (option)
{
case "auto":
exitCode = MarkAuto((MarkAutoOptions)suboptions, true, option, "auto-installed");
break;

case "user":
exitCode = MarkAuto((MarkAutoOptions)suboptions, false, option, "user-selected");
break;

default:
user.RaiseMessage("Unknown command: mark {0}", option);
exitCode = Exit.BADOPT;
break;
}
}
}, () => { exitCode = MainClass.AfterHelp(); });
return exitCode;
}

private int MarkAuto(MarkAutoOptions opts, bool value, string verb, string descrip)
{
if (opts.modules.Count < 1)
{
user.RaiseMessage("Usage: ckan mark {0} Mod [Mod2 ...]", verb);
return Exit.BADOPT;
}

int exitCode = opts.Handle(manager, user);
if (exitCode != Exit.OK)
{
return exitCode;
}

var ksp = MainClass.GetGameInstance(manager);
var regMgr = RegistryManager.Instance(ksp);
bool needSave = false;
Search.AdjustModulesCase(ksp, opts.modules);
foreach (string id in opts.modules)
{
InstalledModule im = regMgr.registry.InstalledModule(id);
if (im == null)
{
user.RaiseError("{0} is not installed.", id);
}
else if (im.AutoInstalled == value)
{
user.RaiseError("{0} is already marked as {1}.", id, descrip);
}
else
{
user.RaiseMessage("Marking {0} as {1}...", id, descrip);
im.AutoInstalled = value;
needSave = true;
}
}
if (needSave)
{
regMgr.Save(false);
user.RaiseMessage("Changes made!");
}
return Exit.OK;
}

private KSPManager manager { get; set; }
private IUser user { get; set; }

private static readonly ILog log = LogManager.GetLogger(typeof(Mark));
}

internal class MarkSubOptions : VerbCommandOptions
{
[VerbOption("auto", HelpText = "Mark modules as auto installed")]
public MarkAutoOptions MarkAutoOptions { get; set; }

[VerbOption("user", HelpText = "Mark modules as user selected (opposite of auto installed)")]
public MarkAutoOptions MarkUserOptions { get; set; }

[HelpVerbOption]
public string GetUsage(string verb)
{
HelpText ht = HelpText.AutoBuild(this, verb);
// Add a usage prefix line
ht.AddPreOptionsLine(" ");
if (string.IsNullOrEmpty(verb))
{
ht.AddPreOptionsLine("ckan mark - Edit flags on modules");
ht.AddPreOptionsLine($"Usage: ckan mark <command> [options]");
}
else
{
ht.AddPreOptionsLine("ksp " + verb + " - " + GetDescription(verb));
switch (verb)
{
case "auto":
ht.AddPreOptionsLine($"Usage: ckan mark {verb} [options] Mod [Mod2 ...]");
break;

case "user":
ht.AddPreOptionsLine($"Usage: ckan mark {verb} [options] Mod [Mod2 ...]");
break;
}
}
return ht;
}
}

internal class MarkAutoOptions : InstanceSpecificOptions
{
[ValueList(typeof(List<string>))]
public List<string> modules { get; set; }
}
}
1 change: 1 addition & 0 deletions Cmdline/CKAN-cmdline.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="Action\ISubCommand.cs" />
<Compile Include="Action\KSP.cs" />
<Compile Include="Action\List.cs" />
<Compile Include="Action\Mark.cs" />
<Compile Include="Action\Remove.cs" />
<Compile Include="Action\Prompt.cs" />
<Compile Include="Action\Repair.cs" />
Expand Down
4 changes: 3 additions & 1 deletion Cmdline/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public static int Execute(KSPManager manager, CommonOptions opts, string[] args)

case "cache":
return (new Cache()).RunSubCommand(manager, opts, new SubCommandOptions(args));


case "mark":
return (new Mark()).RunSubCommand(manager, opts, new SubCommandOptions(args));
}
}
catch (NoGameInstanceKraken)
Expand Down
3 changes: 3 additions & 0 deletions Cmdline/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ internal class Actions : VerbCommandOptions
[VerbOption("repo", HelpText = "Manage CKAN repositories")]
public SubCommandOptions Repo { get; set; }

[VerbOption("mark", HelpText = "Edit flags on modules")]
public SubCommandOptions Mark { get; set; }

[VerbOption("ksp", HelpText = "Manage KSP installs")]
public SubCommandOptions KSP { get; set; }

Expand Down

0 comments on commit 5fa1603

Please sign in to comment.