// Path: sample/src/main/java/com/google/fleetengine/auth/sample/validation/SampleScriptException.java
// public class SampleScriptException extends Exception {
// 
//   /**
//    * Constructs a {@code SampleScriptException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    */
//   public SampleScriptException(String message) {
//     this(message, null);
//   }
// 
//   /**
//    * Constructs a {@code SampleScriptException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method).
//    *     (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
//    */
//   public SampleScriptException(String message, Throwable cause) {
//     super(message, cause);
//   }
// }
// 
// Path: sample/src/main/java/com/google/fleetengine/auth/sample/validation/SampleScriptRuntime.java
// public interface SampleScriptRuntime {
// 
//   /**
//    * Run a command that can throw an exception.
//    *
//    * @param commandName human readable name that describes the command
//    * @param command runnable command
//    * @throws Throwable signals that the command failed
//    */
//   void runCommand(String commandName, ThrowingRunnable command) throws Throwable;
// 
//   /**
//    * Expects that {@code command} throws {@link io.grpc.StatusRuntimeException} with code {@code
//    * io.grpc.Status.PERMISSION_DENIED}. If that exception is thrown but with a different code, then
//    * {@link SampleScriptException} is thrown.
//    *
//    * @param commandName human readable name that describes the command
//    * @param command runnable command
//    * @throws Throwable signals that the command failed
//    */
//   void expectPermissionDenied(String commandName, ThrowingRunnable command) throws Throwable;
// }
// 
// Path: sample/src/main/java/com/google/fleetengine/auth/sample/validation/ThrowingRunnable.java
// public interface ThrowingRunnable {
//   /** Runs code. */
//   void run() throws Throwable;
// }


import static io.grpc.Status.PERMISSION_DENIED;
import com.google.fleetengine.auth.sample.validation.SampleScriptException;
import com.google.fleetengine.auth.sample.validation.SampleScriptRuntime;
import com.google.fleetengine.auth.sample.validation.ThrowingRunnable;
import io.grpc.StatusRuntimeException;
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fleetengine.auth.sample;



/**
 * Create runtime for a command line application.
 *
 * <p>Success and Failure messages are displayed within the terminal.
 */
final class CommandLineRuntime implements SampleScriptRuntime {
  private static final String EXPECTED_PERMISSION_DENIED_MESSAGE =
      "Expected StatusRuntimeException with code PERMISSION_DENIED.";
  private static final String ANSI_RESET = "\u001B[0m";
  private static final String ANSI_RED = "\u001B[31m";
  private static final String ANSI_GREEN = "\u001B[32m";
  private static final String ANSI_YELLOW = "\033[0;33m";

  /** Create runtime. */
  public static SampleScriptRuntime create() {
    return new CommandLineRuntime();
  }

  private CommandLineRuntime() {}

  /**
   * Runs a given command and displays whether or not it was successful. A failure is communicated
   * by the command throwing an Exception.
   *
   * @param commandName displayed immediately before the {@code command} is run
   * @param command command to run
   * @throws Throwable signals that the {@code command} failed
   */
  @Override
  public void runCommand(String commandName, ThrowingRunnable command) throws Throwable {
    System.out.printf("%s... ", commandName);
    try {
      command.run();
    } catch (Exception exception) {
      System.out.printf("%sFAIL!%s\n", ANSI_RED, ANSI_RESET);
      throw exception;
    }
// Path: sample/src/main/java/com/google/fleetengine/auth/sample/validation/SampleScriptException.java
// public class SampleScriptException extends Exception {
// 
//   /**
//    * Constructs a {@code SampleScriptException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    */
//   public SampleScriptException(String message) {
//     this(message, null);
//   }
// 
//   /**
//    * Constructs a {@code SampleScriptException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method).
//    *     (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
//    */
//   public SampleScriptException(String message, Throwable cause) {
//     super(message, cause);
//   }
// }
// 
// Path: sample/src/main/java/com/google/fleetengine/auth/sample/validation/SampleScriptRuntime.java
// public interface SampleScriptRuntime {
// 
//   /**
//    * Run a command that can throw an exception.
//    *
//    * @param commandName human readable name that describes the command
//    * @param command runnable command
//    * @throws Throwable signals that the command failed
//    */
//   void runCommand(String commandName, ThrowingRunnable command) throws Throwable;
// 
//   /**
//    * Expects that {@code command} throws {@link io.grpc.StatusRuntimeException} with code {@code
//    * io.grpc.Status.PERMISSION_DENIED}. If that exception is thrown but with a different code, then
//    * {@link SampleScriptException} is thrown.
//    *
//    * @param commandName human readable name that describes the command
//    * @param command runnable command
//    * @throws Throwable signals that the command failed
//    */
//   void expectPermissionDenied(String commandName, ThrowingRunnable command) throws Throwable;
// }
// 
// Path: sample/src/main/java/com/google/fleetengine/auth/sample/validation/ThrowingRunnable.java
// public interface ThrowingRunnable {
//   /** Runs code. */
//   void run() throws Throwable;
// }


// Path: sample/src/main/java/com/google/fleetengine/auth/sample/CommandLineRuntime.java
import static io.grpc.Status.PERMISSION_DENIED;
import com.google.fleetengine.auth.sample.validation.SampleScriptException;
import com.google.fleetengine.auth.sample.validation.SampleScriptRuntime;
import com.google.fleetengine.auth.sample.validation.ThrowingRunnable;
import io.grpc.StatusRuntimeException;

// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fleetengine.auth.sample;



/**
 * Create runtime for a command line application.
 *
 * <p>Success and Failure messages are displayed within the terminal.
 */
final class CommandLineRuntime implements SampleScriptRuntime {
  private static final String EXPECTED_PERMISSION_DENIED_MESSAGE =
      "Expected StatusRuntimeException with code PERMISSION_DENIED.";
  private static final String ANSI_RESET = "\u001B[0m";
  private static final String ANSI_RED = "\u001B[31m";
  private static final String ANSI_GREEN = "\u001B[32m";
  private static final String ANSI_YELLOW = "\033[0;33m";

  /** Create runtime. */
  public static SampleScriptRuntime create() {
    return new CommandLineRuntime();
  }

  private CommandLineRuntime() {}

  /**
   * Runs a given command and displays whether or not it was successful. A failure is communicated
   * by the command throwing an Exception.
   *
   * @param commandName displayed immediately before the {@code command} is run
   * @param command command to run
   * @throws Throwable signals that the {@code command} failed
   */
  @Override
  public void runCommand(String commandName, ThrowingRunnable command) throws Throwable {
    System.out.printf("%s... ", commandName);
    try {
      command.run();
    } catch (Exception exception) {
      System.out.printf("%sFAIL!%s\n", ANSI_RED, ANSI_RESET);
      throw exception;
    }
    System.out.printf("%sSUCCESS%s\n", ANSI_GREEN, ANSI_RESET);