AppendScript appends each line of a script to s.Cmds. It also applies the following formatting rules: - Trim leading newlines from the script. - Trim trailing whitespace from the script. - If the first line of the script begins with one or more tabs ('\t'), then unindent each line by that same numb
(script string)
| 63 | // tabs as the first line. If it starts with fewer tabs, then it is not |
| 64 | // unindented at all. |
| 65 | func (s *Commands) AppendScript(script string) { |
| 66 | script = strings.TrimLeft(script, "\r\n ") |
| 67 | script = strings.TrimRightFunc(script, unicode.IsSpace) |
| 68 | if len(script) == 0 { |
| 69 | return |
| 70 | } |
| 71 | prefixLen := strings.IndexFunc(script, func(r rune) bool { return r != '\t' }) |
| 72 | prefix := strings.Repeat("\t", prefixLen) |
| 73 | for _, line := range strings.Split(script, "\n") { |
| 74 | line = strings.TrimRightFunc(line, unicode.IsSpace) |
| 75 | line = strings.TrimPrefix(line, prefix) |
| 76 | s.Cmds = append(s.Cmds, line) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // MarshalJSON marshals shell commands according to s.MarshalAs. It marshals |
| 81 | // commands to a string by joining s.Cmds with newlines. |
no outgoing calls