This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git

commit fcd97abb68f88a10d10e32d0b49102a40b9108b4
Author: Daniel Qian <chanjars...@gmail.com>
AuthorDate: Wed Nov 6 09:55:34 2019 +0800

    SCB-1553 Integrate oas-validator compliance check to cli
    Add abbr for checkstyle subcommand: cs
---
 .../apache/servicecomb/toolkit/cli/CheckStyle.java | 103 +--------------------
 .../servicecomb/toolkit/cli/CheckStyleAbbr.java    |  26 ++++++
 .../cli/{CheckStyle.java => CheckStyleBase.java}   |   7 +-
 .../servicecomb/toolkit/cli/ToolkitMain.java       |   2 +-
 .../apache/servicecomb/toolkit/cli/CliTest.java    |   9 ++
 5 files changed, 40 insertions(+), 107 deletions(-)

diff --git 
a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java 
b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java
index 2ea2f99..6cd820b 100644
--- a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java
@@ -17,109 +17,10 @@
 
 package org.apache.servicecomb.toolkit.cli;
 
-import java.io.IOException;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.List;
-
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.servicecomb.toolkit.oasv.common.OasObjectProperty;
-import org.apache.servicecomb.toolkit.oasv.common.OasObjectPropertyLocation;
-import org.apache.servicecomb.toolkit.oasv.compliance.ComplianceCheckParser;
-import 
org.apache.servicecomb.toolkit.oasv.compliance.factory.DefaultOasSpecValidatorFactory;
-import org.apache.servicecomb.toolkit.oasv.validation.api.OasSpecValidator;
-import org.apache.servicecomb.toolkit.oasv.validation.api.OasValidationContext;
-import org.apache.servicecomb.toolkit.oasv.validation.api.OasViolation;
-import 
org.apache.servicecomb.toolkit.oasv.validation.factory.OasSpecValidatorFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import 
org.springframework.context.annotation.AnnotationConfigApplicationContext;
-
-import io.airlift.airline.Arguments;
 import io.airlift.airline.Command;
-import io.swagger.v3.oas.models.OpenAPI;
-import io.swagger.v3.parser.core.models.SwaggerParseResult;
 
 @Command(name = "checkstyle",
-    description = "Check style of OpenAPI v3 spec yaml")
-public class CheckStyle implements Runnable {
-
-  private final static Logger LOGGER = 
LoggerFactory.getLogger(CheckStyle.class);
-
-  @Arguments(
-      title = "file", required = true,
-      description = "OpenAPI v3 spec yaml"
-  )
-  private String filePath;
-
-  @Override
-  public void run() {
-
-    String yaml = null;
-    try {
-      yaml = loadFileContent(filePath);
-    } catch (IOException e) {
-      LOGGER.error(e.getMessage());
-      return;
-    }
-
-    SwaggerParseResult parseResult = ComplianceCheckParser.parseYaml(yaml);
-    OpenAPI openAPI = parseResult.getOpenAPI();
-    if (openAPI == null) {
-      if (CollectionUtils.isNotEmpty(parseResult.getMessages())) {
-        for (String message : parseResult.getMessages()) {
-          LOGGER.error(message);
-        }
-      }
-      LOGGER.error("Parse error");
-      return;
-    }
-
-    OasSpecValidator oasSpecValidator = createOasSpecValidator();
-
-    List<OasViolation> violations = 
oasSpecValidator.validate(createContext(openAPI), openAPI);
-    if (CollectionUtils.isNotEmpty(violations)) {
-      for (OasViolation violation : violations) {
-        LOGGER.info("{}: {}", toPathString(violation.getLocation()), 
violation.getError());
-      }
-      return;
-    }
-    LOGGER.info("Everything is good");
-  }
-
-  private String toPathString(OasObjectPropertyLocation location) {
-    StringBuilder sb = new StringBuilder();
-    List<OasObjectProperty> path = location.getPath();
-    for (OasObjectProperty property : path) {
-      sb.append(property.getName()).append('.');
-    }
-    sb.deleteCharAt(sb.length() - 1);
-    return sb.toString();
-  }
-
-  private OasSpecValidator createOasSpecValidator() {
-    AnnotationConfigApplicationContext ctx = new 
AnnotationConfigApplicationContext(
-        DefaultOasSpecValidatorFactory.class.getPackage().getName());
-    try {
-      OasSpecValidatorFactory oasSpecValidatorFactory = 
ctx.getBean(OasSpecValidatorFactory.class);
-      return oasSpecValidatorFactory.create();
-    } finally {
-      ctx.close();
-    }
-  }
-
-
-  private String loadFileContent(String filePath) throws IOException {
-    Path specPath = Paths.get(filePath);
-    specPath.toAbsolutePath().toString();
-    return FileUtils.readFileToString(specPath.toFile());
-  }
-
-  private OasValidationContext createContext(OpenAPI openAPI) {
+    description = "Check style for OpenAPI v3 spec yaml")
+public class CheckStyle extends CheckStyleBase {
 
-    OasValidationContext oasValidationContext = new 
OasValidationContext(openAPI);
-    return oasValidationContext;
-  }
 }
diff --git 
a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyleAbbr.java 
b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyleAbbr.java
new file mode 100644
index 0000000..eca98a2
--- /dev/null
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyleAbbr.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.servicecomb.toolkit.cli;
+
+import io.airlift.airline.Command;
+
+@Command(name = "cs",
+    description = "Check style for OpenAPI v3 spec yaml(abbr for checkstyle)")
+public class CheckStyleAbbr extends CheckStyleBase {
+
+}
diff --git 
a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java 
b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyleBase.java
similarity index 94%
copy from cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java
copy to cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyleBase.java
index 2ea2f99..e021d9f 100644
--- a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyleBase.java
@@ -24,7 +24,6 @@ import java.util.List;
 
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 import org.apache.servicecomb.toolkit.oasv.common.OasObjectProperty;
 import org.apache.servicecomb.toolkit.oasv.common.OasObjectPropertyLocation;
 import org.apache.servicecomb.toolkit.oasv.compliance.ComplianceCheckParser;
@@ -42,11 +41,9 @@ import io.airlift.airline.Command;
 import io.swagger.v3.oas.models.OpenAPI;
 import io.swagger.v3.parser.core.models.SwaggerParseResult;
 
-@Command(name = "checkstyle",
-    description = "Check style of OpenAPI v3 spec yaml")
-public class CheckStyle implements Runnable {
+public class CheckStyleBase implements Runnable {
 
-  private final static Logger LOGGER = 
LoggerFactory.getLogger(CheckStyle.class);
+  private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
 
   @Arguments(
       title = "file", required = true,
diff --git 
a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java 
b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
index 7bd38f9..39b2477 100755
--- a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
@@ -37,7 +37,7 @@ public class ToolkitMain {
     builder.withDescription("Microservice development toolkit(version " + 
projectVersion
         + "). ");
     builder.withDefaultCommand(Help.class);
-    builder.withCommands(CodeGenerate.class, DocGenerate.class, 
CheckStyle.class, Help.class);
+    builder.withCommands(CodeGenerate.class, DocGenerate.class, 
CheckStyle.class, CheckStyleAbbr.class, Help.class);
     Runnable cmd = builder.build().parse(args);
 
     cmd.run();
diff --git a/cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java 
b/cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
index 3c2fd7b..2068ddb 100755
--- a/cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
+++ b/cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
@@ -100,4 +100,13 @@ public class CliTest {
     ToolkitMain.main(args);
   }
 
+  @Test
+  public void testCheckStyleAbbr() throws IOException {
+    String[] args = new String[] {
+        "cs",
+        
Paths.get("./src/test/resources/oas/parser-test.yaml").toFile().getCanonicalPath()
+    };
+    ToolkitMain.main(args);
+  }
+
 }

Reply via email to