Parse creates a new Target given the provided target name.
(given string)
| 94 | |
| 95 | // Parse creates a new Target given the provided target name. |
| 96 | func (tm *TargetParser) Parse(given string) (*Target, error) { |
| 97 | target := Target{writer: tm.defaultWriter} |
| 98 | |
| 99 | formatAndPath, opts, foundOpts := strings.Cut(given, "?") |
| 100 | |
| 101 | target.Options = tm.defaultOptions |
| 102 | if foundOpts { |
| 103 | if err := target.Options.mutate(opts); err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | var path string |
| 109 | target.Format, path, _ = strings.Cut(formatAndPath, "=") |
| 110 | |
| 111 | if target.Format == "" { |
| 112 | target.Format = tm.defaultFormat |
| 113 | } |
| 114 | |
| 115 | if path != "" { |
| 116 | target.writer = &fileWriter{path: path, fs: tm.fs} |
| 117 | } |
| 118 | |
| 119 | return &target, nil |
| 120 | } |
| 121 | |
| 122 | // fileWriter implements a simple Writer wrapper for afero.Fs. |
| 123 | type fileWriter struct { |