Пример #1
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            PropertyRegistry properties = Properties;
            if (properties == null) {
                Application.Error.WriteLine("the current context does not support properties.");
                return CommandResultCode.ExecutionFailed;
            }

            if (args.MoveNext()) {
                string name = args.Current;
                PropertyHolder holder = properties.GetProperty(name);
                if (holder == null)
                    return CommandResultCode.ExecutionFailed;

                PrintDescription(name, holder, Application.Error);
                return CommandResultCode.Success;
            }

            ProperiesColumns[0].ResetWidth();
            ProperiesColumns[1].ResetWidth();
            TableRenderer table = new TableRenderer(ProperiesColumns, Application.Out);
            foreach(KeyValuePair<string, PropertyHolder> entry in properties) {
                ColumnValue[] row = new ColumnValue[3];
                PropertyHolder holder = entry.Value;
                row[0] = new ColumnValue(entry.Key);
                row[1] = new ColumnValue(holder.Value);
                row[2] = new ColumnValue(holder.ShortDescription);
                table.AddRow(row);
            }
            table.CloseTable();
            return CommandResultCode.Success;
        }
Пример #2
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            ISettingsHandler handler = Application as ISettingsHandler;
            if (handler == null) {
                Error.WriteLine("The application doesn't support settings.");
                return CommandResultCode.ExecutionFailed;
            }

            if (args.MoveNext())
                return CommandResultCode.SyntaxError;

            VarColumns[0].ResetWidth();
            VarColumns[1].ResetWidth();

            TableRenderer table = new TableRenderer(VarColumns, Out);
            table.EnableHeader = true;
            table.EnableFooter = true;
            foreach(KeyValuePair<string, string> setting in handler.Settings) {
                if (setting.Key == ApplicationSettings.SpecialLastCommand)
                    continue;

                ColumnValue[] row = new ColumnValue[4];
                row[0] = new ColumnValue(setting.Key);
                row[1] = new ColumnValue(setting.Value);
                table.AddRow(row);
            }

            table.CloseTable();
            Error.WriteLine();

            return CommandResultCode.Success;
        }
Пример #3
0
 public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
 {
     Columns[0].ResetWidth();
     Columns[1].ResetWidth();
     TableRenderer table = new TableRenderer(Columns, Out);
     foreach(KeyValuePair<string, string> alias in Application.Commands.Aliases) {
         ColumnValue[] row = new ColumnValue[2];
         row[0] = new ColumnValue(alias.Key);
         row[1] = new ColumnValue(alias.Value);
         table.AddRow(row);
     }
     table.CloseTable();
     return CommandResultCode.Success;
 }
Пример #4
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            Columns[0].ResetWidth();
            Columns[1].ResetWidth();
            TableRenderer table = new TableRenderer(Columns, Out);

            foreach (KeyValuePair <string, string> alias in Application.Commands.Aliases)
            {
                ColumnValue[] row = new ColumnValue[2];
                row[0] = new ColumnValue(alias.Key);
                row[1] = new ColumnValue(alias.Value);
                table.AddRow(row);
            }
            table.CloseTable();
            return(CommandResultCode.Success);
        }
Пример #5
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            IInformationProvider provider = Application as IInformationProvider;

            if (provider == null)
            {
                Error.WriteLine("The current context does not support information.");
                return(CommandResultCode.ExecutionFailed);
            }

            if (!args.MoveNext())
            {
                return(CommandResultCode.SyntaxError);
            }

            string infoName = args.Current;

            if (!provider.IsInfoSupported(infoName))
            {
                Error.WriteLine("Information " + infoName + " is not supported by the current context.");
                return(CommandResultCode.ExecutionFailed);
            }

            ColumnDesign[] columns = provider.GetColumns(infoName);
            for (int i = 0; i < columns.Length; i++)
            {
                columns[i].ResetWidth();
            }

            TableRenderer renderer = new TableRenderer(columns, Out);

            // TODO: make it configurable ...
            renderer.EnableHeader = true;
            renderer.EnableFooter = true;

            IList <ColumnValue[]> values = provider.GetValues(infoName);

            for (int i = 0; i < values.Count; i++)
            {
                ColumnValue[] rowValues = values[i];
                renderer.AddRow(rowValues);
            }

            renderer.Flush();
            renderer.CloseTable();
            return(CommandResultCode.Success);
        }
Пример #6
0
        private void ShowFree(NetworkContext context)
        {
            // Refresh
            context.Network.Refresh();

            MachineProfile[] machines = context.Network.GetAllMachineProfiles();
            if (machines.Length == 0)
            {
                Out.WriteLine("No machines in the network.");
            }
            else
            {
                ColumnDesign[] columns = new ColumnDesign[4];
                columns[0] = new ColumnDesign("Machine");
                columns[1] = new ColumnDesign("Used Memory");
                columns[2] = new ColumnDesign("Used Disk");
                columns[3] = new ColumnDesign("Notes");

                TableRenderer table = new TableRenderer(columns, Out);

                foreach (var machine in machines)
                {
                    ColumnValue[] row = new ColumnValue[4];
                    if (machine.IsError)
                    {
                        row[3] = new ColumnValue(" ERROR: " + machine.ErrorMessage);
                    }
                    else
                    {
                        row[0] = new ColumnValue(machine.ServiceAddress.ToString());
                        row[1] = new ColumnValue(MemoryReport(machine.MemoryUsed, machine.MemoryTotal));
                        row[2] = new ColumnValue(MemoryReport(machine.DiskUsed, machine.DiskTotal));
                        if (machine.DiskUsed > ((double)machine.DiskTotal * 0.85d))
                        {
                            row[3] = new ColumnValue(" WARNING: Node is close to full - used storage within 85% of total");
                        }
                    }

                    table.AddRow(row);
                }

                table.CloseTable();
            }
        }
Пример #7
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            ISettingsHandler handler = Application as ISettingsHandler;

            if (handler == null)
            {
                Error.WriteLine("The application doesn't support settings.");
                return(CommandResultCode.ExecutionFailed);
            }

            if (args.MoveNext())
            {
                return(CommandResultCode.SyntaxError);
            }

            VarColumns[0].ResetWidth();
            VarColumns[1].ResetWidth();

            TableRenderer table = new TableRenderer(VarColumns, Out);

            table.EnableHeader = true;
            table.EnableFooter = true;
            foreach (KeyValuePair <string, string> setting in handler.Settings)
            {
                if (setting.Key == ApplicationSettings.SpecialLastCommand)
                {
                    continue;
                }

                ColumnValue[] row = new ColumnValue[4];
                row[0] = new ColumnValue(setting.Key);
                row[1] = new ColumnValue(setting.Value);
                table.AddRow(row);
            }

            table.CloseTable();
            Error.WriteLine();

            return(CommandResultCode.Success);
        }
Пример #8
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            PropertyRegistry properties = Properties;

            if (properties == null)
            {
                Application.Error.WriteLine("the current context does not support properties.");
                return(CommandResultCode.ExecutionFailed);
            }

            if (args.MoveNext())
            {
                string         name   = args.Current;
                PropertyHolder holder = properties.GetProperty(name);
                if (holder == null)
                {
                    return(CommandResultCode.ExecutionFailed);
                }

                PrintDescription(name, holder, Application.Error);
                return(CommandResultCode.Success);
            }

            ProperiesColumns[0].ResetWidth();
            ProperiesColumns[1].ResetWidth();
            TableRenderer table = new TableRenderer(ProperiesColumns, Application.Out);

            foreach (KeyValuePair <string, PropertyHolder> entry in properties)
            {
                ColumnValue[]  row    = new ColumnValue[3];
                PropertyHolder holder = entry.Value;
                row[0] = new ColumnValue(entry.Key);
                row[1] = new ColumnValue(holder.Value);
                row[2] = new ColumnValue(holder.ShortDescription);
                table.AddRow(row);
            }
            table.CloseTable();
            return(CommandResultCode.Success);
        }
Пример #9
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            IInformationProvider provider = Application as IInformationProvider;
            if (provider == null) {
                Error.WriteLine("The current context does not support information.");
                return CommandResultCode.ExecutionFailed;
            }

            if (!args.MoveNext())
                return CommandResultCode.SyntaxError;

            string infoName = args.Current;
            if (!provider.IsInfoSupported(infoName)) {
                Error.WriteLine("Information " + infoName + " is not supported by the current context.");
                return CommandResultCode.ExecutionFailed;
            }

            ColumnDesign[] columns = provider.GetColumns(infoName);
            for (int i = 0; i < columns.Length; i++)
                columns[i].ResetWidth();

            TableRenderer renderer = new TableRenderer(columns, Out);
            // TODO: make it configurable ...
            renderer.EnableHeader = true;
            renderer.EnableFooter = true;

            IList<ColumnValue[]> values = provider.GetValues(infoName);
            for (int i = 0; i < values.Count; i++) {
                ColumnValue[] rowValues = values[i];
                renderer.AddRow(rowValues);
            }

            renderer.Flush();
            renderer.CloseTable();
            return CommandResultCode.Success;
        }
Пример #10
0
        private void ShowStatus(NetworkContext context)
        {
            ColumnDesign[] columns = new ColumnDesign[3];
            columns[0] = new ColumnDesign("Status");
            columns[1] = new ColumnDesign("Service");
            columns[2] = new ColumnDesign("Address");

            TableRenderer table = new TableRenderer(columns, Out);

            context.Network.Refresh();

            IDictionary <IServiceAddress, ServiceStatus> statusInfo = null;

            // Manager servers status,
            MachineProfile[] managers = context.Network.GetManagerServers();
            if (managers.Length > 0)
            {
                foreach (var manager in managers)
                {
                    ColumnValue[] row = new ColumnValue[3];
                    row[0] = new ColumnValue("UP");
                    row[1] = new ColumnValue("Manager");
                    row[2] = new ColumnValue(manager.ServiceAddress.ToString());

                    try {
                        statusInfo = context.Network.GetBlocksStatus();
                    } catch (NetworkAdminException e) {
                        Error.WriteLine("Error retrieving manager status info: " + e.Message);
                    }

                    table.AddRow(row);
                }
            }
            else
            {
                Error.WriteLine("! Manager server not available");
            }

            // Status of root servers
            MachineProfile[] roots = context.Network.GetRootServers();
            if (roots.Length == 0)
            {
                Out.WriteLine("! Root servers not available");
            }
            foreach (MachineProfile r in roots)
            {
                ColumnValue[] row = new ColumnValue[3];
                if (r.IsError)
                {
                    row[0] = new ColumnValue("DOWN");
                }
                else
                {
                    row[0] = new ColumnValue("UP");
                }

                row[1] = new ColumnValue("Root");
                row[2] = new ColumnValue(r.ServiceAddress.ToString());

                if (r.IsError)
                {
                    Out.Write("  ");
                    Out.WriteLine(r.ErrorMessage);
                }

                table.AddRow(row);
            }

            // The block servers we fetch from the map,
            List <IServiceAddress> blocks = new List <IServiceAddress>();

            if (statusInfo != null)
            {
                foreach (IServiceAddress s in statusInfo.Keys)
                {
                    blocks.Add(s);
                }
            }
            else
            {
                MachineProfile[] sblocks = context.Network.GetBlockServers();
                foreach (MachineProfile b in sblocks)
                {
                    blocks.Add(b.ServiceAddress);
                }
            }

            blocks.Sort();

            if (blocks.Count == 0)
            {
                Out.WriteLine("! Block servers not available");
            }

            foreach (IServiceAddress b in blocks)
            {
                ColumnValue[] row = new ColumnValue[3];

                if (statusInfo != null)
                {
                    ServiceStatus status = statusInfo[b];
                    if (status == ServiceStatus.Up)
                    {
                        // Manager reported up
                        row[0] = new ColumnValue("UP");
                    }
                    else if (status == ServiceStatus.DownClientReport)
                    {
                        // Manager reported down from client report of error
                        row[0] = new ColumnValue("D-CR");
                    }
                    else if (status == ServiceStatus.DownHeartbeat)
                    {
                        // Manager reported down from heart beat check on the server
                        row[0] = new ColumnValue("D-HB");
                    }
                    else if (status == ServiceStatus.DownShutdown)
                    {
                        // Manager reported down from shut down request
                        row[0] = new ColumnValue("D-SD");
                    }
                    else
                    {
                        row[0] = new ColumnValue("?ERR");
                    }
                }
                else
                {
                    // Try and get status from machine profile
                    MachineProfile r = context.Network.GetMachineProfile(b);
                    if (r.IsError)
                    {
                        row[0] = new ColumnValue("DOWN");
                    }
                    else
                    {
                        row[0] = new ColumnValue("UP");
                    }
                }

                row[1] = new ColumnValue("Block");
                row[2] = new ColumnValue(b.ToString());

                table.AddRow(row);
            }

            table.CloseTable();
        }
Пример #11
0
        private void ShowNetwork(NetworkContext context)
        {
            int managerCount = 0;
            int rootCount    = 0;
            int blockCount   = 0;

            ColumnDesign[] columns = new ColumnDesign[2];
            columns[0]       = new ColumnDesign("MRB");
            columns[0].Width = 20;
            columns[1]       = new ColumnDesign("Address");

            TableRenderer table = new TableRenderer(columns, Out);

            context.Network.Refresh();

            MachineProfile[] profiles = context.Network.GetAllMachineProfiles();

            foreach (MachineProfile p in profiles)
            {
                if (p.IsError)
                {
                    ColumnValue[] row = new ColumnValue[2];
                    row[0] = new ColumnValue(p.ErrorMessage);
                    row[1] = new ColumnValue(p.ServiceAddress.ToString());
                    table.AddRow(row);
                }
                else
                {
                    string mrb = String.Empty;
                    mrb += p.IsManager ? "M" : ".";
                    mrb += p.IsRoot ? "R" : ".";
                    mrb += p.IsBlock ? "B" : ".";

                    ColumnValue[] row = new ColumnValue[2];
                    row[0] = new ColumnValue(mrb);
                    row[1] = new ColumnValue(p.ServiceAddress.ToString());
                    table.AddRow(row);

                    managerCount += p.IsManager ? 1 : 0;
                    rootCount    += p.IsRoot ? 1 : 0;
                    blockCount   += p.IsBlock ? 1 : 0;
                }
            }

            table.CloseTable();

            Out.WriteLine();
            if (profiles.Length == 1)
            {
                Out.WriteLine("one machine in the network.");
            }
            else
            {
                Out.WriteLine(profiles.Length + " machines in the network.");
            }

            if (managerCount == 0)
            {
                Out.Write("none manager");
            }
            else if (managerCount == 1)
            {
                Out.Write("one manager");
            }
            else
            {
                Out.Write(managerCount + " managers");
            }

            Out.Write(", ");

            if (rootCount == 0)
            {
                Out.Write("none root");
            }
            else if (rootCount == 1)
            {
                Out.Write("one root");
            }
            else
            {
                Out.Write(rootCount + " roots");
            }

            Out.Write(", ");

            if (blockCount == 0)
            {
                Out.Write("none block");
            }
            else if (blockCount == 1)
            {
                Out.Write("one block");
            }
            else
            {
                Out.Write(blockCount + " blocks");
            }

            Out.WriteLine();
        }
Пример #12
0
        private async Task RespondWithRule(CommandContext context, Entry resultEntry)
        {
            _logger.LogInformation("Responding to the rule request, {@resultEntry}", resultEntry);

            var builder = new DiscordEmbedBuilder();

            builder.WithColor(GetColor(resultEntry.Type));
            builder.WithTitle(resultEntry.Name);
            builder.WithFooter($"{resultEntry.Source} pg.{resultEntry.Page}");

            builder.WithDescription(resultEntry.Description.TruncateForDisplay(2048, "(more)"));

            if (resultEntry.Examples != null && resultEntry.Examples.Length > 0)
            {
                var sb = new StringBuilder();
                foreach (string example in resultEntry.Examples)
                {
                    sb.AppendLine($"> {example}");
                }

                builder.AddField("Examples", sb.ToString());
            }

            if (!string.IsNullOrWhiteSpace(resultEntry.Note))
            {
                builder.AddField("Note", resultEntry.Note);
            }
            if (!string.IsNullOrWhiteSpace(resultEntry.Calling))
            {
                builder.AddField("Calling", resultEntry.Calling);
            }
            if (!string.IsNullOrWhiteSpace(resultEntry.InnatePower))
            {
                builder.AddField("Innate Power", resultEntry.InnatePower);
            }

            DiscordEmbed response = builder.Build();

            await context.RespondAsync(embed : response);

            _logger.LogInformation("Building tables");

            foreach (Table table in resultEntry.Tables)
            {
                var tableBuilder = new StringBuilder();
                var renderer     = new TableRenderer();

                if (table.Headers != null)
                {
                    renderer.SetHeader(table.Headers);
                }

                foreach (Row row in table.Rows)
                {
                    renderer.AddRow(row.Columns);
                }

                tableBuilder.AppendLine();
                tableBuilder.AppendLine(Formatter.BlockCode(renderer.Build()));
                await context.RespondAsync(tableBuilder.ToString());
            }
        }
 private void AddRow(TableRenderer table, Product item)
 {
     table.AddRow(item.ID.ToString(), item.Name.First().Name,
                  item.Energy.ToString(), item.Proteins.ToString(),
                  item.Fats.ToString(), item.Carbohydrates.ToString());
 }