Skip to content

Commit a3efc99

Browse files
authored
Adds OperatingSystem enum. (#4)
1 parent 2689ac8 commit a3efc99

3 files changed

Lines changed: 149 additions & 29 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.skaffold.filesystem;
18+
19+
import java.util.Locale;
20+
import java.util.Properties;
21+
22+
/** Gets information about the operating system. */
23+
public enum OperatingSystem {
24+
LINUX,
25+
MAC_OS,
26+
WINDOWS;
27+
28+
/**
29+
* Resolves the operating system type.
30+
*
31+
* @return the {@link OperatingSystem}
32+
*/
33+
public static OperatingSystem resolve() {
34+
return resolve(System.getProperties());
35+
}
36+
37+
static OperatingSystem resolve(Properties properties) {
38+
String rawOsName = properties.getProperty("os.name");
39+
String osName = rawOsName.toLowerCase(Locale.ENGLISH);
40+
41+
if (osName.contains("linux")) {
42+
return LINUX;
43+
}
44+
if (osName.contains("windows")) {
45+
return WINDOWS;
46+
}
47+
if (osName.contains("mac") || osName.contains("darwin")) {
48+
return MAC_OS;
49+
}
50+
51+
throw new IllegalStateException("Unknown OS: " + rawOsName);
52+
}
53+
}

skaffold-plugins-core/src/main/java/com/google/cloud/tools/skaffold/filesystem/UserCacheHome.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
2222
import java.nio.file.Paths;
23-
import java.util.Locale;
2423
import java.util.Map;
2524
import java.util.Properties;
2625
import java.util.logging.Logger;
@@ -51,7 +50,7 @@ public static Path getCacheHome() {
5150
* Returns {@code $XDG_CACHE_HOME}, if available, or resolves the OS-specific user cache home
5251
* based.
5352
*
54-
* <p>For Linus, this is {@code $HOME/.cache/}.
53+
* <p>For Linux, this is {@code $HOME/.cache/}.
5554
*
5655
* <p>For Windows, this is {@code %LOCALAPPDATA%}.
5756
*
@@ -70,37 +69,39 @@ static Path getCacheHome(Properties properties, Map<String, String> environment)
7069
Path userHomeDirectory = Paths.get(userHome);
7170
Path xdgPath = userHomeDirectory.resolve(".cache");
7271

73-
String rawOsName = properties.getProperty("os.name");
74-
String osName = rawOsName.toLowerCase(Locale.ENGLISH);
72+
OperatingSystem operatingSystem = OperatingSystem.resolve(properties);
7573

76-
if (osName.contains("linux")) {
77-
return xdgPath;
78-
79-
} else if (osName.contains("windows")) {
80-
// Use %LOCALAPPDATA% for Windows.
81-
String localAppDataEnv = environment.get("LOCALAPPDATA");
82-
if (localAppDataEnv == null || localAppDataEnv.trim().isEmpty()) {
83-
logger.warning("LOCALAPPDATA environment is invalid or missing");
84-
return xdgPath;
85-
}
86-
Path localAppData = Paths.get(localAppDataEnv);
87-
if (!Files.exists(localAppData)) {
88-
logger.warning(localAppData + " does not exist");
74+
switch (operatingSystem) {
75+
case LINUX:
8976
return xdgPath;
90-
}
91-
return localAppData;
9277

93-
} else if (osName.contains("mac") || osName.contains("darwin")) {
94-
// Use '~/Library/Application Support/' for macOS.
95-
Path applicationSupport = userHomeDirectory.resolve("Library").resolve("Application Support");
96-
if (!Files.exists(applicationSupport)) {
97-
logger.warning(applicationSupport + " does not exist");
98-
return xdgPath;
99-
}
100-
return applicationSupport;
101-
}
78+
case WINDOWS:
79+
// Use %LOCALAPPDATA% for Windows.
80+
String localAppDataEnv = environment.get("LOCALAPPDATA");
81+
if (localAppDataEnv == null || localAppDataEnv.trim().isEmpty()) {
82+
logger.warning("LOCALAPPDATA environment is invalid or missing");
83+
return xdgPath;
84+
}
85+
Path localAppData = Paths.get(localAppDataEnv);
86+
if (!Files.exists(localAppData)) {
87+
logger.warning(localAppData + " does not exist");
88+
return xdgPath;
89+
}
90+
return localAppData;
91+
92+
case MAC_OS:
93+
// Use '~/Library/Application Support/' for macOS.
94+
Path applicationSupport =
95+
userHomeDirectory.resolve("Library").resolve("Application Support");
96+
if (!Files.exists(applicationSupport)) {
97+
logger.warning(applicationSupport + " does not exist");
98+
return xdgPath;
99+
}
100+
return applicationSupport;
102101

103-
throw new IllegalStateException("Unknown OS: " + rawOsName);
102+
default:
103+
throw new IllegalStateException("unreachable");
104+
}
104105
}
105106

106107
private UserCacheHome() {}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.skaffold.filesystem;
18+
19+
import java.util.Properties;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/** Tests for {@link OperatingSystem}. */
24+
public class OperatingSystemTest {
25+
26+
@Test
27+
public void testLinux() {
28+
Properties fakeProperties = new Properties();
29+
fakeProperties.setProperty("os.name", "os is LiNuX");
30+
Assert.assertEquals(OperatingSystem.LINUX, OperatingSystem.resolve(fakeProperties));
31+
}
32+
33+
@Test
34+
public void testMacOs_mac() {
35+
Properties fakeProperties = new Properties();
36+
fakeProperties.setProperty("os.name", "os is mAc");
37+
Assert.assertEquals(OperatingSystem.MAC_OS, OperatingSystem.resolve(fakeProperties));
38+
}
39+
40+
@Test
41+
public void testMacOs_darwin() {
42+
Properties fakeProperties = new Properties();
43+
fakeProperties.setProperty("os.name", "os is DaRwIn");
44+
Assert.assertEquals(OperatingSystem.MAC_OS, OperatingSystem.resolve(fakeProperties));
45+
}
46+
47+
@Test
48+
public void testWindows() {
49+
Properties fakeProperties = new Properties();
50+
fakeProperties.setProperty("os.name", "os is WiNdOwS");
51+
Assert.assertEquals(OperatingSystem.WINDOWS, OperatingSystem.resolve(fakeProperties));
52+
}
53+
54+
@Test
55+
public void testUnknown() {
56+
Properties fakeProperties = new Properties();
57+
fakeProperties.setProperty("os.name", "UnKnOwN");
58+
try {
59+
OperatingSystem.resolve(fakeProperties);
60+
Assert.fail("Resolve should have failed");
61+
62+
} catch (IllegalStateException ex) {
63+
Assert.assertEquals("Unknown OS: UnKnOwN", ex.getMessage());
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)