19
19
20
20
package org .apache .gravitino .cli ;
21
21
22
+ import com .google .common .base .Preconditions ;
23
+ import org .apache .commons .cli .CommandLine ;
22
24
import org .apache .gravitino .cli .commands .Command ;
23
25
24
26
/* Context for a command */
25
27
public class CommandContext {
26
- private String url ;
27
- private boolean ignoreVersions ;
28
- private boolean force ;
29
- private String outputFormat ;
28
+ private final boolean force ;
29
+ private final boolean ignoreVersions ;
30
+ private final String outputFormat ;
31
+ private final String url ;
32
+ private final CommandLine line ;
33
+
34
+ private String ignoreEnv ;
35
+ private boolean ignoreSet = false ;
36
+ private String urlEnv ;
37
+ private boolean urlSet = false ;
30
38
// Can add more "global" command flags here without any major changes e.g. a guiet flag
31
39
32
40
/**
33
41
* Command constructor.
34
42
*
35
- * @param url The URL of the Gravitino server.
36
- * @param ignoreVersions If true don't check the client/server versions match.
43
+ * @param line The command line.
37
44
*/
38
- public CommandContext (String url , boolean ignoreVersions ) {
39
- this .url = url ;
40
- this .ignoreVersions = ignoreVersions ;
41
- this .force = false ;
42
- this .outputFormat = Command .OUTPUT_FORMAT_PLAIN ;
43
- }
45
+ public CommandContext (CommandLine line ) {
46
+ Preconditions .checkNotNull (line );
47
+ this .line = line ;
48
+ this .force = line .hasOption (GravitinoOptions .FORCE );
49
+ this .outputFormat =
50
+ line .hasOption (GravitinoOptions .OUTPUT )
51
+ ? line .getOptionValue (GravitinoOptions .OUTPUT )
52
+ : Command .OUTPUT_FORMAT_PLAIN ;
44
53
45
- /**
46
- * Command constructor.
47
- *
48
- * @param url The URL of the Gravitino server.
49
- * @param ignoreVersions If true don't check the client/server versions match.
50
- * @param force Force operation.
51
- * @param outputFormat Display output format.
52
- */
53
- public CommandContext (String url , boolean ignoreVersions , boolean force , String outputFormat ) {
54
- this .url = url ;
55
- this .ignoreVersions = ignoreVersions ;
56
- this .force = force ;
57
- this .outputFormat = outputFormat ;
54
+ this .url = getUrl ();
55
+ this .ignoreVersions = getIgnore ();
58
56
}
59
57
60
58
/**
@@ -66,15 +64,6 @@ public String url() {
66
64
return url ;
67
65
}
68
66
69
- /**
70
- * Sets the URL.
71
- *
72
- * @param url The URL to be set.
73
- */
74
- public void setUrl (String url ) {
75
- this .url = url ;
76
- }
77
-
78
67
/**
79
68
* Indicates whether versions should be ignored.
80
69
*
@@ -101,4 +90,69 @@ public boolean force() {
101
90
public String outputFormat () {
102
91
return outputFormat ;
103
92
}
93
+
94
+ /**
95
+ * Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment
96
+ * variable or the Gravitino config file.
97
+ *
98
+ * @return The Gravitino URL, or null if not found.
99
+ */
100
+ private String getUrl () {
101
+ GravitinoConfig config = new GravitinoConfig (null );
102
+
103
+ // If specified on the command line use that
104
+ if (line .hasOption (GravitinoOptions .URL )) {
105
+ return line .getOptionValue (GravitinoOptions .URL );
106
+ }
107
+
108
+ // Cache the Gravitino URL environment variable
109
+ if (urlEnv == null && !urlSet ) {
110
+ urlEnv = System .getenv ("GRAVITINO_URL" );
111
+ urlSet = true ;
112
+ }
113
+
114
+ // If set return the Gravitino URL environment variable
115
+ if (urlEnv != null ) {
116
+ return urlEnv ;
117
+ }
118
+
119
+ // Check if the Gravitino URL is specified in the configuration file
120
+ if (config .fileExists ()) {
121
+ config .read ();
122
+ String configURL = config .getGravitinoURL ();
123
+ if (configURL != null ) {
124
+ return configURL ;
125
+ }
126
+ }
127
+
128
+ // Return the default localhost URL
129
+ return GravitinoCommandLine .DEFAULT_URL ;
130
+ }
131
+
132
+ private boolean getIgnore () {
133
+ GravitinoConfig config = new GravitinoConfig (null );
134
+ boolean ignore = false ;
135
+
136
+ /* Check if you should ignore client/version versions */
137
+ if (line .hasOption (GravitinoOptions .IGNORE )) {
138
+ ignore = true ;
139
+ } else {
140
+ // Cache the ignore environment variable
141
+ if (ignoreEnv == null && !ignoreSet ) {
142
+ ignoreEnv = System .getenv ("GRAVITINO_IGNORE" );
143
+ ignore = ignoreEnv != null && ignoreEnv .equals ("true" );
144
+ ignoreSet = true ;
145
+ }
146
+
147
+ // Check if the ignore name is specified in the configuration file
148
+ if (ignoreEnv == null ) {
149
+ if (config .fileExists ()) {
150
+ config .read ();
151
+ ignore = config .getIgnore ();
152
+ }
153
+ }
154
+ }
155
+
156
+ return ignore ;
157
+ }
104
158
}
0 commit comments