private async Task Run(Options options, string userAgent)
        {
            var file = !string.IsNullOrEmpty(options.ScriptFile)
                ? LoadScriptFile(options.ScriptFile)
                : null;

            ConsoleHost = CreateSystemConsoleHost(options, file);

            var verbose = file == null || options.VerboseMode;

            var baseUrl = GetBaseUrl(options);

            if (verbose)
            {
                ConsoleHost.Write($"Loading from swagger endpoint '{options.SwaggerEndpoint}'...");
            }

            var commandParser = new CommandParser();
            var variables     = new InMemoryVariables();

            var watch = Stopwatch.StartNew();

            using var client = CreateHttpClient(baseUrl, userAgent);

            var classes = await CreateSwaggerClasses(client, options);

            if (verbose)
            {
                ConsoleHost.WriteLine($" Done ({watch.Elapsed.TotalMilliseconds:N0}ms).");
            }

            ConsoleHost.WriteLine();

            var commandProcessor = new CommandProcessor(commandParser, variables, classes, client);

            if (file != null)
            {
                if (verbose)
                {
                    ConsoleHost.Write("Running script... ");
                }

                var consoleHost = options.PrintScriptResponses
                    ? ConsoleHost
                    : new NullConsoleHost();
                await new ScriptedShell(consoleHost, commandParser, variables, commandProcessor, file).Run();

                if (verbose)
                {
                    ConsoleHost.WriteLine(" Done.");
                }

                ConsoleHost.WriteLine();
            }

            if (!options.RunInteractiveShell)
            {
                await new InteractiveShell(ConsoleHost, commandParser, variables, commandProcessor).Run();
            }
        }
Exemple #2
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        const int   bufferSize = 1024;
        Span <byte> buffer     = stackalloc byte[bufferSize];

        foreach (var path in args.Skip(1))
        {
            if (!File.Exists(path))
            {
                consoleHost.WriteLine($"cat: {path}: No such file or directory"); continue;
            }
            if (Directory.Exists(path))
            {
                consoleHost.WriteLine($"cat: {path}: Is a directory"); continue;
            }
            using var stream = File.OpenRead(path);
            for (; ;)
            {
                var cbRead = stream.Read(buffer);
                if (cbRead == 0)
                {
                    break;
                }
                var decodedText = Encoding.UTF8.GetString(buffer.Slice(0, cbRead));
                consoleHost.Write(decodedText);
                if (cbRead < bufferSize)
                {
                    break;
                }
            }
        }
    }
Exemple #3
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        var assembly    = this.GetType().Assembly;
        var version     = assembly.GetName().Version;
        var productName = assembly.GetCustomAttribute <AssemblyProductAttribute>().Product;
        var copyright   = assembly.GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright;

        consoleHost.WriteLine($"{Yellow(productName)}");
        consoleHost.WriteLine($"{Cyan("Version")}   - {version}");
        consoleHost.WriteLine($"{Cyan("Copyright")} - {copyright}");
        consoleHost.WriteLine($"{Cyan("3rd party notice")} - {DarkCyan("[follow this link](https://github.com/jsakamoto/jsakamoto.github.io/blob/master/THIRD-PARTY-NOTICES.txt)")}");

        consoleHost.WriteLine();
        consoleHost.WriteLine(Cyan("Special Thanks to") + " -");
        consoleHost.Write("This project has been started after being inspired by [@AtriaSoft](https://twitter.com/AtriaSoft)'s ");
        consoleHost.WriteLine("[\"CUIPortfolio\"](https://github.com/Atria64/CUIPortfolio) project.");
    }