code: static void waitForPipelineToFinish(PipelineResult result) {
      try {
        // Check to see if we are creating a template.
        // This should throw {@link UnsupportedOperationException} when creating a template.
        result.getState();
  
        State state = result.waitUntilFinish();
        LOG.info("Job finished with state: " + state.name());
        if (state != State.DONE) {
          System.exit(1);
        }
      } catch (UnsupportedOperationException e) {
        LOG.warn("Unable to wait for pipeline to finish.", e);
      }
    }
docs: Wait for the pipeline to finish if we are not creating a template. Exit with error if the
  pipeline finishes, but not in {@link State#DONE} state. Log a warning if creating a template.
  
  @param result