Skip to content

EomTaeWook/Dignus.Commands

Repository files navigation

Dignus.Commands

NuGet

Lightweight command execution system for local console and telnet remote control.

간단한 로컬 콘솔 명령 실행텔넷 기반 원격 명령 실행을 지원하는 커맨드 시스템입니다.

picture


Features / 특징

English

  • Attribute based command registration
  • Delegate based command registration
  • Async command execution
  • CancellationToken based cancellation
  • Middleware pipeline support
  • Local console command runner
  • Telnet remote command runner

Korean

  • Attribute 기반 커맨드 등록
  • Delegate 기반 커맨드 등록
  • 비동기 커맨드 실행
  • CancellationToken 기반 취소 지원
  • 미들웨어 파이프라인 지원
  • 로컬 콘솔 실행 지원
  • 텔넷 기반 원격 명령 실행

Quick Start

using Dignus.Commands;

var module = new LocalCommandRunner();

module.AddCommand<Close>();

module.Build();
module.Run();

Local Command Runner

Run commands directly from the local console.

로컬 콘솔에서 직접 명령을 입력받아 실행합니다.

using Dignus.Commands;

var module = new LocalCommandRunner();

module.AddCommand<Close>();

module.Build();
module.Run();

Telnet Command Runner

Start a telnet server and execute commands remotely.

텔넷 서버를 열고 외부 텔넷 클라이언트에서 접속하여 명령을 실행합니다.

using Dignus.Commands;

var module = new TelnetCommandRunner(port: 50000);

module.AddCommand<Close>();

module.Build();
module.Run();

Connect via telnet:

telnet 127.0.0.1 50000

Note

There is no dedicated telnet client module.
You connect using any telnet client.


Command Class

Create a command by implementing ICommand.

using Dignus.Commands.Attributes;
using Dignus.Commands.Interfaces;
using Dignus.Actor.Core;
using System.Diagnostics;

[Command("close")]
internal class Close : ICommand
{
    public Task InvokeAsync(string[] args, IActorRef sender, CancellationToken cancellationToken)
    {
        Process.GetCurrentProcess().Close();
        return Task.CompletedTask;
    }

    public string Print()
    {
        return "close process";
    }
}

Register the command

module.AddCommand<Close>();

Delegate Command

Commands can also be registered using delegates.

module.AddCommand("loop", "loop example", TestAsync);

async Task TestAsync(string[] args, IActorRef sender, CancellationToken cancellationToken)
{
    var count = 0;

    while (cancellationToken.IsCancellationRequested == false)
    {
        sender.Post(new CommandResponseMessage()
        {
            Content = $"sleep : {count++}"
        });

        await Task.Delay(2000, cancellationToken);
    }

    sender.Post(new CommandResponseMessage()
    {
        Content = $"end sleep : {count++}"
    });
}

Middleware

You can add middleware to the command execution pipeline.

module.AddMiddleware((ref CommandPipelineContext context, ref AsyncPipelineNext<CommandPipelineContext> next) =>
{
    var auth = context.Command.GetType().GetCustomAttribute<AuthAttribute>();

    if (auth.Execute(context) == false)
    {
        context.SenderActorRef.Post(new CommandResponseMessage()
        {
            Content = "invalid auth"
        });

        return Task.CompletedTask;
    }

    return next.InvokeAsync(ref context);
});

Command Execution Flow

User Input
     │
     ▼
Command Parser
     │
     ▼
Middleware Pipeline
     │
     ▼
Command Execution
     │
     ▼
CommandResponseMessage

Cancellation

Long running commands should support cancellation.

while (cancellationToken.IsCancellationRequested == false)
{
    await Task.Delay(1000, cancellationToken);
}

If a telnet connection closes, a CancelCommandMessage will be sent.


Output Message

Send output using CommandResponseMessage.

sender.Post(new CommandResponseMessage()
{
    Content = "hello"
});

Default Commands

Command Description
? show command list
Ctrl + C cancel running command

Module Lifecycle

var module = new LocalCommandRunner();

module.AddCommand<Close>();

module.Build();
module.Run();

Execution order

  1. Create module
  2. Register commands
  3. Register middleware
  4. Build()
  5. Run()

Run() must be called after Build().


About

A robust remote command execution and terminal management system built on the Dignus Actor Model framework.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages