|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +package build |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +// BuildResult represents the result of a build operation |
| 17 | +type BuildResult struct { |
| 18 | + Success bool `json:"success"` |
| 19 | + Message string `json:"message"` |
| 20 | + Path string `json:"path,omitempty"` |
| 21 | + BuildOutput string `json:"build_output,omitempty"` |
| 22 | + VetOutput string `json:"vet_output,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +// Command returns the build command |
| 26 | +func Command() *cobra.Command { |
| 27 | + var outputFormat string |
| 28 | + var verbose bool |
| 29 | + |
| 30 | + buildCmd := &cobra.Command{ |
| 31 | + Use: "build <folder-path>", |
| 32 | + Short: "Build and vet Go packages in the specified folder", |
| 33 | + Long: `Build and vet Go packages in the specified folder using 'go build' and 'go vet'. |
| 34 | +
|
| 35 | +This command will: |
| 36 | +1. Run 'go build' to compile the Go packages in the specified folder |
| 37 | +2. Run 'go vet' to check for common Go programming errors |
| 38 | +3. Report any issues found during build or vet process |
| 39 | +
|
| 40 | +The command recursively processes all Go packages found in the specified folder. |
| 41 | +
|
| 42 | +Examples: |
| 43 | + # Build and vet packages in a specific folder |
| 44 | + generator build /path/to/generated/sdk/folder |
| 45 | +
|
| 46 | + # Build with verbose output |
| 47 | + generator build /path/to/generated/sdk/folder --verbose |
| 48 | +
|
| 49 | + # Build with JSON output |
| 50 | + generator build /path/to/generated/sdk/folder --output json`, |
| 51 | + Args: cobra.ExactArgs(1), |
| 52 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | + folderPath := args[0] |
| 54 | + |
| 55 | + // Validate that the path exists and is a directory |
| 56 | + if err := validatePath(folderPath); err != nil { |
| 57 | + return fmt.Errorf("path validation error: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Perform build and vet |
| 61 | + result, err := buildAndVet(folderPath) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("build operation failed: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + // Output result |
| 67 | + switch outputFormat { |
| 68 | + case "json": |
| 69 | + jsonResult, err := json.MarshalIndent(result, "", " ") |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("failed to marshal result: %v", err) |
| 72 | + } |
| 73 | + fmt.Println(string(jsonResult)) |
| 74 | + default: |
| 75 | + // Human-readable output |
| 76 | + if result.Success { |
| 77 | + fmt.Printf("✓ Build and vet completed successfully!\n\n") |
| 78 | + fmt.Printf("Path: %s\n", result.Path) |
| 79 | + if verbose && result.BuildOutput != "" { |
| 80 | + fmt.Printf("\nBuild Output:\n%s\n", result.BuildOutput) |
| 81 | + } |
| 82 | + if verbose && result.VetOutput != "" { |
| 83 | + fmt.Printf("\nVet Output:\n%s\n", result.VetOutput) |
| 84 | + } |
| 85 | + } else { |
| 86 | + fmt.Printf("✗ Build and vet failed: %s\n", result.Message) |
| 87 | + if result.BuildOutput != "" { |
| 88 | + fmt.Printf("\nBuild Output:\n%s\n", result.BuildOutput) |
| 89 | + } |
| 90 | + if result.VetOutput != "" { |
| 91 | + fmt.Printf("\nVet Output:\n%s\n", result.VetOutput) |
| 92 | + } |
| 93 | + return fmt.Errorf("build or vet failed") |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + buildCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Output format (text|json)") |
| 102 | + buildCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output") |
| 103 | + |
| 104 | + return buildCmd |
| 105 | +} |
| 106 | + |
| 107 | +// validatePath validates that the provided path exists and is a directory |
| 108 | +func validatePath(path string) error { |
| 109 | + if info, err := os.Stat(path); err != nil || !info.IsDir() { |
| 110 | + return fmt.Errorf("path '%s' does not exist or is not a directory", path) |
| 111 | + } |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +// buildAndVet performs go build and go vet operations on the specified folder |
| 116 | +func buildAndVet(folderPath string) (*BuildResult, error) { |
| 117 | + result := &BuildResult{ |
| 118 | + Path: folderPath, |
| 119 | + } |
| 120 | + |
| 121 | + // Convert to absolute path |
| 122 | + absPath, err := filepath.Abs(folderPath) |
| 123 | + if err != nil { |
| 124 | + result.Success = false |
| 125 | + result.Message = fmt.Sprintf("Failed to get absolute path: %v", err) |
| 126 | + return result, nil |
| 127 | + } |
| 128 | + result.Path = absPath |
| 129 | + |
| 130 | + // Run go build |
| 131 | + buildSuccess, buildOutput := runGoBuild(absPath) |
| 132 | + result.BuildOutput = buildOutput |
| 133 | + |
| 134 | + // Run go vet |
| 135 | + vetSuccess, vetOutput := runGoVet(absPath) |
| 136 | + result.VetOutput = vetOutput |
| 137 | + |
| 138 | + // Determine overall success |
| 139 | + result.Success = buildSuccess && vetSuccess |
| 140 | + |
| 141 | + if !result.Success { |
| 142 | + if !buildSuccess && !vetSuccess { |
| 143 | + result.Message = "Both build and vet failed" |
| 144 | + } else if !buildSuccess { |
| 145 | + result.Message = "Build failed" |
| 146 | + } else { |
| 147 | + result.Message = "Vet failed" |
| 148 | + } |
| 149 | + } else { |
| 150 | + result.Message = "Build and vet completed successfully" |
| 151 | + } |
| 152 | + |
| 153 | + return result, nil |
| 154 | +} |
| 155 | + |
| 156 | +// runGoBuild executes go build and returns success status and output |
| 157 | +func runGoBuild(path string) (bool, string) { |
| 158 | + cmd := exec.Command("go", "build", "./...") |
| 159 | + cmd.Dir = path |
| 160 | + |
| 161 | + output, err := cmd.CombinedOutput() |
| 162 | + outputStr := string(output) |
| 163 | + |
| 164 | + if err != nil { |
| 165 | + return false, outputStr |
| 166 | + } |
| 167 | + |
| 168 | + return true, outputStr |
| 169 | +} |
| 170 | + |
| 171 | +// runGoVet executes go vet and returns success status and output |
| 172 | +func runGoVet(path string) (bool, string) { |
| 173 | + cmd := exec.Command("go", "vet", "./...") |
| 174 | + cmd.Dir = path |
| 175 | + |
| 176 | + output, err := cmd.CombinedOutput() |
| 177 | + outputStr := string(output) |
| 178 | + |
| 179 | + if err != nil { |
| 180 | + return false, outputStr |
| 181 | + } |
| 182 | + |
| 183 | + return true, outputStr |
| 184 | +} |
0 commit comments