Skip to content

Commit

Permalink
Merge pull request #52 from KiraCore/feature/general_executor
Browse files Browse the repository at this point in the history
Feature/general executor
  • Loading branch information
PeepoFrog authored Jun 25, 2024
2 parents 305508f + 714c8da commit b6a5cd4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/shidai/internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
"status": handleStatusCommand,
"start": handleStartComamnd,
"tx": handleTxCommand,
"sekaid": handleSekaidCommand,
}
)

Expand All @@ -70,6 +71,42 @@ func ExecuteCommandHandler(c *gin.Context) {
}

// [COMMANDS] //
func handleSekaidCommand(args map[string]interface{}) (string, error) {
execInterface, ok := args["exec"].([]interface{})
if !ok {
return "", fmt.Errorf("type assertion to []interface{} failed for args[\"exec\"]")
}

var cmd []string
for _, v := range execInterface {
str, ok := v.(string)
if !ok {
return "", fmt.Errorf("type assertion to string failed for element in exec")
}
cmd = append(cmd, str)
}

cm, err := docker.NewContainerManager()
if err != nil {
log.Error("Failed to initialize Docker API", zap.Error(err))
return "", fmt.Errorf("failed to initialize docker API: %w", err)
}

ctx := context.Background()
containerID := types.SEKAI_CONTAINER_ID

var out []byte = []byte{}
out, err = cm.ExecInContainer(ctx, containerID, cmd)
if err != nil {
log.Error("Failed to execute transaction command", zap.Strings("command", cmd), zap.Error(err))
return "", fmt.Errorf("failed to execute transaction command: %w", err)
}

log.Debug("Container output: ", zap.String("out", string(out)))
log.Info("Transaction command executed successfully", zap.Strings("command", cmd))
return string(out), nil

}
func handleTxCommand(args map[string]interface{}) (string, error) {
cmd, ok := args["tx"].(string)
if !ok {
Expand Down
1 change: 1 addition & 0 deletions src/shidai/internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (cm *ContainerManager) ExecInContainer(ctx context.Context, containerID str

if len(errBuf.Bytes()) > 0 {
log.Warn("Standard error output from container exec", zap.ByteString("stderr", errBuf.Bytes()))
return nil, fmt.Errorf(errBuf.String())
}

output := outBuf.Bytes()
Expand Down

0 comments on commit b6a5cd4

Please sign in to comment.