|  | 
|  | 1 | +// Unless explicitly stated otherwise all files in this repository are licensed | 
|  | 2 | +// under the Apache License Version 2.0. | 
|  | 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). | 
|  | 4 | +// Copyright 2023-present Datadog, Inc. | 
|  | 5 | + | 
|  | 6 | +package cmd | 
|  | 7 | + | 
|  | 8 | +import ( | 
|  | 9 | +	"fmt" | 
|  | 10 | +	"os" | 
|  | 11 | +	"slices" | 
|  | 12 | +	"strings" | 
|  | 13 | +	"text/template" | 
|  | 14 | + | 
|  | 15 | +	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer" | 
|  | 16 | +	"github.com/polyfloyd/go-errorlint/errorlint" | 
|  | 17 | +	"github.com/urfave/cli/v2" | 
|  | 18 | +	"golang.org/x/tools/go/analysis" | 
|  | 19 | +	"golang.org/x/tools/go/analysis/multichecker" | 
|  | 20 | +) | 
|  | 21 | + | 
|  | 22 | +var Lint = &cli.Command{ | 
|  | 23 | +	Name:            "lint", | 
|  | 24 | +	Usage:           "Run selected static analysis checks on Go code for Orchestrion to work better for certain features.", | 
|  | 25 | +	UsageText:       "orchestrion lint [lint arguments...]", | 
|  | 26 | +	Args:            true, | 
|  | 27 | +	SkipFlagParsing: true, | 
|  | 28 | +	Action: func(clictx *cli.Context) (err error) { | 
|  | 29 | +		span, _ := tracer.StartSpanFromContext(clictx.Context, "lint", | 
|  | 30 | +			tracer.ResourceName(strings.Join(clictx.Args().Slice(), " ")), | 
|  | 31 | +		) | 
|  | 32 | +		defer func() { span.Finish(tracer.WithError(err)) }() | 
|  | 33 | + | 
|  | 34 | +		// Check if help was requested and print Orchestrion-style header. | 
|  | 35 | +		args := clictx.Args().Slice() | 
|  | 36 | +		if slices.Contains(args, "-help") || slices.Contains(args, "--help") || slices.Contains(args, "-h") { | 
|  | 37 | +			tmpl := template.Must(template.New("help").Parse(cli.CommandHelpTemplate)) | 
|  | 38 | +			if err := tmpl.Execute(os.Stdout, clictx.Command); err != nil { | 
|  | 39 | +				fmt.Printf("NAME:\n   orchestrion lint - %s\n\n", clictx.Command.Usage) | 
|  | 40 | +				fmt.Printf("USAGE:\n   %s\n\n", clictx.Command.UsageText) | 
|  | 41 | +				fmt.Println() | 
|  | 42 | +			} | 
|  | 43 | +		} | 
|  | 44 | + | 
|  | 45 | +		// Set up os.Args to include the lint subcommand args. | 
|  | 46 | +		// Replace "orchestrion lint" with "orchestrion-lint", | 
|  | 47 | +		// so multichecker sees proper args | 
|  | 48 | +		args = append([]string{"orchestrion-lint"}, args...) | 
|  | 49 | +		os.Args = args | 
|  | 50 | + | 
|  | 51 | +		// Run multichecker. This will take over with its own flags. | 
|  | 52 | +		analyzers := []*analysis.Analyzer{ | 
|  | 53 | +			errorlint.NewAnalyzer( | 
|  | 54 | +				errorlint.WithComparison(true), | 
|  | 55 | +				errorlint.WithAsserts(true), | 
|  | 56 | +			), | 
|  | 57 | +		} | 
|  | 58 | +		multichecker.Main(analyzers...) | 
|  | 59 | + | 
|  | 60 | +		return nil | 
|  | 61 | +	}, | 
|  | 62 | +} | 
0 commit comments