Skip to content

Commit

Permalink
Merge pull request #35 from creative-commoners/pulls/1.1/tx-token
Browse files Browse the repository at this point in the history
FIX Manually read token from .transifexrc
  • Loading branch information
GuySartorelli authored Nov 5, 2024
2 parents 72ae09b + 3f1412e commit 64494cd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"symfony/process": "^4 || ^5 || ^6",
"symfony/yaml": "^4 || ^5 || ^6",
"symfony/dotenv": "^5 || ^6",
"symfony/filesystem": "^5 || ^6",
"guzzlehttp/guzzle": "^6 || ^7"
},
"conflict": {
Expand Down
30 changes: 29 additions & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RuntimeException;
use LogicException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Filesystem\Path;

class Translator
{
Expand Down Expand Up @@ -46,6 +47,8 @@ class Translator

private ?OutputFormatter $outputFormatter = null;

private string $txToken = '';

public function run()
{
$this->outputFormatter = new OutputFormatter(true);
Expand Down Expand Up @@ -80,6 +83,7 @@ public function run()

private function checkEnv(): void
{
$this->readTxToken();
$txInstructions = 'Install the new go version of the client https://developers.transifex.com/docs/cli';
try {
$this->exec('which tx');
Expand Down Expand Up @@ -618,7 +622,13 @@ private function getModuleName(string $modulePath): string

private function exec(string $command, ?string $cwd = null): string
{
$this->log("Running $command");
if (str_starts_with($command, 'tx ')) {
$token = $this->txToken;
$command = "TX_TOKEN=$token $command";
}
$message = "Running $command";
$message = preg_replace('/TX_TOKEN=.+? tx/', 'TX_TOKEN=**** tx', $message);
$this->log($message);
$process = Process::fromShellCommandline($command, $cwd);
// set a timeout of 5 minutes - tx pull operations may take a long time
$process->setTimeout(300);
Expand Down Expand Up @@ -837,4 +847,22 @@ private function recursiveKeySort(array &$arr): void
}
}
}

/**
* The tx binary doesn't appear to correctly read from ~/.transifexrc, even though it will correctly
* write to it after prompting for it if it's missing. This is a workaround to read the token and
* use it a TX_TOKEN environment variable, which the tx binary will use.
*/
private function readTxToken(): void
{
$path = Path::canonicalize('~/.transifexrc');
if (!file_exists($path)) {
throw new RuntimeException("$path does not exist");
}
$contents = file_get_contents($path);
if (!preg_match('/\ntoken *= *(.+)(\n|$)/', $contents, $matches)) {
throw new RuntimeException("Could not get a valid token from $path");
}
$this->txToken = $matches[1];
}
}

0 comments on commit 64494cd

Please sign in to comment.