Helm-generator - seedcode delivery for helm chart generation tool from component...
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-cli / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / HelmChartGeneratorApplication.java
1 /*
2  * # ============LICENSE_START=======================================================
3  * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
4  * # ================================================================================
5  * # Licensed under the Apache License, Version 2.0 (the "License");
6  * # you may not use this file except in compliance with the License.
7  * # You may obtain a copy of the License at
8  * #
9  * #      http://www.apache.org/licenses/LICENSE-2.0
10  * #
11  * # Unless required by applicable law or agreed to in writing, software
12  * # distributed under the License is distributed on an "AS IS" BASIS,
13  * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * # See the License for the specific language governing permissions and
15  * # limitations under the License.
16  * # ============LICENSE_END=========================================================
17  */
18
19 package org.onap.dcaegen2.platform.helmchartgenerator;
20
21 import lombok.extern.slf4j.Slf4j;
22 import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.ChartBuilder;
23 import org.onap.dcaegen2.platform.helmchartgenerator.distribution.ChartDistributor;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.boot.CommandLineRunner;
26 import org.springframework.boot.SpringApplication;
27 import org.springframework.boot.autoconfigure.SpringBootApplication;
28 import org.springframework.core.io.ClassPathResource;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36
37 /**
38  * Main class to run the application.
39  * @author Dhrumin Desai
40  */
41 @SpringBootApplication
42 @Slf4j
43 public class HelmChartGeneratorApplication implements CommandLineRunner {
44
45         @Autowired
46         private final ChartBuilder builder;
47
48         @Autowired
49         private final ChartDistributor distributor;
50
51         public HelmChartGeneratorApplication(ChartBuilder builder, ChartDistributor distributor) {
52                 this.builder = builder;
53                 this.distributor = distributor;
54         }
55
56         public static void main(String[] args) {
57                 SpringApplication.run(HelmChartGeneratorApplication.class, args);
58         }
59
60         @Override
61         public void run(String... args) throws Exception {
62                 List<String> argList = new ArrayList<>(Arrays.asList(args));
63                 boolean isDistribute = false;
64                 if(argList.contains("--help") || argList.size() < 3){
65                         printUsage();
66                         return;
67                 }
68                 if(argList.contains("--distribute")){
69                         isDistribute = true;
70                         argList.remove("--distribute");
71                 }
72
73                 log.info("STARTED HELM GENERATION:");
74                 final File chartPackage = builder.build(argList.get(0), argList.get(1), argList.get(2),
75                                 getSpecSchemaLocation(argList));
76                 if(isDistribute) {
77                         log.info("Distributing..");
78                         distributor.distribute(chartPackage);
79                 }
80         }
81
82         private String getSpecSchemaLocation(List<String> argList) {
83                 String specSchemaLocation;
84                 try {
85                         specSchemaLocation = argList.get(3);
86                 }
87                 catch (Exception e) {
88                         specSchemaLocation = "";
89                 }
90                 return specSchemaLocation;
91         }
92
93         private void printUsage() throws IOException {
94                 InputStream inputStream = new ClassPathResource("Usage.txt").getInputStream();
95                 log.info(new String(inputStream.readAllBytes()));
96         }
97 }