mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
implement basic config methods
This commit is contained in:
@@ -43,8 +43,6 @@ import org.kohsuke.args4j.Option;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.client.ScmClient;
|
||||
import sonia.scm.client.ScmClientSession;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
@@ -141,6 +139,8 @@ public class App
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
loadConfig();
|
||||
|
||||
I18n i18n = new I18n();
|
||||
|
||||
if ((args.length == 0) || (subcommand == null) || help)
|
||||
@@ -149,25 +149,8 @@ public class App
|
||||
}
|
||||
else
|
||||
{
|
||||
ScmClientSession session = null;
|
||||
|
||||
if (subcommand.isSessionRequired())
|
||||
{
|
||||
session = createSession();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
subcommand.init(input, output, i18n, session);
|
||||
subcommand.run(arguments);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (session != null)
|
||||
{
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
subcommand.init(input, output, i18n, config);
|
||||
subcommand.run(arguments);
|
||||
}
|
||||
|
||||
IOUtil.close(input);
|
||||
@@ -177,27 +160,41 @@ public class App
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private ScmClientSession createSession()
|
||||
private void loadConfig()
|
||||
{
|
||||
ScmClientSession session = null;
|
||||
|
||||
if (Util.isNotEmpty(username) && Util.isNotEmpty(password))
|
||||
if (config == null)
|
||||
{
|
||||
session = ScmClient.createSession(serverUrl, username, password);
|
||||
}
|
||||
else
|
||||
{
|
||||
session = ScmClient.createSession(serverUrl);
|
||||
}
|
||||
config = ScmClientConfig.getInstance().getDefaultConfig();
|
||||
|
||||
return session;
|
||||
if (Util.isNotEmpty(serverUrl))
|
||||
{
|
||||
config.setServerUrl(serverUrl);
|
||||
}
|
||||
|
||||
if (Util.isNotEmpty(username))
|
||||
{
|
||||
config.setUsername(username);
|
||||
}
|
||||
|
||||
if (Util.isNotEmpty(password))
|
||||
{
|
||||
config.setPassword(password);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
@Option(
|
||||
name = "--config",
|
||||
usage = "optionConfig",
|
||||
metaVar = "metaVar_config",
|
||||
aliases = { "-c" }
|
||||
)
|
||||
private ServerConfig config;
|
||||
|
||||
/** Field description */
|
||||
@Option(
|
||||
name = "--help",
|
||||
@@ -236,7 +233,7 @@ public class App
|
||||
@Argument(
|
||||
index = 0,
|
||||
metaVar = "metaVar_command",
|
||||
handler = SubCommandHandler.class
|
||||
handler = SubCommandOptionHandler.class
|
||||
)
|
||||
private SubCommand subcommand;
|
||||
|
||||
|
||||
@@ -49,5 +49,4 @@ public @interface Command
|
||||
{
|
||||
String value() default "";
|
||||
String usage() default "";
|
||||
boolean sessionRequired() default true;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,6 @@ public class CommandDescriptor
|
||||
{
|
||||
this.name = cmd.value();
|
||||
this.usage = cmd.usage();
|
||||
this.sessionRequired = cmd.sessionRequired();
|
||||
}
|
||||
|
||||
if (Util.isEmpty(name))
|
||||
@@ -114,7 +113,6 @@ public class CommandDescriptor
|
||||
{
|
||||
command = commandClass.newInstance();
|
||||
command.setName(name);
|
||||
command.setSessionRequired(sessionRequired);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.kohsuke.args4j.CmdLineException;
|
||||
import org.kohsuke.args4j.CmdLineParser;
|
||||
import org.kohsuke.args4j.OptionDef;
|
||||
import org.kohsuke.args4j.spi.OptionHandler;
|
||||
import org.kohsuke.args4j.spi.Parameters;
|
||||
import org.kohsuke.args4j.spi.Setter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class ConfigOptionHandler extends OptionHandler<ServerConfig>
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param parser
|
||||
* @param option
|
||||
* @param setter
|
||||
*/
|
||||
public ConfigOptionHandler(CmdLineParser parser, OptionDef option,
|
||||
Setter<? super ServerConfig> setter)
|
||||
{
|
||||
super(parser, option, setter);
|
||||
loadClientConfig();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param parameters
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws CmdLineException
|
||||
*/
|
||||
@Override
|
||||
public int parseArguments(Parameters parameters) throws CmdLineException
|
||||
{
|
||||
String name = parameters.getParameter(0);
|
||||
ServerConfig config = clientConfig.getConfig(name);
|
||||
|
||||
setter.addValue(config);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getDefaultMetaVariable()
|
||||
{
|
||||
return "metaVar_config";
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
private void loadClientConfig()
|
||||
{
|
||||
|
||||
// todo
|
||||
clientConfig = new ScmClientConfig();
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private ScmClientConfig clientConfig;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class ScmClientConfig
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String DEFAULT_NAME = "default";
|
||||
|
||||
/** Field description */
|
||||
private static volatile ScmClientConfig instance;
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public ScmClientConfig()
|
||||
{
|
||||
this.serverConfigMap = new HashMap<String, ServerConfig>();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ScmClientConfig getInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
synchronized (ScmClientConfig.class)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
|
||||
// TODO load config
|
||||
instance = new ScmClientConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ServerConfig getConfig(String name)
|
||||
{
|
||||
ServerConfig config = serverConfigMap.get(name);
|
||||
|
||||
if (config == null)
|
||||
{
|
||||
config = new ServerConfig();
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ServerConfig getDefaultConfig()
|
||||
{
|
||||
return getConfig(DEFAULT_NAME);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Map<String, ServerConfig> serverConfigMap;
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Validateable;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class ServerConfig implements Validateable
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public ServerConfig() {}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param serverUrl
|
||||
* @param username
|
||||
* @param password
|
||||
*/
|
||||
public ServerConfig(String serverUrl, String username, String password)
|
||||
{
|
||||
this.serverUrl = serverUrl;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getPassword()
|
||||
{
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getServerUrl()
|
||||
{
|
||||
return serverUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getUsername()
|
||||
{
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean isValid()
|
||||
{
|
||||
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param password
|
||||
*/
|
||||
public void setPassword(String password)
|
||||
{
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param serverUrl
|
||||
*/
|
||||
public void setServerUrl(String serverUrl)
|
||||
{
|
||||
this.serverUrl = serverUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param username
|
||||
*/
|
||||
public void setUsername(String username)
|
||||
{
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private String password;
|
||||
|
||||
/** Field description */
|
||||
private String serverUrl;
|
||||
|
||||
/** Field description */
|
||||
private String username;
|
||||
}
|
||||
@@ -42,7 +42,10 @@ import org.kohsuke.args4j.Option;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.client.ScmClient;
|
||||
import sonia.scm.client.ScmClientSession;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -77,15 +80,15 @@ public abstract class SubCommand
|
||||
* @param input
|
||||
* @param output
|
||||
* @param i18n
|
||||
* @param session
|
||||
* @param config
|
||||
*/
|
||||
public void init(BufferedReader input, PrintWriter output, I18n i18n,
|
||||
ScmClientSession session)
|
||||
ServerConfig config)
|
||||
{
|
||||
this.input = input;
|
||||
this.output = output;
|
||||
this.i18n = i18n;
|
||||
this.session = session;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +112,14 @@ public abstract class SubCommand
|
||||
}
|
||||
else
|
||||
{
|
||||
run();
|
||||
try
|
||||
{
|
||||
run();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (CmdLineException ex)
|
||||
@@ -133,17 +143,6 @@ public abstract class SubCommand
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isSessionRequired()
|
||||
{
|
||||
return sessionRequired;
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -157,19 +156,36 @@ public abstract class SubCommand
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sessionRequired
|
||||
* @return
|
||||
*/
|
||||
public void setSessionRequired(boolean sessionRequired)
|
||||
protected ScmClientSession createSession()
|
||||
{
|
||||
this.sessionRequired = sessionRequired;
|
||||
if (Util.isNotEmpty(config.getUsername())
|
||||
&& Util.isNotEmpty(config.getPassword()))
|
||||
{
|
||||
session = ScmClient.createSession(config.getServerUrl(),
|
||||
config.getUsername(),
|
||||
config.getPassword());
|
||||
}
|
||||
else
|
||||
{
|
||||
session = ScmClient.createSession(config.getServerUrl());
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
protected ServerConfig config;
|
||||
|
||||
/** Field description */
|
||||
protected I18n i18n;
|
||||
|
||||
@@ -192,7 +208,4 @@ public abstract class SubCommand
|
||||
aliases = { "-h" }
|
||||
)
|
||||
private boolean help = false;
|
||||
|
||||
/** Field description */
|
||||
private boolean sessionRequired = true;
|
||||
}
|
||||
|
||||
@@ -61,16 +61,16 @@ import java.util.Map;
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class SubCommandHandler extends OptionHandler<SubCommand>
|
||||
public class SubCommandOptionHandler extends OptionHandler<SubCommand>
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String RESOURCE_SERVICES =
|
||||
"/META-INF/services/".concat(SubCommand.class.getName());
|
||||
|
||||
/** the logger for SubCommandHandler */
|
||||
/** the logger for SubCommandOptionHandler */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(SubCommandHandler.class);
|
||||
LoggerFactory.getLogger(SubCommandOptionHandler.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
@@ -82,7 +82,7 @@ public class SubCommandHandler extends OptionHandler<SubCommand>
|
||||
* @param option
|
||||
* @param setter
|
||||
*/
|
||||
public SubCommandHandler(CmdLineParser parser, OptionDef option,
|
||||
public SubCommandOptionHandler(CmdLineParser parser, OptionDef option,
|
||||
Setter<? super SubCommand> setter)
|
||||
{
|
||||
super(parser, option, setter);
|
||||
Reference in New Issue
Block a user