Skip to content

Commit

Permalink
Issue 27799 dot cli do not require the dot service yml (#28389)
Browse files Browse the repository at this point in the history
* #27799 Adding support for a dotCMS URL and bypass the yml file configuration

* #27799 Adding support for a dotCMS URL and bypass the yml file configuration

* #27799 Adding support for a dotCMS URL and bypass the yml file configuration

* #27799 Fixing IT

* #27799 Applying sonarlint feedback

* #27799 Renaming test class

* #27799 Handling nulls

* #27799 Improving cleanup in IT

* #27799 Implemented Resetable interface in API models

Added a new Resetable interface and implemented it in the AuthenticationParam and RemoteURLParam classes. This implementation standardized the way objects reset to their original state across the API models. As a result, removed the reset method from these classes as it's now inherited from the Resetable interface.
  • Loading branch information
jgambarios authored May 2, 2024
1 parent 318564e commit 1e05f0e
Show file tree
Hide file tree
Showing 37 changed files with 529 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dotcms.api;

/**
* The Resetable interface represents an object that can be reset to its original state.
*/
public interface Resetable {

/**
* Resets the object to its original state.
*/
void reset();

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.dotcms.api.client.model;

import com.dotcms.api.Resetable;
import java.util.Optional;


/***
* This interface is used to pass the token from the CLI to the API client.
* If the token is present here we use directly instead of trying to load it from the service manager.
*/
public interface AuthenticationParam {
void setToken(char[] token);
public interface AuthenticationParam extends Resetable {

Optional<char[] > getToken();
void setToken(char[] token);

Optional<char[]> getToken();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AuthenticationParamContextImpl implements AuthenticationParam {

@Override
public void setToken(final char[] token) {
this.token = Arrays.copyOf(token, token.length);
this.token = Arrays.copyOf(token, token.length);
}

public Optional<char[]> getToken() {
Expand All @@ -26,4 +26,9 @@ public Optional<char[]> getToken() {
return Optional.of(token);
}

@Override
public void reset() {
token = null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dotcms.api.client.model;

import com.dotcms.api.Resetable;
import java.net.URL;
import java.util.Optional;

/**
* The RemoteURLParam interface is used in the {@link com.dotcms.cli.common.AuthenticationMixin} to
* set and get the dotCMS URL parameter. It provides methods to set and retrieve the URL.
*/
public interface RemoteURLParam extends Resetable {

void setURL(URL remoteURL);

Optional<URL> getURL();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.dotcms.api.client.model;

import io.quarkus.arc.DefaultBean;
import java.net.URL;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;

/**
* This class is used to pass the token from the CLI to the API client. If the token is present here
* we use directly
*/
@DefaultBean
@ApplicationScoped
public class RemoteURLParamContextImpl implements RemoteURLParam {

URL remoteURL;

@Override
public void setURL(final URL remoteURL) {
this.remoteURL = remoteURL;
}

@Override
public Optional<URL> getURL() {
if (null == remoteURL) {
return Optional.empty();
}
return Optional.of(remoteURL);
}

@Override
public void reset() {
remoteURL = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,43 @@ public class RestClientFactory {
@Inject
ServiceManager serviceManager;

@Inject
RemoteURLParam remoteURLParam;

// Stores a reference to the current selected profile, so we don't have to get it from disk every time
AtomicReference<ServiceBean> profile = new AtomicReference<>();

/**
* Given the selected profile this will return or instantiate a Rest Client
* @param clazz
/**
* Given the selected profile this will return or instantiate a Rest Client
* @param clazz
* @return
* @param <T>
*/
public <T > T getClient( final Class<T> clazz){
* @param <T>
*/
public <T> T getClient(final Class<T> clazz) {

URI uri = getAPIURI(clazz);

return newRestClient(clazz, uri);
}

/**
* Retrieves the API URI, if the dotCMS URL is present in the command line arguments, it will
* use it otherwise it will use the one from the selected profile.
*
* @param clazz The class for which to retrieve the API URI.
* @param <T> The type of the class.
* @return The API URI.
* @throws IllegalStateException If there is an error building the API URI.
*/
private <T> URI getAPIURI(final Class<T> clazz) {

URL remoteURL;

//This injects the dotCMS URL from the command line if present
final Optional<URL> paramRemoteURL = remoteURLParam.getURL();
if (paramRemoteURL.isPresent()) {
remoteURL = paramRemoteURL.get();
} else {

Optional<ServiceBean> optional;
try {
Expand All @@ -52,18 +79,21 @@ public <T > T getClient( final Class<T> clazz){
);
}

URI uri;
try{
uri = toApiURI(optional.get().url());
} catch (URISyntaxException | IOException e) {
throw new IllegalStateException(
String.format("Error building rest client for [%s] ",clazz.getSimpleName()),
e);
}
remoteURL = optional.get().url();
}

return newRestClient(clazz, uri);
URI uri;
try {
uri = toApiURI(remoteURL);
} catch (URISyntaxException | IOException e) {
throw new IllegalStateException(
String.format(
"Error building rest client for [%s] ", clazz.getSimpleName()), e);
}

return uri;
}

public Optional<ServiceBean> getServiceProfile() throws IOException {
ServiceBean val = profile.get();
if (val == null) {
Expand Down
9 changes: 6 additions & 3 deletions tools/dotcms-cli/cli/docs/content-type-find.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ content-type-find - *Use this command to find Content-types.*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*content-type find* [*-h*] [*--non-interactive*] [*-tk*=_<token>_] [[*-n*=_<typeName>_]
[*-s*=_<site>_] [*-o*=_<orderBy>_] [*-d*=_<direction>_] [*-p*=_<page>_]
[*-ps*=_<pageSize>_]]
*content-type find* [*-h*] [*--non-interactive*] [*--dotcms-url*=_<remoteURL>_]
[*-tk*=_<token>_] [[*-n*=_<typeName>_] [*-s*=_<site>_] [*-o*=_<orderBy>_]
[*-d*=_<direction>_] [*-p*=_<page>_] [*-ps*=_<pageSize>_]]

// end::picocli-generated-man-section-synopsis[]

Expand All @@ -39,6 +39,9 @@ Use --name in conjunction with *Filter/Search* Options.
*-tk, --token*=_<token>_::
A dotCMS token to use for authentication.

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-h*, *--help*::
Display this help message.

Expand Down
9 changes: 6 additions & 3 deletions tools/dotcms-cli/cli/docs/content-type-pull.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ content-type-pull - *Retrieves Content-types descriptors*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*content-type pull* [*-h*] [*-ff*] [*-fmt*=_<inputOutputFormat>_]
[*--retry-attempts*=_<retryAttempts>_] [*-tk*=_<token>_]
[*--workspace*=_<file>_] [_idOrName_]
*content-type pull* [*-h*] [*-ff*] [*--dotcms-url*=_<remoteURL>_]
[*-fmt*=_<inputOutputFormat>_] [*--retry-attempts*=_<retryAttempts>_]
[*-tk*=_<token>_] [*--workspace*=_<file>_] [_idOrName_]

// end::picocli-generated-man-section-synopsis[]

Expand Down Expand Up @@ -50,6 +50,9 @@ content-type-pull - *Retrieves Content-types descriptors*
// tag::picocli-generated-man-section-options[]
== Options

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-ff, --fail-fast*::
Stop at first failure and exit the command. By default, this option is disabled, and the command will continue on error.

Expand Down
6 changes: 5 additions & 1 deletion tools/dotcms-cli/cli/docs/content-type-push.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ content-type-push - *Push content types*
== Synopsis

*content-type push* [*-h*] [*-dau*] [*--dry-run*] [*-ff*] [*-rct*]
[*--retry-attempts*=_<retryAttempts>_] [*-tk*=_<token>_] [_path_]
[*--dotcms-url*=_<remoteURL>_] [*--retry-attempts*=_<retryAttempts>_]
[*-tk*=_<token>_] [_path_]

// end::picocli-generated-man-section-synopsis[]

Expand All @@ -38,6 +39,9 @@ This command enables the pushing of content types to the server. It accommodates
*-dau, --disable-auto-update*::
Disable the default behaviour of updating the local file descriptor with the response from the server after a push. When this option is used, the local file will remain in its initial state even after a successful push.

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*--dry-run*::
When this option is enabled, the push process displays information about the changes that would be made on the remote server without actually pushing those changes. No modifications will be made to the remote server. By default, this option is disabled, and the changes will be applied to the remote server.

Expand Down
6 changes: 5 additions & 1 deletion tools/dotcms-cli/cli/docs/content-type-remove.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ content-type-remove - *Use this command to remove Content-types.*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*content-type remove* [*-h*] [*--non-interactive*] [*-tk*=_<token>_] _<idOrVar>_
*content-type remove* [*-h*] [*--non-interactive*] [*--dotcms-url*=_<remoteURL>_]
[*-tk*=_<token>_] _<idOrVar>_

// end::picocli-generated-man-section-synopsis[]

Expand All @@ -37,6 +38,9 @@ content-type-remove - *Use this command to remove Content-types.*
*-tk, --token*=_<token>_::
A dotCMS token to use for authentication.

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-h*, *--help*::
Display this help message.

Expand Down
7 changes: 5 additions & 2 deletions tools/dotcms-cli/cli/docs/files-ls.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ files-ls - *dotCMS Files ls*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*files ls* [*-h*] [*-ee*] [*-ea*=_patterns_] [*-ef*=_patterns_] [*-ia*=_patterns_] [*-if*=_patterns_]
[*-tk*=_<token>_] _path_
*files ls* [*-h*] [*-ee*] [*--dotcms-url*=_<remoteURL>_] [*-ea*=_patterns_] [*-ef*=_patterns_]
[*-ia*=_patterns_] [*-if*=_patterns_] [*-tk*=_<token>_] _path_

// end::picocli-generated-man-section-synopsis[]

Expand All @@ -35,6 +35,9 @@ files-ls - *dotCMS Files ls*
// tag::picocli-generated-man-section-options[]
== Options

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-ea, --excludeAsset*=_patterns_::
Exclude assets matching the given glob patterns. Multiple patterns can be specified, separated by commas.

Expand Down
8 changes: 6 additions & 2 deletions tools/dotcms-cli/cli/docs/files-pull.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ files-pull - *dotCMS Files pull*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*files pull* [*-hp*] [*-ff*] [*-ie*] [*-nr*] [*-ea*=_patterns_] [*-ef*=_patterns_] [*-ia*=_patterns_]
[*-if*=_patterns_] [*--retry-attempts*=_<retryAttempts>_] [*-tk*=_<token>_]
*files pull* [*-hp*] [*-ff*] [*-ie*] [*-nr*] [*--dotcms-url*=_<remoteURL>_] [*-ea*=_patterns_]
[*-ef*=_patterns_] [*-ia*=_patterns_] [*-if*=_patterns_]
[*--retry-attempts*=_<retryAttempts>_] [*-tk*=_<token>_]
[*--workspace*=_<file>_] [_path_]

// end::picocli-generated-man-section-synopsis[]
Expand Down Expand Up @@ -51,6 +52,9 @@ files-pull - *dotCMS Files pull*
// tag::picocli-generated-man-section-options[]
== Options

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-ea, --excludeAsset*=_patterns_::
Exclude assets matching the given glob patterns. Multiple patterns can be specified, separated by commas.

Expand Down
5 changes: 4 additions & 1 deletion tools/dotcms-cli/cli/docs/files-push.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ files-push - *dotCMS Files push*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*files push* [*-h*] [*--dry-run*] [*-ff*] [*-ra*] [*-rf*]
*files push* [*-h*] [*--dry-run*] [*-ff*] [*-ra*] [*-rf*] [*--dotcms-url*=_<remoteURL>_]
[*--retry-attempts*=_<retryAttempts>_] [*-tk*=_<token>_] [_path_]

// end::picocli-generated-man-section-synopsis[]
Expand All @@ -35,6 +35,9 @@ files-push - *dotCMS Files push*
// tag::picocli-generated-man-section-options[]
== Options

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*--dry-run*::
When this option is enabled, the push process displays information about the changes that would be made on the remote server without actually pushing those changes. No modifications will be made to the remote server. By default, this option is disabled, and the changes will be applied to the remote server.

Expand Down
7 changes: 5 additions & 2 deletions tools/dotcms-cli/cli/docs/files-tree.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ files-tree - *dotCMS Files Tree*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*files tree* [*-h*] [*-ee*] [*-d*=_<depth>_] [*-ea*=_patterns_] [*-ef*=_patterns_] [*-ia*=_patterns_]
[*-if*=_patterns_] [*-tk*=_<token>_] _path_
*files tree* [*-h*] [*-ee*] [*-d*=_<depth>_] [*--dotcms-url*=_<remoteURL>_] [*-ea*=_patterns_]
[*-ef*=_patterns_] [*-ia*=_patterns_] [*-if*=_patterns_] [*-tk*=_<token>_] _path_

// end::picocli-generated-man-section-synopsis[]

Expand All @@ -39,6 +39,9 @@ files-tree - *dotCMS Files Tree*
*-d*, *--depth*=_<depth>_::
Limits the depth of the directory tree to <number> levels. The default value is 0, which means that only the files and directories in the root directory are displayed. If the <number> argument is not provided, there is no limit on the depth of the directory tree.

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-ea, --excludeAsset*=_patterns_::
Exclude assets matching the given glob patterns. Multiple patterns can be specified, separated by commas.

Expand Down
5 changes: 4 additions & 1 deletion tools/dotcms-cli/cli/docs/language-find.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ language-find - *Get all existing languages*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*language find* [*-h*] [*-tk*=_<token>_]
*language find* [*-h*] [*--dotcms-url*=_<remoteURL>_] [*-tk*=_<token>_]

// end::picocli-generated-man-section-synopsis[]

Expand All @@ -35,6 +35,9 @@ language-find - *Get all existing languages*
// tag::picocli-generated-man-section-options[]
== Options

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-h*, *--help*::
Display this help message.

Expand Down
5 changes: 4 additions & 1 deletion tools/dotcms-cli/cli/docs/language-pull.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ language-pull - *Retrieves languages descriptors*
// tag::picocli-generated-man-section-synopsis[]
== Synopsis

*language pull* [*-h*] [*-ff*] [*-fmt*=_<inputOutputFormat>_]
*language pull* [*-h*] [*-ff*] [*--dotcms-url*=_<remoteURL>_] [*-fmt*=_<inputOutputFormat>_]
[*--retry-attempts*=_<retryAttempts>_] [*-tk*=_<token>_]
[*--workspace*=_<file>_] [_idOrIso_]

Expand Down Expand Up @@ -50,6 +50,9 @@ language-pull - *Retrieves languages descriptors*
// tag::picocli-generated-man-section-options[]
== Options

*--dotcms-url*=_<remoteURL>_::
The dotCMS URL to connect to. This option must be used along with the token option, which provides the token for the specified dotCMS URL.

*-ff, --fail-fast*::
Stop at first failure and exit the command. By default, this option is disabled, and the command will continue on error.

Expand Down
Loading

0 comments on commit 1e05f0e

Please sign in to comment.