• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.utils;
17 
18 import java.util.Optional;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 
21 /**
22  * Load the home directory that should be used for the stored file. This will check the same environment variables as the CLI
23  * to identify the location of home, before falling back to java-specific resolution.
24  */
25 @SdkProtectedApi
26 public final class UserHomeDirectoryUtils {
27 
UserHomeDirectoryUtils()28     private UserHomeDirectoryUtils() {
29 
30     }
31 
userHomeDirectory()32     public static String userHomeDirectory() {
33         // CHECKSTYLE:OFF - To match the logic of the CLI we have to consult environment variables directly.
34         Optional<String> home = SystemSetting.getStringValueFromEnvironmentVariable("HOME");
35 
36         if (home.isPresent()) {
37             return home.get();
38         }
39 
40         boolean isWindows = JavaSystemSetting.OS_NAME.getStringValue()
41                                                      .map(s -> StringUtils.lowerCase(s).startsWith("windows"))
42                                                      .orElse(false);
43 
44         if (isWindows) {
45             Optional<String> userProfile = SystemSetting.getStringValueFromEnvironmentVariable("USERPROFILE");
46 
47             if (userProfile.isPresent()) {
48                 return userProfile.get();
49             }
50 
51             Optional<String> homeDrive = SystemSetting.getStringValueFromEnvironmentVariable("HOMEDRIVE");
52             Optional<String> homePath = SystemSetting.getStringValueFromEnvironmentVariable("HOMEPATH");
53 
54             if (homeDrive.isPresent() && homePath.isPresent()) {
55                 return homeDrive.get() + homePath.get();
56             }
57         }
58 
59         return JavaSystemSetting.USER_HOME.getStringValueOrThrow();
60         // CHECKSTYLE:ON
61     }
62 }
63