|
1 | 1 | // Package debug is a dummy package that is not yet implemented. |
2 | 2 | package debug |
3 | 3 |
|
4 | | -import "runtime" |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "runtime" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
5 | 10 |
|
6 | 11 | // SetMaxStack sets the maximum amount of memory that can be used by a single |
7 | 12 | // goroutine stack. |
@@ -62,9 +67,59 @@ func SetGCPercent(n int) int { |
62 | 67 | return n |
63 | 68 | } |
64 | 69 |
|
65 | | -// String implements Stringer for BuildInfo. |
66 | | -// |
67 | | -// Not implemented. |
| 70 | +// Start of stolen from big go. TODO: import/reuse without copy pasta. |
| 71 | + |
| 72 | +// quoteKey reports whether key is required to be quoted. |
| 73 | +func quoteKey(key string) bool { |
| 74 | + return len(key) == 0 || strings.ContainsAny(key, "= \t\r\n\"`") |
| 75 | +} |
| 76 | + |
| 77 | +// quoteValue reports whether value is required to be quoted. |
| 78 | +func quoteValue(value string) bool { |
| 79 | + return strings.ContainsAny(value, " \t\r\n\"`") |
| 80 | +} |
| 81 | + |
68 | 82 | func (bi *BuildInfo) String() string { |
69 | | - return "<build info placeholder>\n" |
| 83 | + buf := new(strings.Builder) |
| 84 | + if bi.GoVersion != "" { |
| 85 | + fmt.Fprintf(buf, "go\t%s\n", bi.GoVersion) |
| 86 | + } |
| 87 | + if bi.Path != "" { |
| 88 | + fmt.Fprintf(buf, "path\t%s\n", bi.Path) |
| 89 | + } |
| 90 | + var formatMod func(string, Module) |
| 91 | + formatMod = func(word string, m Module) { |
| 92 | + buf.WriteString(word) |
| 93 | + buf.WriteByte('\t') |
| 94 | + buf.WriteString(m.Path) |
| 95 | + buf.WriteByte('\t') |
| 96 | + buf.WriteString(m.Version) |
| 97 | + if m.Replace == nil { |
| 98 | + buf.WriteByte('\t') |
| 99 | + buf.WriteString(m.Sum) |
| 100 | + } else { |
| 101 | + buf.WriteByte('\n') |
| 102 | + formatMod("=>", *m.Replace) |
| 103 | + } |
| 104 | + buf.WriteByte('\n') |
| 105 | + } |
| 106 | + if bi.Main != (Module{}) { |
| 107 | + formatMod("mod", bi.Main) |
| 108 | + } |
| 109 | + for _, dep := range bi.Deps { |
| 110 | + formatMod("dep", *dep) |
| 111 | + } |
| 112 | + for _, s := range bi.Settings { |
| 113 | + key := s.Key |
| 114 | + if quoteKey(key) { |
| 115 | + key = strconv.Quote(key) |
| 116 | + } |
| 117 | + value := s.Value |
| 118 | + if quoteValue(value) { |
| 119 | + value = strconv.Quote(value) |
| 120 | + } |
| 121 | + fmt.Fprintf(buf, "build\t%s=%s\n", key, value) |
| 122 | + } |
| 123 | + |
| 124 | + return buf.String() |
70 | 125 | } |
0 commit comments