// Path: minikube-gradle-plugin/src/main/java/com/google/cloud/tools/minikube/util/CommandExecutorFactory.java
// public class CommandExecutorFactory {
//   private final Logger logger;
// 
//   /**
//    * Creates a new factory.
//    *
//    * @param logger for logging messages during the command execution
//    */
//   public CommandExecutorFactory(Logger logger) {
//     this.logger = logger;
//   }
// 
//   public CommandExecutor newCommandExecutor() {
//     return new CommandExecutor().setLogger(logger);
//   }
// }
// 
// Path: minikube-gradle-plugin/src/main/java/com/google/cloud/tools/minikube/util/MinikubeDockerEnvParser.java
// public class MinikubeDockerEnvParser {
// 
//   private MinikubeDockerEnvParser() {}
// 
//   /**
//    * Parses a list of KEY=VALUE strings into a map from KEY to VALUE.
//    *
//    * @param keyValueStrings a list of "KEY=VALUE" strings, where KEY is the environment variable
//    *     name and VALUE is the value to set it to
//    */
//   public static Map<String, String> parse(List<String> keyValueStrings) {
//     Map<String, String> environmentMap = new HashMap<>();
// 
//     for (String keyValueString : keyValueStrings) {
//       String[] keyValuePair = keyValueString.split("=", 2);
// 
//       if (keyValuePair.length < 2) {
//         throw new IllegalArgumentException(
//             "Error while parsing minikube's Docker environment: environment variable string not in KEY=VALUE format");
//       }
//       if (keyValuePair[0].length() == 0) {
//         throw new IllegalArgumentException(
//             "Error while parsing minikube's Docker environment: encountered empty environment variable name");
//       }
// 
//       environmentMap.put(keyValuePair[0], keyValuePair[1]);
//     }
// 
//     return environmentMap;
//   }
// }


import com.google.cloud.tools.minikube.util.CommandExecutorFactory;
import com.google.cloud.tools.minikube.util.MinikubeDockerEnvParser;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.gradle.api.Project;
import org.gradle.api.provider.Property;
/*
 * Copyright 2017 Google Inc.
 *
 * 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.cloud.tools.minikube;


/** Minikube configuration extension. */
public class MinikubeExtension {

  private final Property<String> minikube;

  private final CommandExecutorFactory commandExecutorFactory;

  public MinikubeExtension(Project project, CommandExecutorFactory commandExecutorFactory) {
    minikube = project.getObjects().property(String.class);
    setMinikube("minikube");

    this.commandExecutorFactory = commandExecutorFactory;
  }

  public String getMinikube() {
    return minikube.get();
  }

  public void setMinikube(String minikube) {
    this.minikube.set(minikube);
  }

  public Property<String> getMinikubeProvider() {
    return minikube;
  }

  /**
   * Gets the minikube docker environment variables by running the command 'minikube docker-env
   * --shell=none'.
   *
   * @return A map of docker environment variables and their values
   */
  public Map<String, String> getDockerEnv() throws IOException, InterruptedException {
    return getDockerEnv("");
  }

  /**
   * Gets the minikube docker environment variables by running the command 'minikube docker-env
   * --shell=none'.
   *
   * @param profile target minikube profile
   * @return A map of docker environment variables and their values
   */
  public Map<String, String> getDockerEnv(String profile) throws IOException, InterruptedException {
    if (profile == null) {
      throw new NullPointerException("Minikube profile must not be null");
    }

    List<String> minikubeDockerEnvCommand =
        Arrays.asList(minikube.get(), "docker-env", "--shell=none", "--profile=" + profile);

    List<String> dockerEnv =
        commandExecutorFactory.newCommandExecutor().run(minikubeDockerEnvCommand);

// Path: minikube-gradle-plugin/src/main/java/com/google/cloud/tools/minikube/util/CommandExecutorFactory.java
// public class CommandExecutorFactory {
//   private final Logger logger;
// 
//   /**
//    * Creates a new factory.
//    *
//    * @param logger for logging messages during the command execution
//    */
//   public CommandExecutorFactory(Logger logger) {
//     this.logger = logger;
//   }
// 
//   public CommandExecutor newCommandExecutor() {
//     return new CommandExecutor().setLogger(logger);
//   }
// }
// 
// Path: minikube-gradle-plugin/src/main/java/com/google/cloud/tools/minikube/util/MinikubeDockerEnvParser.java
// public class MinikubeDockerEnvParser {
// 
//   private MinikubeDockerEnvParser() {}
// 
//   /**
//    * Parses a list of KEY=VALUE strings into a map from KEY to VALUE.
//    *
//    * @param keyValueStrings a list of "KEY=VALUE" strings, where KEY is the environment variable
//    *     name and VALUE is the value to set it to
//    */
//   public static Map<String, String> parse(List<String> keyValueStrings) {
//     Map<String, String> environmentMap = new HashMap<>();
// 
//     for (String keyValueString : keyValueStrings) {
//       String[] keyValuePair = keyValueString.split("=", 2);
// 
//       if (keyValuePair.length < 2) {
//         throw new IllegalArgumentException(
//             "Error while parsing minikube's Docker environment: environment variable string not in KEY=VALUE format");
//       }
//       if (keyValuePair[0].length() == 0) {
//         throw new IllegalArgumentException(
//             "Error while parsing minikube's Docker environment: encountered empty environment variable name");
//       }
// 
//       environmentMap.put(keyValuePair[0], keyValuePair[1]);
//     }
// 
//     return environmentMap;
//   }
// }


// Path: minikube-gradle-plugin/src/main/java/com/google/cloud/tools/minikube/MinikubeExtension.java
import com.google.cloud.tools.minikube.util.CommandExecutorFactory;
import com.google.cloud.tools.minikube.util.MinikubeDockerEnvParser;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.gradle.api.Project;
import org.gradle.api.provider.Property;

/*
 * Copyright 2017 Google Inc.
 *
 * 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.cloud.tools.minikube;


/** Minikube configuration extension. */
public class MinikubeExtension {

  private final Property<String> minikube;

  private final CommandExecutorFactory commandExecutorFactory;

  public MinikubeExtension(Project project, CommandExecutorFactory commandExecutorFactory) {
    minikube = project.getObjects().property(String.class);
    setMinikube("minikube");

    this.commandExecutorFactory = commandExecutorFactory;
  }

  public String getMinikube() {
    return minikube.get();
  }

  public void setMinikube(String minikube) {
    this.minikube.set(minikube);
  }

  public Property<String> getMinikubeProvider() {
    return minikube;
  }

  /**
   * Gets the minikube docker environment variables by running the command 'minikube docker-env
   * --shell=none'.
   *
   * @return A map of docker environment variables and their values
   */
  public Map<String, String> getDockerEnv() throws IOException, InterruptedException {
    return getDockerEnv("");
  }

  /**
   * Gets the minikube docker environment variables by running the command 'minikube docker-env
   * --shell=none'.
   *
   * @param profile target minikube profile
   * @return A map of docker environment variables and their values
   */
  public Map<String, String> getDockerEnv(String profile) throws IOException, InterruptedException {
    if (profile == null) {
      throw new NullPointerException("Minikube profile must not be null");
    }

    List<String> minikubeDockerEnvCommand =
        Arrays.asList(minikube.get(), "docker-env", "--shell=none", "--profile=" + profile);

    List<String> dockerEnv =
        commandExecutorFactory.newCommandExecutor().run(minikubeDockerEnvCommand);

    return MinikubeDockerEnvParser.parse(dockerEnv);