Просмотр исходного кода

Write to the same directory as the input file

This only happens if the output file is specified in the YAML prefix, no
output file was given on the commandline, the specified output file is
only a file name *and* the input file is not in the current directory.

It ensures that `blog /some/path/to/blog.yaml` doesn't generate a file
in the current directory if the output was specified as `output: file.yaml`.
Lucas Stadler лет назад: 9
Родитель
Сommit
e2361eb334
1 измененных файлов с 7 добавлено и 0 удалено
  1. 7 0
      go/blog/blog.go

+ 7 - 0
go/blog/blog.go

13
	"io/ioutil"
13
	"io/ioutil"
14
	"net/url"
14
	"net/url"
15
	"os"
15
	"os"
16
	"path"
16
	"sort"
17
	"sort"
17
	"strings"
18
	"strings"
18
	"unicode"
19
	"unicode"
227
228
228
		// write to specified `output` file if none was given as an argument
229
		// write to specified `output` file if none was given as an argument
229
		if opts.Output != "" && out == os.Stdout {
230
		if opts.Output != "" && out == os.Stdout {
231
			// write to same directory as the input file if opts.Output is
232
			// just the file name
233
			if path.Base(opts.Output) == opts.Output && path.Dir(dataPath) != "." {
234
				opts.Output = path.Join(path.Dir(dataPath), opts.Output)
235
			}
236
230
			out, err = os.Create(opts.Output)
237
			out, err = os.Create(opts.Output)
231
			if err != nil {
238
			if err != nil {
232
				exit(err)
239
				exit(err)