<?php

namespace App\Services;

class Docker
{

    public static function base(?string $command = null): string
    {
        $commandString = $command ? ' ' . $command : '';

        return sprintf('docker compose -f %s%s', config('project.compose_file'), $commandString);
    }

    public static function exec(?string $command = null, string $cwd = null): string
    {
        $base = self::base();
        $dockerCwd = $cwd ?? config('project.dockerCwd');
        $user = config('project.dockerUser');
        $serviceName = config('project.serviceName');
        $shell = config('project.shell');
        $commandString = $command ? '-c "' . $command . '"' : '';

        return sprintf('%s exec -w %s --user %s %s %s %s',
            $base,
            $dockerCwd,
            $user,
            $serviceName,
            $shell,
            $commandString
        );
    }

    public static function bin(string $name): string
    {
        return self::exec(sprintf('./bin/%s.sh', $name));

    }

}