// Path: grafana-dashboard-generator-cli/src/main/java/uk/co/szmg/grafana/cli/DashboardGeneratorApplication.java
// public class DashboardGeneratorApplication {
// 
//     private String rootPackage;
// 
//     public DashboardGeneratorApplication(String rootPackage) {
//         this.rootPackage = rootPackage;
//     }
// 
//     public int main(String[] args) throws IOException {
//         try {
//             return safeMain(args);
//         } catch (ExitPlease ex) {
//             return ex.getReturnCode();
//         }
//     }
// 
//     private int safeMain(String[] args) throws IOException, ExitPlease {
//         AnsiConsole.systemInstall();
// 
//         // welcome
//         System.out.println(ansi().render("\n@|blue Welcome to Grafana Dashboard Builder|@\n"));
// 
//         UploaderConfig config = new UploaderConfig();
// 
//         CommandLineArgs commandLineArgs = new CommandLineArgs();
//         commandLineArgs.parse(args, config);
//         initStores(config);
//         commandLineArgs.apply(config);
//         loadDefaults(config);
// 
//         Interaction.askUserForDetails(config);
// 
//         if (config.getEndpoint().isPresent()) {
//             upload(config);
//         } else {
//             writeToFile(config);
//         }
// 
//         return 0;
//     }
// 
//     private void initStores(UploaderConfig config) {
//         // dashboard
//         config.setDashboardStore(new DashboardStore(rootPackage));
//         DashboardStore dashboardStore = config.getDashboardStore();
//         dashboardStore.load();
// 
//         // check duplicates
//         dashboardStore.getDuplicatesErrorMessage().ifPresent(msg -> {
//             System.err.println(msg);
//             throw new ExitPlease(-1);
//         });
// 
//         // endpoint
//         GrafanaEndpointStore endpointStore = config.getEndpointStore();
//         if (endpointStore == null) {
//             endpointStore = new ClasspathGrafanaEndpointStore("/endpoints.yaml");
//             config.setEndpointStore(endpointStore);
//         }
// 
//         try {
//             endpointStore.load();
//         } catch (IOException e) {
//             System.err.println("Could not load endpoint store; " + e.getMessage());
//             throw new ExitPlease(-2);
//         }
// 
//     }
// 
//     private void loadDefaults(UploaderConfig config) {
//         List<Dashboard> dashboards = config.getDashboardStore().getDashboards();
//         if (config.getDashboards() == null && dashboards.size() == 1) {
//             config.setDashboards(dashboards);
//         }
// 
//         if (config.getEndpointStore().getEndpoints().isEmpty()) {
//             config.setEndpoint(Optional.empty());
//         }
// 
//         if (config.getOutputDirectory() == null) {
//             config.setOutputDirectory(new File("./output"));
//         }
// 
//         if (config.isBatchMode() && config.getOverwrite() == null) {
//             config.setOverwrite(false);
//         }
//     }
// 
//     private void upload(UploaderConfig config) {
//         boolean overwrite = config.getOverwrite();
//         DashboardUploader dashboardUploader = new DashboardUploader(config.getEndpoint().get());
//         for (Dashboard dashboard : config.getDashboards()) {
//             System.out.printf("%s: ", dashboard.getTitle());
// 
//             try {
//                 dashboardUploader.upload(dashboard, overwrite);
//                 System.out.println(ansi().render("@|green done|@"));
//             } catch (RuntimeException e) {
//                 boolean alreadyExists = false;
//                 if (e.getCause() instanceof GrafanaClient.UnexpectedGrafanaResponseException) {
//                     GrafanaClient.UnexpectedGrafanaResponseException ex = (GrafanaClient.UnexpectedGrafanaResponseException) e.getCause();
//                     alreadyExists = ex.getResponseCode() == 412;
//                 }
// 
//                 if (alreadyExists && !overwrite) {
//                     System.out.println(ansi().render("@|red already exists|@"));
//                 } else {
//                     throw e;
//                 }
//             }
//         }
//     }
// 
//     private void writeToFile(UploaderConfig config) throws IOException {
//         DashboardSerializer dashboardSerializer = new DashboardSerializer();
//         File outputDirectory = config.getOutputDirectory();
//         outputDirectory.mkdirs();
//         for (Dashboard dashboard : config.getDashboards()) {
//             System.out.printf("%s: ", dashboard.getTitle());
// 
//             File f = new File(outputDirectory, filenameFor(dashboard));
//             if (f.exists() && !config.getOverwrite()) {
//                 System.out.println(ansi().render("@|red already exists|@"));
//             } else {
//                 try (FileOutputStream stream = new FileOutputStream(f)) {
//                     dashboardSerializer.write(dashboard, stream);
//                 }
//                 System.out.println(ansi().render("@|green done|@"));
//             }
// 
//         }
// 
//     }
// 
//     private String filenameFor(Dashboard dashboard) {
//         return dashboard.getTitle().replaceAll("[\\s:/\\\\]+", "-") + ".json";
//     }
// 
// }


import java.io.IOException;
import uk.co.szmg.grafana.cli.DashboardGeneratorApplication;
package uk.co.szmg.grafana.example;

/*-
 * #%L
 * grafana-dashboard-generator-example
 * %%
 * Copyright (C) 2017 Mate Gabor Szvoboda
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */



public class Main {

    public static void main(String[] args) throws IOException {
// Path: grafana-dashboard-generator-cli/src/main/java/uk/co/szmg/grafana/cli/DashboardGeneratorApplication.java
// public class DashboardGeneratorApplication {
// 
//     private String rootPackage;
// 
//     public DashboardGeneratorApplication(String rootPackage) {
//         this.rootPackage = rootPackage;
//     }
// 
//     public int main(String[] args) throws IOException {
//         try {
//             return safeMain(args);
//         } catch (ExitPlease ex) {
//             return ex.getReturnCode();
//         }
//     }
// 
//     private int safeMain(String[] args) throws IOException, ExitPlease {
//         AnsiConsole.systemInstall();
// 
//         // welcome
//         System.out.println(ansi().render("\n@|blue Welcome to Grafana Dashboard Builder|@\n"));
// 
//         UploaderConfig config = new UploaderConfig();
// 
//         CommandLineArgs commandLineArgs = new CommandLineArgs();
//         commandLineArgs.parse(args, config);
//         initStores(config);
//         commandLineArgs.apply(config);
//         loadDefaults(config);
// 
//         Interaction.askUserForDetails(config);
// 
//         if (config.getEndpoint().isPresent()) {
//             upload(config);
//         } else {
//             writeToFile(config);
//         }
// 
//         return 0;
//     }
// 
//     private void initStores(UploaderConfig config) {
//         // dashboard
//         config.setDashboardStore(new DashboardStore(rootPackage));
//         DashboardStore dashboardStore = config.getDashboardStore();
//         dashboardStore.load();
// 
//         // check duplicates
//         dashboardStore.getDuplicatesErrorMessage().ifPresent(msg -> {
//             System.err.println(msg);
//             throw new ExitPlease(-1);
//         });
// 
//         // endpoint
//         GrafanaEndpointStore endpointStore = config.getEndpointStore();
//         if (endpointStore == null) {
//             endpointStore = new ClasspathGrafanaEndpointStore("/endpoints.yaml");
//             config.setEndpointStore(endpointStore);
//         }
// 
//         try {
//             endpointStore.load();
//         } catch (IOException e) {
//             System.err.println("Could not load endpoint store; " + e.getMessage());
//             throw new ExitPlease(-2);
//         }
// 
//     }
// 
//     private void loadDefaults(UploaderConfig config) {
//         List<Dashboard> dashboards = config.getDashboardStore().getDashboards();
//         if (config.getDashboards() == null && dashboards.size() == 1) {
//             config.setDashboards(dashboards);
//         }
// 
//         if (config.getEndpointStore().getEndpoints().isEmpty()) {
//             config.setEndpoint(Optional.empty());
//         }
// 
//         if (config.getOutputDirectory() == null) {
//             config.setOutputDirectory(new File("./output"));
//         }
// 
//         if (config.isBatchMode() && config.getOverwrite() == null) {
//             config.setOverwrite(false);
//         }
//     }
// 
//     private void upload(UploaderConfig config) {
//         boolean overwrite = config.getOverwrite();
//         DashboardUploader dashboardUploader = new DashboardUploader(config.getEndpoint().get());
//         for (Dashboard dashboard : config.getDashboards()) {
//             System.out.printf("%s: ", dashboard.getTitle());
// 
//             try {
//                 dashboardUploader.upload(dashboard, overwrite);
//                 System.out.println(ansi().render("@|green done|@"));
//             } catch (RuntimeException e) {
//                 boolean alreadyExists = false;
//                 if (e.getCause() instanceof GrafanaClient.UnexpectedGrafanaResponseException) {
//                     GrafanaClient.UnexpectedGrafanaResponseException ex = (GrafanaClient.UnexpectedGrafanaResponseException) e.getCause();
//                     alreadyExists = ex.getResponseCode() == 412;
//                 }
// 
//                 if (alreadyExists && !overwrite) {
//                     System.out.println(ansi().render("@|red already exists|@"));
//                 } else {
//                     throw e;
//                 }
//             }
//         }
//     }
// 
//     private void writeToFile(UploaderConfig config) throws IOException {
//         DashboardSerializer dashboardSerializer = new DashboardSerializer();
//         File outputDirectory = config.getOutputDirectory();
//         outputDirectory.mkdirs();
//         for (Dashboard dashboard : config.getDashboards()) {
//             System.out.printf("%s: ", dashboard.getTitle());
// 
//             File f = new File(outputDirectory, filenameFor(dashboard));
//             if (f.exists() && !config.getOverwrite()) {
//                 System.out.println(ansi().render("@|red already exists|@"));
//             } else {
//                 try (FileOutputStream stream = new FileOutputStream(f)) {
//                     dashboardSerializer.write(dashboard, stream);
//                 }
//                 System.out.println(ansi().render("@|green done|@"));
//             }
// 
//         }
// 
//     }
// 
//     private String filenameFor(Dashboard dashboard) {
//         return dashboard.getTitle().replaceAll("[\\s:/\\\\]+", "-") + ".json";
//     }
// 
// }


// Path: grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/Main.java
import java.io.IOException;
import uk.co.szmg.grafana.cli.DashboardGeneratorApplication;

package uk.co.szmg.grafana.example;

/*-
 * #%L
 * grafana-dashboard-generator-example
 * %%
 * Copyright (C) 2017 Mate Gabor Szvoboda
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */



public class Main {

    public static void main(String[] args) throws IOException {
        new DashboardGeneratorApplication("uk.co.szmg.grafana.example.dashboards").main(args);