|
| 1 | +// Part of package cli. See cli.go. |
| 2 | +package cli |
| 3 | + |
| 4 | +import ( |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/donislawdev/TestingFilesGenerator/internal/core" |
| 11 | + "github.com/donislawdev/TestingFilesGenerator/internal/damage" |
| 12 | +) |
| 13 | + |
| 14 | +// damageEntry is what "tfg damage --json" returns. |
| 15 | +// |
| 16 | +// The parameters ride in the same propertyEntry the format and the preset lists |
| 17 | +// use, under the same key the preset list gives them. That is not tidiness: a |
| 18 | +// damage parameter IS a format.Property, so a script that already knows how to |
| 19 | +// draw a field from "tfg preset show --json" draws this one with no new code. |
| 20 | +type damageEntry struct { |
| 21 | + ID string `json:"id"` |
| 22 | + Detail string `json:"detail"` |
| 23 | + // SmallestFileBytes is the smallest file this damage can be given, with |
| 24 | + // its settings left at their defaults. |
| 25 | + // |
| 26 | + // At the defaults on purpose, because that is the number somebody reading |
| 27 | + // this column is about to ask a run for. The floor is a function of the |
| 28 | + // settings - zero-head's floor IS its bytes value - so a person who raises |
| 29 | + // the setting raises this, and the refusal names the new number rather |
| 30 | + // than this one. tfg formats made the other choice once and printed a |
| 31 | + // floor that runs refused: measured 2026-08-03, pdf announced 3265 and |
| 32 | + // took 3286. |
| 33 | + SmallestFileBytes int64 `json:"smallest_file_bytes"` |
| 34 | + Parameters []propertyEntry `json:"parameters,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +// damageEntryFor is one damage as a script sees it. |
| 38 | +func damageEntryFor(d damage.Descriptor) damageEntry { |
| 39 | + params := make([]propertyEntry, 0, len(d.Parameters)) |
| 40 | + for _, p := range d.Parameters { |
| 41 | + params = append(params, propertyEntry{ |
| 42 | + Name: p.Name, Kind: string(p.Kind), Min: p.Min, Max: p.Max, |
| 43 | + Unit: p.Unit, Choices: p.Choices, Default: p.Default, Detail: p.Detail, |
| 44 | + }) |
| 45 | + } |
| 46 | + return damageEntry{ |
| 47 | + ID: d.ID, Detail: d.Detail, |
| 48 | + SmallestFileBytes: defaultFloor(d), Parameters: params, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// defaultFloor is the smallest file this damage takes when nobody states a |
| 53 | +// setting. |
| 54 | +// |
| 55 | +// Asked of the declaration rather than worked out here, so a damage whose floor |
| 56 | +// is arithmetic on two settings answers for itself. |
| 57 | +func defaultFloor(d damage.Descriptor) int64 { |
| 58 | + return d.Floor(d.Defaults()) |
| 59 | +} |
| 60 | + |
| 61 | +// damageExample is the flag a person would type to use this damage, with its |
| 62 | +// first setting at the declared default. |
| 63 | +// |
| 64 | +// Built from the declaration rather than written out, because the colon and |
| 65 | +// comma syntax is the one thing about this flag nobody guesses, and an example |
| 66 | +// naming a setting that no longer exists teaches the wrong thing twice. |
| 67 | +func damageExample(d damage.Descriptor) string { |
| 68 | + out := "--damage " + d.ID |
| 69 | + if len(d.Parameters) > 0 { |
| 70 | + p := d.Parameters[0] |
| 71 | + value := p.Default |
| 72 | + if value == "" { |
| 73 | + value = "<value>" |
| 74 | + } |
| 75 | + out += ":" + p.Name + "=" + value |
| 76 | + } |
| 77 | + return out |
| 78 | +} |
| 79 | + |
| 80 | +// describeOneDamage prints everything one damage declares. |
| 81 | +// |
| 82 | +// The sentence comes first and the settings after it, which is the other way |
| 83 | +// round from a format: a format id says what the file will be and a damage id |
| 84 | +// does not, so the sentence is the answer rather than the footnote. |
| 85 | +func describeOneDamage(d damage.Descriptor, out io.Writer) { |
| 86 | + fmt.Fprintf(out, "%s - smallest file %s with the settings below\n", |
| 87 | + d.ID, core.ExactBytes(defaultFloor(d))) |
| 88 | + fmt.Fprintf(out, " %s\n", d.Detail) |
| 89 | + |
| 90 | + if len(d.Parameters) == 0 { |
| 91 | + fmt.Fprint(out, "\nThis damage takes no settings.\n") |
| 92 | + return |
| 93 | + } |
| 94 | + fmt.Fprintf(out, "\nsettings, written after a colon: %s\n", damageExample(d)) |
| 95 | + for _, p := range d.Parameters { |
| 96 | + fmt.Fprintf(out, " %-14s %s\n", p.Name, p.Allowed()) |
| 97 | + if p.Detail != "" { |
| 98 | + fmt.Fprintf(out, " %-14s %s\n", "", p.Detail) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// settingsColumn is the names of what a damage takes, for the list. |
| 104 | +// |
| 105 | +// A word rather than an empty cell when there are none, because a blank column |
| 106 | +// reads as "not filled in yet" and this is an answer. |
| 107 | +func settingsColumn(d damage.Descriptor) string { |
| 108 | + names := d.ParameterNames() |
| 109 | + if len(names) == 0 { |
| 110 | + return "none" |
| 111 | + } |
| 112 | + return strings.Join(names, ", ") |
| 113 | +} |
| 114 | + |
| 115 | +func listDamages(out io.Writer) { |
| 116 | + fmt.Fprintf(out, "%-14s %-14s %s\n", "DAMAGE", "SMALLEST FILE", "SETTINGS") |
| 117 | + for _, d := range damage.All() { |
| 118 | + fmt.Fprintf(out, "%-14s %-14s %s\n", |
| 119 | + d.ID, core.ExactBytes(defaultFloor(d)), settingsColumn(d)) |
| 120 | + // The sentence under the row rather than in a column of its own. A |
| 121 | + // damage id is not self describing the way a format id is - nobody |
| 122 | + // reads "zero-head" and knows what comes out - so a list of names |
| 123 | + // would say no more than the --damage flag help already says. |
| 124 | + fmt.Fprintf(out, " %s\n", d.Detail) |
| 125 | + } |
| 126 | + fmt.Fprint(out, "\nRun \"tfg damage <id>\" for what one damage takes.\n") |
| 127 | + fmt.Fprint(out, "The smallest file is what the default settings need.\n") |
| 128 | +} |
| 129 | + |
| 130 | +func damageUsage(w io.Writer, fs *flag.FlagSet, errOut io.Writer) { |
| 131 | + fmt.Fprint(w, `tfg damage - list the ways this build can break a file on purpose. |
| 132 | +
|
| 133 | +A damaged file comes out exactly the size you asked for and is one a reader |
| 134 | +refuses, so the manifest records it as expected to be rejected. Use them with |
| 135 | +"tfg generate --damage <id>" or with the damage key of a recipe. |
| 136 | +
|
| 137 | +Usage: |
| 138 | + tfg damage |
| 139 | + tfg damage --json |
| 140 | + tfg damage <id> |
| 141 | +
|
| 142 | +Flags: |
| 143 | +`) |
| 144 | + fs.SetOutput(w) |
| 145 | + fs.PrintDefaults() |
| 146 | + fs.SetOutput(errOut) |
| 147 | +} |
| 148 | + |
| 149 | +// everyDamageEntry is the whole registry as a script sees it. |
| 150 | +func everyDamageEntry() []damageEntry { |
| 151 | + all := damage.All() |
| 152 | + list := make([]damageEntry, 0, len(all)) |
| 153 | + for _, d := range all { |
| 154 | + list = append(list, damageEntryFor(d)) |
| 155 | + } |
| 156 | + return list |
| 157 | +} |
| 158 | + |
| 159 | +// damageCmd answers what this build can break and what each one takes. |
| 160 | +// |
| 161 | +// It exists because the window drew the settings of a chosen damage from the |
| 162 | +// declaration and the command line named the damages and stopped there: to |
| 163 | +// learn that zero-head takes bytes, and that bytes runs from 4 to 4096, the |
| 164 | +// only way was to type a wrong value and read the refusal. The parity guard |
| 165 | +// does not see that gap, because it counts recipe keys, formats and presets and |
| 166 | +// a damage parameter is none of the three. Written up in |
| 167 | +// docs/CORRUPTION-ARCHITECTURE-2026-09-08.md section 14.5. |
| 168 | +func damageCmd(args []string, out, errOut io.Writer) int { |
| 169 | + fs := flag.NewFlagSet("damage", flag.ContinueOnError) |
| 170 | + fs.SetOutput(errOut) |
| 171 | + asJSON := fs.Bool("json", false, "write the list as JSON to standard output") |
| 172 | + fs.Usage = func() { damageUsage(errOut, fs, errOut) } |
| 173 | + if helpRequested(args) { |
| 174 | + damageUsage(out, fs, errOut) |
| 175 | + return ExitOK |
| 176 | + } |
| 177 | + leading, rest := splitLeadingPath(args) |
| 178 | + if err := fs.Parse(rest); err != nil { |
| 179 | + return ExitUsage |
| 180 | + } |
| 181 | + |
| 182 | + wanted, ok := atMostOneName(leading, fs, errOut) |
| 183 | + if !ok { |
| 184 | + return ExitUsage |
| 185 | + } |
| 186 | + |
| 187 | + if wanted == "" { |
| 188 | + if *asJSON { |
| 189 | + return renderJSON(everyDamageEntry(), out, errOut) |
| 190 | + } |
| 191 | + listDamages(out) |
| 192 | + return ExitOK |
| 193 | + } |
| 194 | + |
| 195 | + d, err := damage.Get(wanted) |
| 196 | + if err != nil { |
| 197 | + // The registry already names the damages it knows, so the message is |
| 198 | + // written once rather than here as well. |
| 199 | + fmt.Fprintf(errOut, "tfg: %s\n", describeError(err)) |
| 200 | + return classify(err) |
| 201 | + } |
| 202 | + if *asJSON { |
| 203 | + return renderJSON([]damageEntry{damageEntryFor(d)}, out, errOut) |
| 204 | + } |
| 205 | + describeOneDamage(d, out) |
| 206 | + return ExitOK |
| 207 | +} |
| 208 | + |
| 209 | +// atMostOneName takes the single id a listing command was asked about. |
| 210 | +// |
| 211 | +// Two names are refused rather than answered about the first. Silence about an |
| 212 | +// argument somebody typed is the shape untouchable rule 6 forbids: measured on |
| 213 | +// 2026-09-09, "tfg formats png svg" describes png, ignores svg and ends with |
| 214 | +// zero, so a script asking about the wrong thing gets a confident answer about |
| 215 | +// something else. This command does not repeat that. |
| 216 | +func atMostOneName(leading string, fs *flag.FlagSet, errOut io.Writer) (string, bool) { |
| 217 | + extra := fs.Args() |
| 218 | + switch { |
| 219 | + case leading == "" && len(extra) == 0: |
| 220 | + return "", true |
| 221 | + case leading == "" && len(extra) == 1: |
| 222 | + return extra[0], true |
| 223 | + case leading != "" && len(extra) == 0: |
| 224 | + return leading, true |
| 225 | + } |
| 226 | + names := append([]string{}, extra...) |
| 227 | + if leading != "" { |
| 228 | + names = append([]string{leading}, names...) |
| 229 | + } |
| 230 | + fmt.Fprintf(errOut, "tfg: asked about %s at once, and this describes one at a time. "+ |
| 231 | + "Run it again with a single name, or with none for the whole list.\n", strings.Join(names, ", ")) |
| 232 | + return "", false |
| 233 | +} |
0 commit comments