/*
 * Copyright 2017 Okta
 *
 * 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.okta.tools;

import java.io.FileReader;
import java.io.IOException;
import java.time.Instant;
import java.util.*;

public class awscli {
    public static void main(String[] args) throws Exception {
        String profileName = createAwscli().run(Instant.now());
        List<String> awsCommand = new ArrayList<>();
        awsCommand.add("aws");
        awsCommand.add("--profile");
        awsCommand.add(profileName);
        awsCommand.addAll(Arrays.asList(args));
        ProcessBuilder awsProcessBuilder = new ProcessBuilder().inheritIO().command(awsCommand);
        Process awsSubProcess = awsProcessBuilder.start();
        int exitCode = awsSubProcess.waitFor();
        System.exit(exitCode);
    }

    private static OktaAwsCliAssumeRole createAwscli() throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader("config.properties"));

        return OktaAwsCliAssumeRole.createOktaAwsCliAssumeRole(
                getEnvOrConfig(properties, "OKTA_ORG"),
                getEnvOrConfig(properties, "OKTA_AWS_APP_URL"),
                getEnvOrConfig(properties, "OKTA_USERNAME"),
                getEnvOrConfig(properties, "OKTA_PASSWORD"),
                getEnvOrConfig(properties, "OKTA_AWS_ROLE_TO_ASSUME")
        );
    }

    private static String getEnvOrConfig(Properties properties, String propertyName) {
        String envValue = System.getenv(propertyName);
        return envValue != null ?
                envValue : properties.getProperty(propertyName);
    }
}

@@ -625,22 +625,27 @@
 .io.File
-Reader
+InputStream
 ;%0Aimport
@@ -666,16 +666,78 @@
 eption;%0A
+import java.io.InputStream;%0Aimport java.io.InputStreamReader;%0A
 import j
@@ -1509,40 +1509,255 @@
     
-properties.load(new FileReader
+InputStream config;%0A        try %7B%0A            config = new FileInputStream(%22config.properties%22);%0A        %7D catch (IOException ex) %7B%0A            // Fallback to classpath input stream%0A            config = properties.getClass().getResourceAsStream
 (%22
+/
 conf
@@ -1771,16 +1771,81 @@
 erties%22)
+;%0A        %7D%0A        properties.load(new InputStreamReader(config)
 );%0A%0A    

Load from classpath if config.properties is not found