gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/main.go (about)

     1  // Copyright 2017 The GoReporter Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //    http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // GoReporter is a Golang tool that does static analysis, unit testing, code
    15  // review and generate code quality report.
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"log"
    24  	"os"
    25  	"runtime"
    26  	"strings"
    27  	"sync"
    28  	"time"
    29  
    30  	"github.com/360EntSecGroup-Skylar/goreporter/engine"
    31  	"github.com/360EntSecGroup-Skylar/goreporter/engine/processbar"
    32  	"github.com/facebookgo/inject"
    33  )
    34  
    35  // Received parameters, you can control some features using:
    36  //
    37  // -p:Specify the relative path of your project(Must Be Relative path),
    38  //    by default, the current path is used
    39  // -r:Specifies the save path for the generated report,
    40  //    by default, the current path is used
    41  // -e:Ignored detection of packages and multiple packages separated by commas.
    42  // -t:Customize the path of the report template, not necessarily using the
    43  //    default report template
    44  // -f:Set the format to generate reports, support text, html and json,not
    45  //    necessarily using the default formate-html.
    46  
    47  const VERSION = "v3.0.0"
    48  
    49  var (
    50  	version        = flag.Bool("version", false, "print GoReporter version.")
    51  	projectPath    = flag.String("p", "", "path of project.")
    52  	reportPath     = flag.String("r", "", "path of report.")
    53  	exceptPackages = flag.String("e", "", "except packages.")
    54  	templatePath   = flag.String("t", "", "report html template path.")
    55  	reportFormat   = flag.String("f", "", "project report format(text/json/html).")
    56  	coresOfCPU     = flag.Int("c", -1, "cores of CPU.")
    57  )
    58  
    59  func main() {
    60  	flag.Parse()
    61  	if *coresOfCPU != -1 && *coresOfCPU <= runtime.NumCPU() {
    62  		runtime.GOMAXPROCS(*coresOfCPU)
    63  	}
    64  	if *version {
    65  		fmt.Printf("GoReporter %s\r\n", VERSION)
    66  		os.Exit(0)
    67  	}
    68  
    69  	if *projectPath == "" {
    70  		log.Fatal("The project path is not specified")
    71  	} else {
    72  		_, err := os.Stat(*projectPath)
    73  		if err != nil {
    74  			log.Fatal("project path is invalid")
    75  		}
    76  	}
    77  
    78  	var templateHtml string
    79  	if *templatePath == "" {
    80  		templateHtml = engine.DefaultTpl
    81  		log.Println("The template path is not specified,and will use the default template")
    82  	} else {
    83  		if !strings.HasSuffix(*templatePath, ".html") {
    84  			log.Println("The template file is not a html template")
    85  		}
    86  		fileData, err := ioutil.ReadFile(*templatePath)
    87  		if err != nil {
    88  			log.Fatal(err)
    89  		} else {
    90  			templateHtml = string(fileData)
    91  		}
    92  	}
    93  
    94  	if *reportPath == "" {
    95  		log.Println("The report path is not specified, and the current path is used by default")
    96  	} else {
    97  		_, err := os.Stat(*reportPath)
    98  		if err != nil {
    99  			log.Fatal("report path is invalid:", err)
   100  		}
   101  	}
   102  
   103  	if *exceptPackages == "" {
   104  		log.Println("There are no packages that are excepted, review all items of the package")
   105  	}
   106  
   107  	synchronizer := &engine.Synchronizer{
   108  		LintersProcessChans:   make(chan int64, 20),
   109  		LintersFinishedSignal: make(chan string, 10),
   110  	}
   111  	syncRW := &sync.RWMutex{}
   112  	waitGW := &engine.WaitGroupWrapper{}
   113  
   114  	reporter := engine.NewReporter(*projectPath, *reportPath, *reportFormat, templateHtml)
   115  	strategyCountCode := &engine.StrategyCountCode{}
   116  	strategyCyclo := &engine.StrategyCyclo{}
   117  	strategyDeadCode := &engine.StrategyDeadCode{}
   118  	strategyDependGraph := &engine.StrategyDependGraph{}
   119  	strategyDepth := &engine.StrategyDepth{}
   120  	strategyImportPackages := &engine.StrategyImportPackages{}
   121  	strategyInterfacer := &engine.StrategyInterfacer{}
   122  	strategySimpleCode := &engine.StrategySimpleCode{}
   123  	strategySpellCheck := &engine.StrategySpellCheck{}
   124  	strategyUnitTest := &engine.StrategyUnitTest{}
   125  	strategyLint := &engine.StrategyLint{}
   126  	strategyGoVet := &engine.StrategyGoVet{}
   127  	strategyGoFmt := &engine.StrategyGoFmt{}
   128  
   129  	if err := inject.Populate(
   130  		reporter,
   131  		synchronizer,
   132  		strategyCountCode,
   133  		strategyCyclo,
   134  		strategyDeadCode,
   135  		strategyDependGraph,
   136  		strategyDepth,
   137  		strategyImportPackages,
   138  		strategyInterfacer,
   139  		strategySimpleCode,
   140  		strategySpellCheck,
   141  		strategyUnitTest,
   142  		strategyLint,
   143  		strategyGoVet,
   144  		strategyGoFmt,
   145  		syncRW,
   146  		waitGW,
   147  	); err != nil {
   148  		log.Fatal(err)
   149  	}
   150  
   151  	reporter.AddLinters(strategyCountCode, strategyCyclo, strategyDeadCode, strategyDependGraph,
   152  		strategyDepth, strategyImportPackages, strategyInterfacer, strategySimpleCode,
   153  		strategySpellCheck, strategyUnitTest, strategyLint, strategyGoVet, strategyGoFmt)
   154  
   155  	go processbar.LinterProcessBar(synchronizer.LintersProcessChans, synchronizer.LintersFinishedSignal)
   156  
   157  	if err := reporter.Report(); err != nil {
   158  		log.Fatal(err)
   159  	}
   160  
   161  	if err := reporter.Render(); err != nil {
   162  		log.Fatal(err)
   163  	}
   164  
   165  	log.Println(fmt.Sprintf("GoReporter Finished,time consuming %vs", time.Since(reporter.StartTime).Seconds()))
   166  }