Skip to content

Commit bf14f5d

Browse files
committed
added CRUD operations for Config to the CLI
1 parent b6d4588 commit bf14f5d

File tree

12 files changed

+541
-89
lines changed

12 files changed

+541
-89
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/ImageTool.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2019, 2023, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.weblogic.imagetool.cli;
@@ -7,6 +7,7 @@
77

88
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
99
import com.oracle.weblogic.imagetool.cli.cache.CacheCLI;
10+
import com.oracle.weblogic.imagetool.cli.config.Config;
1011
import com.oracle.weblogic.imagetool.cli.menu.CreateAuxImage;
1112
import com.oracle.weblogic.imagetool.cli.menu.CreateImage;
1213
import com.oracle.weblogic.imagetool.cli.menu.InspectImage;
@@ -28,6 +29,7 @@
2829
sortOptions = false,
2930
subcommands = {
3031
CacheCLI.class,
32+
Config.class,
3133
CreateImage.class,
3234
CreateAuxImage.class,
3335
UpdateImage.class,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.config;
5+
6+
import picocli.CommandLine;
7+
8+
@CommandLine.Command(
9+
name = "config",
10+
description = "Set global configuration options and defaults for the Image Tool",
11+
commandListHeading = "%nCommands:%n%n",
12+
subcommands = {
13+
SetConfigEntry.class,
14+
ReadConfigEntry.class,
15+
DeleteConfigEntry.class
16+
},
17+
sortOptions = false
18+
)
19+
public class Config {
20+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.config;
5+
6+
import com.oracle.weblogic.imagetool.settings.UserSettings;
7+
8+
public enum ConfigAttributeName {
9+
buildContextDirectory("BuildContextDirectory") {
10+
@Override
11+
public void set(UserSettings settings, String value) {
12+
settings.setBuildContextDirectory(value);
13+
}
14+
15+
@Override
16+
public String get(UserSettings settings) {
17+
return settings.getBuildContextDirectory();
18+
}
19+
},
20+
patchDirectory("PatchDirectory") {
21+
@Override
22+
public void set(UserSettings settings, String value) {
23+
settings.setPatchDirectory(value);
24+
}
25+
26+
@Override
27+
public String get(UserSettings settings) {
28+
return settings.getPatchDirectory();
29+
}
30+
},
31+
installerDirectory("InstallerDirectory") {
32+
@Override
33+
public void set(UserSettings settings, String value) {
34+
settings.setInstallerDirectory(value);
35+
}
36+
37+
@Override
38+
public String get(UserSettings settings) {
39+
return settings.getInstallerDirectory();
40+
}
41+
},
42+
buildEngine("BuildEngine") {
43+
@Override
44+
public void set(UserSettings settings, String value) {
45+
settings.setBuildEngine(value);
46+
}
47+
48+
@Override
49+
public String get(UserSettings settings) {
50+
return settings.getBuildEngine();
51+
}
52+
},
53+
containerEngine("ContainerEngine") {
54+
@Override
55+
public void set(UserSettings settings, String value) {
56+
settings.setContainerEngine(value);
57+
}
58+
59+
@Override
60+
public String get(UserSettings settings) {
61+
return settings.getContainerEngine();
62+
}
63+
},
64+
aruRetryMax("AruRetryMax") {
65+
@Override
66+
public void set(UserSettings settings, String value) {
67+
settings.setAruRetryMax(Integer.parseInt(value));
68+
}
69+
70+
@Override
71+
public String get(UserSettings settings) {
72+
//TODO check for null
73+
return settings.getAruRetryMax().toString();
74+
}
75+
},
76+
aruRetryInterval("AruRetryInterval") {
77+
@Override
78+
public void set(UserSettings settings, String value) {
79+
settings.setAruRetryInterval(Integer.parseInt(value));
80+
}
81+
82+
@Override
83+
public String get(UserSettings settings) {
84+
//TODO check for null
85+
return settings.getAruRetryInterval().toString();
86+
}
87+
};
88+
89+
private final String value;
90+
91+
public abstract void set(UserSettings settings, String value);
92+
93+
public abstract String get(UserSettings settings);
94+
95+
ConfigAttributeName(String value) {
96+
this.value = value;
97+
}
98+
99+
@Override
100+
public String toString() {
101+
return value;
102+
}
103+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.config;
5+
6+
import java.util.concurrent.Callable;
7+
8+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
9+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
10+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
11+
import com.oracle.weblogic.imagetool.settings.UserSettings;
12+
import picocli.CommandLine;
13+
14+
@CommandLine.Command(
15+
name = "reset",
16+
description = "Remove/reset a configuration entry",
17+
sortOptions = false
18+
)
19+
public class DeleteConfigEntry implements Callable<CommandResponse> {
20+
private static final LoggingFacade logger = LoggingFactory.getLogger(DeleteConfigEntry.class);
21+
22+
@CommandLine.Option(
23+
names = {"--name"},
24+
description = "Name of the setting",
25+
order = 0,
26+
required = true
27+
)
28+
private ConfigAttributeName name;
29+
30+
@Override
31+
public CommandResponse call() throws Exception {
32+
logger.entering();
33+
UserSettings settings = UserSettings.load();
34+
35+
name.set(settings, null);
36+
settings.save();
37+
38+
logger.exiting();
39+
return new CommandResponse(0, "");
40+
}
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.config;
5+
6+
import java.util.concurrent.Callable;
7+
8+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
9+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
10+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
11+
import com.oracle.weblogic.imagetool.settings.UserSettings;
12+
import picocli.CommandLine;
13+
14+
@CommandLine.Command(
15+
name = "show",
16+
description = "Print a configuration entry",
17+
sortOptions = false
18+
)
19+
public class ReadConfigEntry implements Callable<CommandResponse> {
20+
private static final LoggingFacade logger = LoggingFactory.getLogger(ReadConfigEntry.class);
21+
22+
@CommandLine.Option(
23+
names = {"--name"},
24+
description = "Name of the setting",
25+
order = 0,
26+
required = true
27+
)
28+
private ConfigAttributeName name;
29+
30+
@Override
31+
public CommandResponse call() throws Exception {
32+
logger.entering();
33+
UserSettings settings = UserSettings.load();
34+
35+
String result = name.get(settings);
36+
37+
logger.exiting();
38+
if (result != null) {
39+
System.out.println(result);
40+
return new CommandResponse(0, "");
41+
} else {
42+
return new CommandResponse(CommandLine.ExitCode.SOFTWARE, "Not Found");
43+
}
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.config;
5+
6+
import java.util.concurrent.Callable;
7+
8+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
9+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
10+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
11+
import com.oracle.weblogic.imagetool.settings.UserSettings;
12+
import picocli.CommandLine.Command;
13+
import picocli.CommandLine.Option;
14+
import picocli.CommandLine.Parameters;
15+
16+
@Command(
17+
name = "set",
18+
description = "Set or update a configuration entry",
19+
sortOptions = false
20+
)
21+
public class SetConfigEntry implements Callable<CommandResponse> {
22+
private static final LoggingFacade logger = LoggingFactory.getLogger(SetConfigEntry.class);
23+
24+
@Option(
25+
names = {"--name"},
26+
description = "Name of the setting",
27+
order = 0,
28+
required = true
29+
)
30+
private ConfigAttributeName name;
31+
32+
@Parameters(index = "0")
33+
@SuppressWarnings("UnusedDeclaration")
34+
private String value;
35+
36+
@Override
37+
public CommandResponse call() throws Exception {
38+
UserSettings settings = UserSettings.load();
39+
logger.entering();
40+
41+
name.set(settings, value);
42+
settings.save();
43+
44+
logger.exiting();
45+
return new CommandResponse(0, "done");
46+
}
47+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) 2023, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.settings;
5+
6+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
7+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
8+
import com.oracle.weblogic.imagetool.util.Utils;
9+
10+
/**
11+
* Wrapper class for UserSettings where default values are returned when no user setting exists for a given attribute.
12+
*/
13+
public class ConfigSettings {
14+
private static final LoggingFacade logger = LoggingFactory.getLogger(ConfigSettings.class);
15+
16+
UserSettings userSettings;
17+
18+
/**
19+
* Parent directory for the build context directory.
20+
* A temporary folder created under "Build Directory" with the prefix "wlsimgbuilder_tempXXXXXXX" will be created
21+
* to hold the image build context (files, and Dockerfile).
22+
*/
23+
public String imageBuildContextDirectory() {
24+
String result = userSettings.getBuildContextDirectory();
25+
if (Utils.isEmptyString(result)) {
26+
return ".";
27+
}
28+
return result;
29+
}
30+
31+
/**
32+
* Patch download directory.
33+
* The directory for storing and using downloaded patches.
34+
*/
35+
public String patchDirectory() {
36+
String result = userSettings.getPatchDirectory();
37+
if (Utils.isEmptyString(result)) {
38+
return UserSettings.getSettingsDirectory().resolve("patches").toString();
39+
}
40+
return result;
41+
}
42+
43+
/**
44+
* Installer download directory.
45+
* The directory for storing and using downloaded Java and middleware installers.
46+
*/
47+
public String installerDirectory() {
48+
String result = userSettings.getInstallerDirectory();
49+
if (Utils.isEmptyString(result)) {
50+
return UserSettings.getSettingsDirectory().resolve("installers").toString();
51+
}
52+
return result;
53+
}
54+
55+
/**
56+
* Container image build tool.
57+
* Allow the user to specify the executable that will be used to build the container image. For example,
58+
* "/usr/local/bin/docker" or just "docker" if "docker" is on the user's path. For example, "podman" or "docker".
59+
*/
60+
public String buildEngine() {
61+
String result = userSettings.getBuildEngine();
62+
if (Utils.isEmptyString(result)) {
63+
return "docker";
64+
}
65+
return result;
66+
}
67+
68+
/**
69+
* Container image runtime tool.
70+
* Allow the user to specify the executable that will be used to run and/or interrogate images. For example,
71+
* "/usr/local/bin/docker" or just "docker" if "docker" is on the user's path. For example, "podman" or "docker".
72+
*/
73+
public String containerEngine() {
74+
String result = userSettings.getContainerEngine();
75+
if (Utils.isEmptyString(result)) {
76+
return buildEngine();
77+
}
78+
return result;
79+
}
80+
81+
/**
82+
* REST calls to ARU should be retried up to this number of times.
83+
*/
84+
public Integer aruRetryMax() {
85+
Integer result = userSettings.getAruRetryMax();
86+
if (result == null) {
87+
return 10;
88+
}
89+
return result;
90+
}
91+
92+
/**
93+
* The time between each ARU REST call in milliseconds.
94+
*/
95+
public int aruRetryInterval() {
96+
Integer result = userSettings.getAruRetryInterval();
97+
if (result == null) {
98+
return 500;
99+
}
100+
return result;
101+
}
102+
}

0 commit comments

Comments
 (0)