001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.boot;
019
020import io.stallion.plugins.StallionJavaPlugin;
021import io.stallion.settings.Settings;
022import io.stallion.settings.StrictnessLevel;
023import org.kohsuke.args4j.Argument;
024import org.kohsuke.args4j.Option;
025
026
027import java.util.ArrayList;
028import java.util.List;
029
030import static io.stallion.utils.Literals.list;
031
032
033public class CommandOptionsBase {
034    public static final String ALLOWED_LEVELS = "Allowed values: OFF, SEVERE, WARNING, INFO, FINE, FINER, FINEST";
035
036    private String action = "";
037    @Option(name="-targetPath", usage="The path to your project directory with the settings.toml file in it")
038    private String targetPath = "";
039    @Option(name="-logLevel", usage="How verbose the logging is. " + ALLOWED_LEVELS)
040    private String logLevel = "";
041    @Option(name="-loggingAlwaysIncludesLineNumber", usage="If true, even FINE and INFO log statements will use the stack frame to find the log line and class")
042    private boolean loggingAlwaysIncludesLineNumber = false;
043
044    @Option(name="-strictnessLevel", usage="If strict, will throw exceptions for errors such as missing templates, bad configs, etc. If lax, will eat the errors and try to keep running anyway.")
045    private StrictnessLevel strictnessLevel;
046    @Option(name="-autoReload", usage="If a javascript file is touched, the entire server will reload. Use this only during development")
047    private boolean autoReload = false;
048
049    @Option(name="-env", usage="The environment you are running in. The file settings.(env).toml will be merged into your settings.")        // no usage
050    private String env = "local";
051
052    @Option(name="-devMode", usage="Set to 'true' if you want to use the development URL for resource assets")
053    private Boolean devMode = false;
054
055    @Argument(index = 0)
056    private List<String> arguments = new ArrayList<String>();
057
058    private List<StallionJavaPlugin> extraPlugins = list();
059
060
061    public Settings hydrateSettings(Settings settings) {
062        if (strictnessLevel != null) {
063            settings.setStrictnessLevel(strictnessLevel);
064        }
065        return settings;
066    }
067
068
069    public String getTargetPath() {
070        return targetPath;
071    }
072
073    public void setTargetPath(String targetPath) {
074        this.targetPath = targetPath;
075    }
076
077
078    public String getAction() {
079        if (getArguments().size() == 0) {
080            return "";
081        }
082        return getArguments().get(0);
083    }
084
085    public String getActionTarget() {
086        if (getArguments().size() <= 1) {
087            return "";
088        }
089        return getArguments().get(1);
090    }
091
092
093    public void setAction(String action) {
094        this.action = action;
095    }
096
097    public List<String> getArguments() {
098        return arguments;
099    }
100
101
102    public void setArguments(List<String> arguments) {
103        this.arguments = arguments;
104    }
105
106
107    public String getLogLevel() {
108        return logLevel;
109    }
110
111    public void setLogLevel(String logLevel) {
112        this.logLevel = logLevel;
113    }
114
115    public List<StallionJavaPlugin> getExtraPlugins() {
116        return extraPlugins;
117    }
118
119    public void setExtraPlugins(List<StallionJavaPlugin> extraPlugins) {
120        this.extraPlugins = extraPlugins;
121    }
122
123    public boolean isAutoReload() {
124        return autoReload;
125    }
126
127    public CommandOptionsBase setAutoReload(boolean autoReload) {
128        this.autoReload = autoReload;
129        return this;
130    }
131
132
133    public String getEnv() {
134        return env;
135    }
136
137
138
139    public void setEnv(String env) {
140        this.env = env;
141    }
142
143
144    public Boolean isDevMode() {
145        return devMode;
146    }
147
148
149    public void setDevMode(Boolean devMode) {
150        this.devMode = devMode;
151    }
152
153    public boolean isLoggingAlwaysIncludesLineNumber() {
154        return loggingAlwaysIncludesLineNumber;
155    }
156
157    public CommandOptionsBase setLoggingAlwaysIncludesLineNumber(boolean loggingAlwaysIncludesLineNumber) {
158        this.loggingAlwaysIncludesLineNumber = loggingAlwaysIncludesLineNumber;
159        return this;
160    }
161}