|
| 1 | +package upload |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/bugsnag/bugsnag-cli/pkg/log" |
| 9 | + "github.com/bugsnag/bugsnag-cli/pkg/options" |
| 10 | + "github.com/bugsnag/bugsnag-cli/pkg/server" |
| 11 | + "github.com/bugsnag/bugsnag-cli/pkg/utils" |
| 12 | +) |
| 13 | + |
| 14 | +// uploadSymbolFile uploads a single Linux symbol file to the Bugsnag symbol server. |
| 15 | +// |
| 16 | +// Parameters: |
| 17 | +// - symbolFile: The path to the symbol file to upload. |
| 18 | +// - linuxOpts: Linux-specific upload options including appId, versionName, etc. |
| 19 | +// - opts: Global CLI options including an API key and overwrite behavior. |
| 20 | +// - logger: Logger for structured output. |
| 21 | +// |
| 22 | +// Returns: |
| 23 | +// - error: non-nil if the upload fails due to request or file issues. |
| 24 | +func uploadSymbolFile(symbolFile string, linuxOpts options.LinuxOptions, opts options.CLI, logger log.Logger) error { |
| 25 | + uploadOpts := map[string]string{} |
| 26 | + |
| 27 | + if linuxOpts.ApplicationId != "" { |
| 28 | + uploadOpts["appId"] = linuxOpts.ApplicationId |
| 29 | + } |
| 30 | + if linuxOpts.VersionName != "" { |
| 31 | + uploadOpts["versionName"] = linuxOpts.VersionName |
| 32 | + } |
| 33 | + if linuxOpts.ProjectRoot != "" { |
| 34 | + uploadOpts["projectRoot"] = linuxOpts.ProjectRoot |
| 35 | + } |
| 36 | + if base := filepath.Base(symbolFile); base != "" { |
| 37 | + uploadOpts["sharedObjectName"] = base |
| 38 | + } |
| 39 | + if linuxOpts.Overwrite { |
| 40 | + uploadOpts["overwrite"] = "true" |
| 41 | + } |
| 42 | + |
| 43 | + fileField := map[string]server.FileField{ |
| 44 | + "soFile": server.LocalFile(symbolFile), |
| 45 | + } |
| 46 | + |
| 47 | + if err := server.ProcessFileRequest( |
| 48 | + opts.ApiKey, |
| 49 | + "/linux", |
| 50 | + uploadOpts, |
| 51 | + fileField, |
| 52 | + filepath.Base(symbolFile), |
| 53 | + opts, |
| 54 | + logger, |
| 55 | + ); err != nil { |
| 56 | + return fmt.Errorf("uploading Linux symbol file %q: %w", symbolFile, err) |
| 57 | + } |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// ProcessLinux locates, validates, and uploads Linux symbol files. |
| 62 | +// |
| 63 | +// Parameters: |
| 64 | +// - opts: Global CLI options including upload configuration and API key. |
| 65 | +// - logger: Logger for structured logging and debug output. |
| 66 | +// |
| 67 | +// Behavior: |
| 68 | +// - Scans provided paths for build folders or symbol files. |
| 69 | +// - Reads metadata (appId, versionName) if provided. |
| 70 | +// - Uploads all recognized symbol files to the Bugsnag /linux endpoint. |
| 71 | +// |
| 72 | +// Returns: |
| 73 | +// - error: non-nil if scanning, build ID resolution, or upload fails. |
| 74 | +func ProcessLinux(opts options.CLI, logger log.Logger) error { |
| 75 | + linuxOpts := opts.Upload.Linux |
| 76 | + |
| 77 | + var fileList []string |
| 78 | + var soFileList []string |
| 79 | + var err error |
| 80 | + |
| 81 | + for _, path := range linuxOpts.Path { |
| 82 | + // Build a list of potential symbol files |
| 83 | + if utils.IsDir(path) { |
| 84 | + logger.Info(fmt.Sprintf("Scanning path: %s", path)) |
| 85 | + fileList, err = utils.BuildFileList([]string{path}) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("building file list from %q: %w", path, err) |
| 88 | + } |
| 89 | + logger.Debug(fmt.Sprintf("Found %d files in directory %s", len(fileList), path)) |
| 90 | + } else { |
| 91 | + fileList = append(fileList, path) |
| 92 | + } |
| 93 | + |
| 94 | + // Filter for valid ELF symbol files |
| 95 | + for _, file := range fileList { |
| 96 | + // Check for .so, .so.debug, and .debug files |
| 97 | + if strings.HasSuffix(file, ".so") || strings.HasSuffix(file, ".so.debug") || strings.HasSuffix(file, ".debug") { |
| 98 | + ok, err := utils.IsSymbolFile(file) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + if ok { |
| 103 | + soFileList = append(soFileList, file) |
| 104 | + logger.Debug(fmt.Sprintf("Found symbol file: %s", file)) |
| 105 | + } else { |
| 106 | + logger.Debug(fmt.Sprintf("%s is not a valid symbol file.", file)) |
| 107 | + } |
| 108 | + } else { |
| 109 | + // Skip files not matching the common suffixes |
| 110 | + logger.Debug(fmt.Sprintf("Skipping non-symbol file: %s", file)) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if linuxOpts.ProjectRoot != "" { |
| 115 | + logger.Debug(fmt.Sprintf("Using project root: %s", linuxOpts.ProjectRoot)) |
| 116 | + } |
| 117 | + |
| 118 | + if len(soFileList) == 0 { |
| 119 | + logger.Warn("No symbol files found to upload") |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + for _, file := range soFileList { |
| 124 | + if err := uploadSymbolFile(file, linuxOpts, opts, logger); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
0 commit comments