| 115 | } |
| 116 | |
| 117 | func Format(opts Option) { |
| 118 | var content []byte |
| 119 | var err error |
| 120 | if opts.Format == "stdin" { |
| 121 | content, err = io.ReadAll(os.Stdin) |
| 122 | } else { |
| 123 | content, err = os.ReadFile(opts.Format) |
| 124 | } |
| 125 | |
| 126 | if err != nil { |
| 127 | return |
| 128 | } |
| 129 | |
| 130 | // Group by host+path for deduplication. |
| 131 | // Prefer the HTTP Host header when present so that |
| 132 | // different virtual hosts on the same IP:port do not |
| 133 | // get merged together. |
| 134 | group := make(map[string]map[string]*baseline.Baseline) |
| 135 | for _, line := range bytes.Split(bytes.TrimSpace(content), []byte("\n")) { |
| 136 | var result baseline.Baseline |
| 137 | err := json.Unmarshal(line, &result) |
| 138 | if err != nil { |
| 139 | logs.Log.Error(err.Error()) |
| 140 | return |
| 141 | } |
| 142 | result.Url, err = url.Parse(result.UrlString) |
| 143 | if err != nil { |
| 144 | continue |
| 145 | } |
| 146 | |
| 147 | // Determine host key: prefer Host header when available. |
| 148 | hostKey := result.Host |
| 149 | if hostKey == "" { |
| 150 | hostKey = result.Url.Host |
| 151 | // Normalize default ports when URL host does not include one. |
| 152 | if result.Url.Port() == "" { |
| 153 | if result.Url.Scheme == "https" { |
| 154 | hostKey += ":443" |
| 155 | } else if result.Url.Scheme == "http" { |
| 156 | hostKey += ":80" |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | if hostKey == "" { |
| 161 | continue |
| 162 | } |
| 163 | |
| 164 | if _, exists := group[hostKey]; !exists { |
| 165 | group[hostKey] = make(map[string]*baseline.Baseline) |
| 166 | } |
| 167 | group[hostKey][result.Path] = &result |
| 168 | } |
| 169 | |
| 170 | // Default to tree mode if not specified |
| 171 | outputProbe := opts.OutputProbe |
| 172 | if outputProbe == "" { |
| 173 | outputProbe = "full" |
| 174 | } |