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() {
  var results = argResults!;

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

  var overwrite = results['overwrite'] as bool?;

  var dockerPath = findDockerfile();

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

  if (imageName == null) {
    var repo = ask('ImageName:', defaultValue: 'noojee/nginx-le');
    print('Current version: $packageVersion');
    var version = ask('Version:',
        defaultValue: packageVersion, validator: Ask.required);
    imageName = '$repo:$version';
  }

  // check for an existing image.
  var image = Images().findByFullname(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);
    }
  }
  var pulldcli = results['update-dcli'] as bool;

  /// force dcli to pull the latest version.
  if (pulldcli) {
    replace(join(dockerPath, 'Dockerfile'), RegExp('# flush-cache:.*'),
        '# flush-cache: ${Uuid().v4()}');
    'update-dcli.txt'.write(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 .'.start(workingDirectory: dockerPath);

  /// get the new image.
  Images().flushCache();
  image = Images().findByFullname(imageName);
  var config = ConfigYaml();
  config.image = image;
  config.save();

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