run method

  1. @override
Future<void> run()
override

Runs the command to create a new task and update the config file.

Implementation

@override
Future<void> run() async {
  final configPath = globalResults?["config"] ?? "distribution.yaml";
  final file = File(configPath);
  if (!file.existsSync()) {
    logger.logError("Configuration file not found: $configPath");
    return;
  }

  final configJson = _loadYamlAsJson(file);
  configJson["tasks"] ??= [];
  final tasks = configJson["tasks"] as List<dynamic>;

  String? taskKey;
  String? taskName;
  String? taskDescription;

  bool isWizard = argResults?["wizard"] ?? false;

  if (isWizard) {
    logger.logInfo("Starting task creation wizard...");
    logger.logInfo("Available tasks:");
    for (var task in tasks) {
      logger.logInfo("- ${task["name"]} (Key: ${task["key"]})");
    }
    logger.logEmpty();
    logger.logInfo("Please provide the following details:");
    taskName = await prompt("Enter new task name");
    taskKey = await prompt("Enter new task key");
    taskDescription = await prompt(
      "Enter new task description",
      nullable: true,
    );
  } else {
    taskKey = argResults?["key"];
    taskName = argResults?["name"];
    taskDescription = argResults?["description"];
  }

  if ((taskKey?.isEmpty ?? true) || (taskName?.isEmpty ?? true)) {
    logger.logError("`key` and `name` are mandatory.");
    return;
  }

  if (tasks.any((task) => task["key"] == taskKey)) {
    logger.logError("Task with key $taskKey already exists.");
    return;
  }

  tasks.add(
    Task(
      name: taskName!,
      key: taskKey!,
      description: taskDescription,
      workflows: [],
      jobs: [],
    ).toJson(),
  );

  configJson["tasks"] = tasks;
  await _writeYaml(file, configJson);
  logger.logSuccess("Task created successfully: $taskName");
}