|
| 1 | +package project |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "golang.org/x/sync/errgroup" |
| 10 | + |
| 11 | + "github.com/shopware/shopware-cli/internal/system" |
| 12 | + "github.com/shopware/shopware-cli/internal/verifier" |
| 13 | +) |
| 14 | + |
| 15 | +var projectValidateCmd = &cobra.Command{ |
| 16 | + Use: "validate", |
| 17 | + Short: "Validate project", |
| 18 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 19 | + reportingFormat, _ := cmd.Flags().GetString("reporter") |
| 20 | + only, _ := cmd.Flags().GetString("only") |
| 21 | + tmpDir, err := os.MkdirTemp(os.TempDir(), "analyse-project-*") |
| 22 | + if err != nil { |
| 23 | + return fmt.Errorf("cannot create temporary directory: %w", err) |
| 24 | + } |
| 25 | + |
| 26 | + projectPath := "" |
| 27 | + |
| 28 | + if len(args) > 0 { |
| 29 | + projectPath = args[0] |
| 30 | + } else { |
| 31 | + projectPath, err = findClosestShopwareProject() |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + projectPath, err = filepath.Abs(projectPath) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("cannot find path: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + if reportingFormat == "" { |
| 43 | + reportingFormat = verifier.DetectDefaultReporter() |
| 44 | + } |
| 45 | + |
| 46 | + if err := system.CopyFiles(projectPath, tmpDir); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + toolCfg, err := verifier.GetConfigFromProject(tmpDir) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + result := verifier.NewCheck() |
| 56 | + |
| 57 | + var gr errgroup.Group |
| 58 | + |
| 59 | + tools := verifier.GetTools() |
| 60 | + |
| 61 | + tools, err = tools.Only(only) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + for _, tool := range tools { |
| 67 | + tool := tool |
| 68 | + gr.Go(func() error { |
| 69 | + return tool.Check(cmd.Context(), result, *toolCfg) |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + if err := gr.Wait(); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + return verifier.DoCheckReport(result.RemoveByIdentifier(toolCfg.ValidationIgnores), reportingFormat) |
| 78 | + }, |
| 79 | +} |
| 80 | + |
| 81 | +func init() { |
| 82 | + projectRootCmd.AddCommand(projectValidateCmd) |
| 83 | + projectValidateCmd.PersistentFlags().String("reporter", "", "Reporting format (summary, json, github, junit, markdown)") |
| 84 | + projectValidateCmd.PersistentFlags().String("only", "", "Run only specific tools by name (comma-separated, e.g. phpstan,eslint)") |
| 85 | +} |
0 commit comments