Skip to content

Commit f5df5c4

Browse files
authored
fix: envconfig, use OS-specific config file paths (#2762)
* use OS-specific config file paths * address pr nits
1 parent 7a1a238 commit f5df5c4

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

temporal-envconfig/src/main/java/io/temporal/envconfig/ClientConfig.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@
55
import com.fasterxml.jackson.dataformat.toml.TomlMapper;
66
import io.temporal.common.Experimental;
77
import java.io.*;
8+
import java.nio.file.Paths;
89
import java.util.HashMap;
910
import java.util.Map;
1011
import java.util.Objects;
1112

12-
/** ClientConfig represents a client config file. */
13+
/**
14+
* ClientConfig represents a client config file.
15+
*
16+
* <p>The default config file path is OS-specific:
17+
*
18+
* <ul>
19+
* <li>macOS: $HOME/Library/Application Support/temporalio/temporal.toml
20+
* <li>Windows: %APPDATA%\temporalio\temporal.toml
21+
* <li>Linux/other: $HOME/.config/temporalio/temporal.toml
22+
* </ul>
23+
*/
1324
@Experimental
1425
public class ClientConfig {
1526
/** Creates a new builder to build a {@link ClientConfig}. */
@@ -32,13 +43,31 @@ public static ClientConfig getDefaultInstance() {
3243
return new ClientConfig.Builder().build();
3344
}
3445

35-
/** Get the default config file path: $HOME/.config/temporalio/temporal.toml */
3646
private static String getDefaultConfigFilePath() {
3747
String userDir = System.getProperty("user.home");
3848
if (userDir == null || userDir.isEmpty()) {
3949
throw new RuntimeException("failed getting user home directory");
4050
}
41-
return userDir + "/.config/temporalio/temporal.toml";
51+
return getDefaultConfigFilePath(userDir, System.getProperty("os.name"), System.getenv());
52+
}
53+
54+
static String getDefaultConfigFilePath(
55+
String userDir, String osName, Map<String, String> environment) {
56+
if (osName != null) {
57+
String osNameLower = osName.toLowerCase();
58+
if (osNameLower.contains("mac")) {
59+
return Paths.get(userDir, "Library", "Application Support", "temporalio", "temporal.toml")
60+
.toString();
61+
}
62+
if (osNameLower.contains("win")) {
63+
String appData = environment != null ? environment.get("APPDATA") : null;
64+
if (appData == null || appData.isEmpty()) {
65+
throw new RuntimeException("%APPDATA% is not defined");
66+
}
67+
return Paths.get(appData, "temporalio", "temporal.toml").toString();
68+
}
69+
}
70+
return Paths.get(userDir, ".config", "temporalio", "temporal.toml").toString();
4271
}
4372

4473
/**

temporal-envconfig/src/test/java/io/temporal/envconfig/ClientConfigProfileTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
88
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
99
import java.io.*;
10+
import java.nio.file.Paths;
1011
import java.util.Collections;
1112
import org.junit.Assert;
1213
import org.junit.Test;
@@ -302,6 +303,28 @@ public void loadClientConfigProfileMissingFileReturnsDefault() throws IOExceptio
302303
Assert.assertNull(profile.getNamespace());
303304
}
304305

306+
@Test
307+
public void defaultConfigFilePath() {
308+
// macOS: ~/Library/Application Support
309+
Assert.assertEquals(
310+
Paths.get("/Users/test", "Library", "Application Support", "temporalio", "temporal.toml")
311+
.toString(),
312+
ClientConfig.getDefaultConfigFilePath("/Users/test", "Mac OS X", Collections.emptyMap()));
313+
314+
// Windows: %APPDATA%
315+
Assert.assertEquals(
316+
Paths.get("C:/Users/test/AppData/Roaming", "temporalio", "temporal.toml").toString(),
317+
ClientConfig.getDefaultConfigFilePath(
318+
"C:/Users/test",
319+
"Windows 10",
320+
Collections.singletonMap("APPDATA", "C:/Users/test/AppData/Roaming")));
321+
322+
// Linux: ~/.config
323+
Assert.assertEquals(
324+
Paths.get("/home/test", ".config", "temporalio", "temporal.toml").toString(),
325+
ClientConfig.getDefaultConfigFilePath("/home/test", "Linux", Collections.emptyMap()));
326+
}
327+
305328
@Test
306329
public void parseToml() throws IOException {
307330
String toml =

0 commit comments

Comments
 (0)