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

Commit

Permalink
Add CLI command: install <pluginName> and uninstall <pluginName> (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
陈志同 authored and erikzhang committed Dec 14, 2018
1 parent d78c38f commit 40d95a7
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
15 changes: 15 additions & 0 deletions neo-cli/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Linq;
using System.Reflection;

namespace Neo
{
internal static class Helper
{
internal static string GetVersion(this Assembly assembly)
{
CustomAttributeData attribute = assembly.CustomAttributes.FirstOrDefault(p => p.AttributeType == typeof(AssemblyInformationalVersionAttribute));
if (attribute == null) return assembly.GetName().Version.ToString(3);
return (string)attribute.ConstructorArguments[0].Value;
}
}
}
8 changes: 5 additions & 3 deletions neo-cli/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Neo.Network.P2P;
using System.Net;

namespace Neo
{
Expand All @@ -9,7 +9,8 @@ internal class Settings
public PathsSettings Paths { get; }
public P2PSettings P2P { get; }
public RPCSettings RPC { get; }
public UnlockWalletSettings UnlockWallet { get; set; }
public UnlockWalletSettings UnlockWallet { get; }
public string PluginURL { get; }

public static Settings Default { get; }

Expand All @@ -25,6 +26,7 @@ public Settings(IConfigurationSection section)
this.P2P = new P2PSettings(section.GetSection("P2P"));
this.RPC = new RPCSettings(section.GetSection("RPC"));
this.UnlockWallet = new UnlockWalletSettings(section.GetSection("UnlockWallet"));
this.PluginURL = section.GetSection("PluginURL").Value;
}
}

Expand Down
57 changes: 56 additions & 1 deletion neo-cli/Shell/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
Expand Down Expand Up @@ -89,6 +90,10 @@ protected override bool OnCommand(string[] args)
return OnStartCommand(args);
case "upgrade":
return OnUpgradeCommand(args);
case "install":
return OnInstallCommand(args);
case "uninstall":
return OnUnInstallCommand(args);
default:
return base.OnCommand(args);
}
Expand Down Expand Up @@ -392,7 +397,6 @@ private bool OnHelpCommand(string[] args)
"Normal Commands:\n" +
"\tversion\n" +
"\thelp [plugin-name]\n" +
"\tplugins\n" +
"\tclear\n" +
"\texit\n" +
"Wallet Commands:\n" +
Expand All @@ -416,6 +420,10 @@ private bool OnHelpCommand(string[] args)
"\tshow state\n" +
"\tshow pool [verbose]\n" +
"\trelay <jsonObjectToSign>\n" +
"Plugin Commands:\n" +
"\tplugins\n" +
"\tinstall <pluginName>\n" +
"\tuninstall <pluginName>\n" +
"Advanced Commands:\n" +
"\tstart consensus\n");

Expand Down Expand Up @@ -941,6 +949,53 @@ private bool OnUpgradeCommand(string[] args)
}
}

private bool OnInstallCommand(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("error");
return true;
}
var pluginName = args[1];
var address = string.Format(Settings.Default.PluginURL, pluginName, typeof(Plugin).Assembly.GetVersion());
var fileName = Path.Combine("Plugins", $"{pluginName}.zip");
Directory.CreateDirectory("Plugins");
Console.WriteLine($"Downloading from {address}");
using (WebClient wc = new WebClient())
{
wc.DownloadFile(address, fileName);
}
try
{
ZipFile.ExtractToDirectory(fileName, ".");
}
catch (IOException)
{
Console.WriteLine($"Plugin already exist.");
return true;
}
finally
{
File.Delete(fileName);
}
Console.WriteLine($"Install successful, please restart neo-cli.");
return true;
}

private bool OnUnInstallCommand(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("error");
return true;
}
var pluginName = args[1];
Directory.Delete(Path.Combine("Plugins", pluginName), true);
File.Delete(Path.Combine("Plugins", $"{pluginName}.dll"));
Console.WriteLine($"Uninstall successful, please restart neo-cli.");
return true;
}

private bool OnUpgradeWalletCommand(string[] args)
{
if (args.Length < 3)
Expand Down
3 changes: 2 additions & 1 deletion neo-cli/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Password": "",
"StartConsensus": false,
"IsActive": false
}
},
"PluginURL": "https://github.com/neo-project/neo-plugins/releases/download/v{1}/{0}.zip"
}
}
3 changes: 2 additions & 1 deletion neo-cli/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Password": "",
"StartConsensus": false,
"IsActive": false
}
},
"PluginURL": "https://github.com/neo-project/neo-plugins/releases/download/v{1}/{0}.zip"
}
}
3 changes: 2 additions & 1 deletion neo-cli/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Password": "",
"StartConsensus": false,
"IsActive": false
}
},
"PluginURL": "https://github.com/neo-project/neo-plugins/releases/download/v{1}/{0}.zip"
}
}

0 comments on commit 40d95a7

Please sign in to comment.