Example #1
0
        public async Task ExecuteAsync(Parameter parameter)
        {
            if (!IsInitSuccess)
            {
                return;
            }

            if (parameter.Parameters.Length > MaxParameterCount)
            {
                ShellOut.Error("Too many arguments.");
                return;
            }

            if (parameter.Parameters == null || parameter.Parameters.Length <= 0)
            {
                return;
            }

            await Sync.WaitAsync().ConfigureAwait(false);

            try {
                if (OnExecuteFunc != null)
                {
                    if (OnExecuteFunc.Invoke(parameter))
                    {
                        return;
                    }
                }

                switch (parameter.ParameterCount)
                {
                case 1 when !string.IsNullOrEmpty(parameter.Parameters[0].Trim()):
                    string bashScriptPath = parameter.Parameters[0].Trim();

                    if (!File.Exists(bashScriptPath))
                    {
                        ShellOut.Error($"{bashScriptPath} doesn't exist.");
                        return;
                    }

                    ShellOut.Info($"Executing {bashScriptPath} ...");
                    ShellOut.Info(bashScriptPath.ExecuteBash(false));
                    return;

                default:
                    ShellOut.Error("Command seems to be in incorrect syntax.");
                    return;
                }
            }
            catch (Exception e) {
                ShellOut.Exception(e);
                return;
            }
            finally {
                Sync.Release();
            }
        }
Example #2
0
        public async Task ExecuteAsync(Parameter parameter)
        {
            if (!IsInitSuccess)
            {
                return;
            }

            if (parameter.Parameters.Length > MaxParameterCount)
            {
                ShellIO.Error("Too many arguments.");
                return;
            }

            await Sync.WaitAsync().ConfigureAwait(false);

            try {
                if (OnExecuteFunc != null)
                {
                    if (OnExecuteFunc.Invoke(parameter))
                    {
                        return;
                    }
                }

                switch (parameter.ParameterCount)
                {
                case 0:
                    foreach (KeyValuePair <string, IShellCommand> cmd in Interpreter.Commands)
                    {
                        if (string.IsNullOrEmpty(cmd.Value.CommandKey) || string.IsNullOrEmpty(cmd.Value.CommandName))
                        {
                            continue;
                        }

                        cmd.Value.OnHelpExec(true);
                    }
                    return;

                case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]) && parameter.Parameters[0].Equals("all", StringComparison.OrdinalIgnoreCase):
                    PrintAll();
                    return;

                case 1 when !string.IsNullOrEmpty(parameter.Parameters[0]):
                    IShellCommand shellCmd = await Interpreter.Init.GetCommandWithKeyAsync <IShellCommand>(parameter.Parameters[0]).ConfigureAwait(false);

                    if (shellCmd == null)
                    {
                        ShellIO.Error("Command doesn't exist. use ' help -all ' to check all available commands!");
                        return;
                    }

                    shellCmd.OnHelpExec(false);
                    return;

                default:
                    ShellIO.Error("Command seems to be in incorrect syntax.");
                    return;
                }
            }
            catch (Exception e) {
                ShellIO.Exception(e);
                return;
            }
            finally {
                Sync.Release();
            }
        }