setName("git:ignore") ->setDescription("List, add or remove paths from gits ignorelists") ->addOption("local","l",InputOption::VALUE_NONE,"Use the local ignore rather than .gitignore") ->addOption("add","a",InputOption::VALUE_NONE,"Add a pattern") ->addOption("remove","r",InputOption::VALUE_NONE,"Attempt to remove a pattern") ->addArgument("pattern", InputArgument::OPTIONAL, "Pattern to add or remove") ; } protected function execute(InputInterface $input, OutputInterface $output) { $local = $input->getOption("local"); $root = $this->getEnvironment()->getProjectDirectory(); $file = $root . (!$local ? "/.gitignore" : "/.git/info/exclude"); $pattern = $input->getArgument("pattern"); if (empty($pattern)) { if (file_exists($file)) { $ignores = file($file, FILE_IGNORE_NEW_LINES); foreach ($ignores as $ignore) { if (str_starts_with(trim($ignore),'#')) { $output->writeln("".$ignore.""); } else { $output->writeln("".$ignore.""); } } return Command::SUCCESS; } $output->writeln("Empty list"); return Command::SUCCESS; } elseif ($input->getOption("add") && $pattern) { if (file_exists($file)) { $ignores = file($file, FILE_IGNORE_NEW_LINES); } else { $ignores = []; } array_push($ignores, $pattern); file_put_contents($file, join("\n", $ignores)); $output->writeln("Updated {$file}"); return Command::SUCCESS; } elseif ($input->getOption("remove") && $pattern) { if (file_exists($file)) { $ignores = file($file, FILE_IGNORE_NEW_LINES); $ignores = array_filter($ignores, function ($v) use ($pattern) { return $v != $pattern; }); $output->writeln("Updated {$file}"); file_put_contents($file, join("\n", $ignores)); return Command::SUCCESS; } $output->writeln("Not updating non-existing file {$file}"); return Command::SUCCESS; } $output->writeln("Expected no pattern, --add pattern or --remove pattern"); return Command::INVALID; } }