github.com/datreeio/datree@v1.9.22-rc/main.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/datreeio/datree/pkg/evaluation"
     9  
    10  	"github.com/datreeio/datree/bl/files"
    11  
    12  	"github.com/datreeio/datree/pkg/ciContext"
    13  	"github.com/datreeio/datree/pkg/deploymentConfig"
    14  
    15  	"github.com/datreeio/datree/pkg/executor"
    16  	"github.com/datreeio/datree/pkg/fileReader"
    17  	"github.com/datreeio/datree/pkg/jsonSchemaValidator"
    18  	"github.com/datreeio/datree/pkg/networkValidator"
    19  
    20  	"github.com/datreeio/datree/pkg/cliClient"
    21  	"github.com/datreeio/datree/pkg/localConfig"
    22  	"github.com/datreeio/datree/pkg/printer"
    23  	"github.com/datreeio/datree/pkg/utils"
    24  
    25  	"github.com/datreeio/datree/bl/errorReporter"
    26  	"github.com/datreeio/datree/bl/messager"
    27  	"github.com/datreeio/datree/bl/validation"
    28  	"github.com/datreeio/datree/cmd"
    29  	"github.com/datreeio/datree/cmd/test"
    30  )
    31  
    32  const DEFAULT_ERR_EXIT_CODE = 1
    33  const VIOLATIONS_FOUND_EXIT_CODE = 2
    34  
    35  func main() {
    36  	validator := networkValidator.NewNetworkValidator()
    37  	cliClient := cliClient.NewCliClient(deploymentConfig.URL, validator)
    38  	localConfig := localConfig.NewLocalConfigClient(cliClient, validator)
    39  	ciContext := ciContext.Extract()
    40  
    41  	reporter := errorReporter.NewErrorReporter(cliClient, localConfig)
    42  	globalPrinter := printer.CreateNewPrinter()
    43  
    44  	app := &cmd.App{
    45  		Context: &cmd.Context{
    46  			LocalConfig:         localConfig,
    47  			CiContext:           ciContext,
    48  			Evaluator:           evaluation.New(cliClient, ciContext),
    49  			CliClient:           cliClient,
    50  			Messager:            messager.New(cliClient),
    51  			Printer:             globalPrinter,
    52  			Reader:              fileReader.CreateFileReader(nil),
    53  			K8sValidator:        validation.New(),
    54  			JSONSchemaValidator: jsonSchemaValidator.New(),
    55  			CommandRunner:       executor.CreateNewCommandRunner(),
    56  			FilesExtractor:      files.New(),
    57  		},
    58  	}
    59  
    60  	cmd := cmd.NewRootCommand(app)
    61  
    62  	defer func() {
    63  		if panicErr := recover(); panicErr != nil {
    64  			reporter.ReportPanicError(panicErr)
    65  
    66  			globalPrinter.PrintMessage(fmt.Sprintf("Unexpected error: %s\n", utils.ParseErrorToString(panicErr)), "error")
    67  			os.Exit(DEFAULT_ERR_EXIT_CODE)
    68  		}
    69  	}()
    70  
    71  	if err := cmd.Execute(); err != nil {
    72  		if errors.Is(err, test.ViolationsFoundError) {
    73  			os.Exit(VIOLATIONS_FOUND_EXIT_CODE)
    74  		}
    75  		reporter.ReportUnexpectedError(err)
    76  		os.Exit(DEFAULT_ERR_EXIT_CODE)
    77  	}
    78  }