|
1 | 1 | package com.contentstack.sdk; |
2 | 2 |
|
3 | | -import io.github.cdimascio.dotenv.Dotenv; |
4 | | -import io.github.cdimascio.dotenv.DotenvException; |
| 3 | +import lombok.var; |
5 | 4 |
|
6 | 5 | import java.rmi.AccessException; |
7 | 6 |
|
8 | 7 | public class Credentials { |
9 | | - static Dotenv env = getEnv(); |
| 8 | + //static Dotenv env = getEnv(); |
10 | 9 |
|
11 | | - /** |
12 | | - * The provided Java code defines a method named `getEnv()` that attempts to load environment variables from a `.env` file using the `Dotenv` library. If loading the environment variables encounters an exception (specifically, a `DotenvException`), the code takes an alternative path by creating an empty `.env` file in the current working directory. |
13 | | - * <p> |
14 | | - * Here's a breakdown of what the code is doing step by step: |
15 | | - * <p> |
16 | | - * 1. The method `public static Dotenv getEnv()` is defined. It returns an instance of the `Dotenv` class, which is used to manage environment variables loaded from the `.env` file. |
17 | | - * <p> |
18 | | - * 2. Inside the `try` block, the code tries to load environment variables using `Dotenv.load()`. If successful, the loaded environment variables are stored in the `env` variable. |
19 | | - * <p> |
20 | | - * 3. If loading the environment variables from the `.env` file encounters an exception (a `DotenvException`), the code enters the `catch` block. |
21 | | - * <p> |
22 | | - * 4. In the `catch` block, it gets the current working directory using `System.getProperty("user.dir")` and creates a `File` object named `envFile` representing the `.env` file in the current directory. |
23 | | - * <p> |
24 | | - * 5. The code attempts to create an empty `.env` file using `envFile.createNewFile()`. |
25 | | - * <p> |
26 | | - * 6. If there's an error during the file creation process (an `IOException`), an error message is printed to the standard error output, and the exception's stack trace is printed for debugging purposes. |
27 | | - * <p> |
28 | | - * 7. Finally, regardless of whether the environment variables were successfully loaded or a new `.env` file was created, the method returns the `env` variable, which may either contain the loaded environment variables or be `null` if an exception occurred during the loading process. |
29 | | - * <p> |
30 | | - * In summary, this code defines a method that attempts to load environment variables from a `.env` file using the `Dotenv` library. If loading fails due to an exception, it creates an empty `.env` file in the current working directory and then returns the `Dotenv` instance, which may or may not have loaded environment variables depending on whether an exception occurred. |
31 | | - * |
32 | | - * @return Dotenv |
33 | | - */ |
34 | | - public static Dotenv getEnv() { |
35 | | - String currentDirectory = System.getProperty("user.dir"); |
36 | | - //File envFile = new File(currentDirectory, "env"); |
37 | | - env = Dotenv.configure() |
38 | | - .directory("src/test/resources") |
39 | | - .filename("env") // instead of '.env', use 'env' |
40 | | - .load(); |
41 | | - try { |
42 | | - env = Dotenv.load(); |
43 | | - } catch (DotenvException ex) { |
44 | | - System.out.println("Could not load from local .env"); |
45 | | -// File envFile = new File(currentDirectory, ".env"); |
46 | | -// try { |
47 | | -// // Create .env file in the current directory |
48 | | -// envFile.createNewFile(); |
49 | | -// } catch (IOException e) { |
50 | | -// System.err.println("An error occurred while creating .env file."); |
51 | | -// e.printStackTrace(); |
52 | | -// } |
| 10 | + private static String envChecker() { |
| 11 | + String githubActions = System.getenv("GITHUB_ACTIONS"); |
| 12 | + if (githubActions != null && githubActions.equals("true")) { |
| 13 | + System.out.println("Tests are running in GitHub Actions environment."); |
| 14 | + String mySecretKey = System.getenv("API_KEY"); |
| 15 | + System.out.println("My Secret Key: " + mySecretKey); |
| 16 | + return "GitHub"; |
| 17 | + } else { |
| 18 | + System.out.println("Tests are running in a local environment."); |
| 19 | + return "local"; |
53 | 20 | } |
54 | | - return env; |
55 | 21 | } |
56 | 22 |
|
| 23 | +// public static Dotenv getEnv() { |
| 24 | +// String currentDirectory = System.getProperty("user.dir"); |
| 25 | +// File envFile = new File(currentDirectory, "env"); |
| 26 | +// env = Dotenv.configure() |
| 27 | +// .directory("src/test/resources") |
| 28 | +// .filename("env") // instead of '.env', use 'env' |
| 29 | +// .load(); |
| 30 | +// try { |
| 31 | +// env = Dotenv.load(); |
| 32 | +// } catch (DotenvException ex) { |
| 33 | +// System.out.println("Could not load from local .env"); |
| 34 | +//// File envFile = new File(currentDirectory, ".env"); |
| 35 | +//// try { |
| 36 | +//// // Create .env file in the current directory |
| 37 | +//// envFile.createNewFile(); |
| 38 | +//// } catch (IOException e) { |
| 39 | +//// System.err.println("An error occurred while creating .env file."); |
| 40 | +//// e.printStackTrace(); |
| 41 | +//// } |
| 42 | +// } |
| 43 | +// return env; |
| 44 | +// } |
57 | 45 |
|
58 | | - public final static String pwd = (env.get("PWD") != null) ? env.get("PWD") : "contentstack-java"; |
59 | | - public final static String HOST = (env.get("HOST") != null) ? env.get("HOST") : "cdn.contentstack.io"; |
60 | | - public final static String API_KEY = (env.get("API_KEY") != null) ? env.get("API_KEY") : "***REMOVED***"; |
61 | | - public final static String DELIVERY_TOKEN = (env.get("DELIVERY_TOKEN") != null) ? env.get("DELIVERY_TOKEN") : "***REMOVED***"; |
62 | | - public final static String ENVIRONMENT = (env.get("ENVIRONMENT") != null) ? env.get("ENVIRONMENT") : "env1"; |
63 | | - public final static String CONTENT_TYPE = (env.get("contentType") != null) ? env.get("contentType") : "product"; |
64 | | - public final static String ENTRY_UID = (env.get("assetUid") != null) ? env.get("assetUid") : "blt884786476373"; |
65 | 46 |
|
66 | | - private static Stack stack; |
| 47 | +// public final static String pwd = (env.get("PWD") != null) ? env.get("PWD") : "contentstack-java"; |
| 48 | +// public final static String HOST = (env.get("HOST") != null) ? env.get("HOST") : "cdn.contentstack.io"; |
| 49 | +// public final static String API_KEY = (env.get("API_KEY") != null) ? env.get("API_KEY") : "***REMOVED***"; |
| 50 | +// public final static String DELIVERY_TOKEN = (env.get("DELIVERY_TOKEN") != null) ? env.get("DELIVERY_TOKEN") : "***REMOVED***"; |
| 51 | +// public final static String ENVIRONMENT = (env.get("ENVIRONMENT") != null) ? env.get("ENVIRONMENT") : "env1"; |
| 52 | +// public final static String CONTENT_TYPE = (env.get("contentType") != null) ? env.get("contentType") : "product"; |
| 53 | +// public final static String ENTRY_UID = (env.get("assetUid") != null) ? env.get("assetUid") : "blt884786476373"; |
| 54 | + |
| 55 | + |
| 56 | + public final static String HOST = "cdn.contentstack.io"; |
| 57 | + public final static String API_KEY = "***REMOVED***"; |
| 58 | + public final static String DELIVERY_TOKEN = "***REMOVED***"; |
| 59 | + public final static String ENVIRONMENT = "env1"; |
| 60 | + public final static String CONTENT_TYPE = "product"; |
| 61 | + public final static String ENTRY_UID = "blt884786476373"; |
| 62 | + |
| 63 | + private static volatile Stack stack; |
67 | 64 |
|
68 | 65 | private Credentials() throws AccessException { |
69 | | - // Private constructor to prevent direct instantiation |
70 | 66 | throw new AccessException("Can not access credential access"); |
71 | 67 | } |
72 | 68 |
|
73 | 69 | public static Stack getStack() { |
74 | | - |
75 | 70 | if (stack == null) { |
| 71 | + var envCheck = envChecker(); |
| 72 | + System.out.println(envCheck); |
76 | 73 | synchronized (Credentials.class) { |
77 | 74 | if (stack == null) { |
78 | 75 | try { |
|
0 commit comments