run method

  1. @override
void run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
void run() {
  final results = argResults!;

  final debug = argResults!['debug'] as bool;
  Settings().setVerbose(enabled: debug);

  final overwrite = results['overwrite'] as bool;

  final dockerPath = findDockerfile();

  var imageName = argResults!['image'] as String?;

  if (imageName == null) {
    final repo = ask('ImageName:', defaultValue: 'onepub/nginx-le');
    final currentVersion = DartProject.self.pubSpec.version!.toString();
    print('Current version: $currentVersion');
    final version = ask('Version:',
        defaultValue: currentVersion, validator: Ask.required);
    imageName = '$repo:$version';
  }

  final latest = genLatestTag(imageName);

  // check for an existing image.
  var image = Images().findByName(imageName);
  if (image != null) {
    if (!overwrite) {
      printerr(
          'The image $imageName already exists. Choose a different name or '
          'use --overwrite to replace it.');
      showUsage(argParser);
    } else {
      /// delete the image an all its associated containers.
      deleteImage(image);
    }
  }
  final pulldcli = results['update-dcli'] as bool;

  /// force dcli to pull the latest version.
  if (pulldcli) {
    replace(join(dockerPath, 'Dockerfile'), RegExp('# flush-cache:.*'),
        '# flush-cache: ${const Uuid().v4()}');
    'update-dcli.txt'.write(const Uuid().v4());
  }

  // make certain we always have the latest source
  replace(join(dockerPath, 'Dockerfile'), RegExp('# update-source:.*'),
      '# update-source: ${const Uuid().v4()}');
  'update-source.txt'.write(const Uuid().v4());

  print(green('Building nginx-le docker image $imageName '));

  // get the latest ubuntu image before we build.
  Images().pull(fullname: 'ubuntu:20.04');

  /// required to give docker access to our ssh keys.
  'docker build -t $imageName -t $latest .'
      .start(workingDirectory: dockerPath);

  'docker push $imageName'.run;
  'docker push $latest'.run;

  /// get the new image.
  image = Images().findByName(imageName);
  ConfigYaml()
    ..image = image
    ..save();

  print('');
  print(green(
      "Build Complete. You should now run 'nginx-le config' to reconfigure "
      'your system to use the new image'));
}