import 'package:dart_framework/utils/system_util.dart';
import 'package:oto/oto/application.dart';
import 'package:oto/oto/commands/command.dart';
import 'package:oto/oto/data/command_data.dart';

class Docker extends Command {
  @override
  Future execute(DataCommand command) async {
    DataDocker data = DataDocker.fromJson(getParam(command));
    String dockerCommand = data.command;
    List<String> args = data.args;

    StringBuffer shell = StringBuffer(getCDPath(workspace));
    shell.write(' $and docker $dockerCommand');

    for (String arg in args) {
      shell.write(' $arg');
    }
    var process =
        await runtime.start(shell, logHandler: Application.logWithType);

    return await completeProcess(process, command);
  }
}

void registerDockerCommand() {
  Command.register(
    CommandType.Docker,
    () => Docker(),
    spec: const CommandSpec(
      type: CommandType.Docker,
      category: 'docker',
      dataModel: 'DataDocker',
      samplePath: 'assets/yaml/sample/16_docker.yaml',
    ),
  );
}
