Format renders the dry-run output as plain text for AI/human consumption.
()
| 131 | |
| 132 | // Format renders the dry-run output as plain text for AI/human consumption. |
| 133 | func (d *DryRunAPI) Format() string { |
| 134 | var b strings.Builder |
| 135 | |
| 136 | if d.desc != "" { |
| 137 | b.WriteString("# ") |
| 138 | b.WriteString(d.desc) |
| 139 | b.WriteByte('\n') |
| 140 | } |
| 141 | |
| 142 | for i, c := range d.calls { |
| 143 | if i > 0 || d.desc != "" { |
| 144 | b.WriteByte('\n') |
| 145 | } |
| 146 | if c.Desc != "" { |
| 147 | b.WriteString("# ") |
| 148 | b.WriteString(c.Desc) |
| 149 | b.WriteByte('\n') |
| 150 | } |
| 151 | |
| 152 | u := d.resolveURL(c.URL) |
| 153 | if len(c.Params) > 0 { |
| 154 | u += "?" + encodeParams(c.Params) |
| 155 | } |
| 156 | |
| 157 | method := c.Method |
| 158 | if method == "" { |
| 159 | method = "GET" |
| 160 | } |
| 161 | b.WriteString(method) |
| 162 | b.WriteByte(' ') |
| 163 | b.WriteString(u) |
| 164 | b.WriteByte('\n') |
| 165 | |
| 166 | if !util.IsNil(c.Body) { |
| 167 | j, _ := json.Marshal(c.Body) |
| 168 | b.WriteString(" ") |
| 169 | b.Write(j) |
| 170 | b.WriteByte('\n') |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if len(d.calls) == 0 && len(d.extra) > 0 { |
| 175 | if d.desc != "" { |
| 176 | b.WriteByte('\n') |
| 177 | } |
| 178 | keys := make([]string, 0, len(d.extra)) |
| 179 | for k := range d.extra { |
| 180 | keys = append(keys, k) |
| 181 | } |
| 182 | sort.Strings(keys) |
| 183 | for _, k := range keys { |
| 184 | sv := dryRunFormatValue(d.extra[k]) |
| 185 | if sv == "" { |
| 186 | continue |
| 187 | } |
| 188 | b.WriteString(k) |
| 189 | b.WriteString(": ") |
| 190 | b.WriteString(sv) |