Przeglądaj źródła

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 lat temu
rodzic
commit
e2361eb334
1 zmienionych plików z 7 dodań i 0 usunięć
  1. 7 0
      go/blog/blog.go

+ 7 - 0
go/blog/blog.go

@ -13,6 +13,7 @@ import (
13 13
	"io/ioutil"
14 14
	"net/url"
15 15
	"os"
16
	"path"
16 17
	"sort"
17 18
	"strings"
18 19
	"unicode"
@ -227,6 +228,12 @@ func main() {
227 228
228 229
		// write to specified `output` file if none was given as an argument
229 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 237
			out, err = os.Create(opts.Output)
231 238
			if err != nil {
232 239
				exit(err)