~kota/gemgen

1facce3f9a75e497f4ab60b663fae07e3f824daf — Dakota Walsh 2 years ago 6f7aa4c
create output dir if missing
1 files changed, 25 insertions(+), 3 deletions(-)

M options/options.go
M options/options.go => options/options.go +25 -3
@@ 78,6 78,31 @@ func ParseArgs(progname string, args []string) (*Opts, string, error) {
	}

	// Handle flags.
	opts.TemplateArgs = *templateFlag

	opts.Output = *outputFlag
	if opts.Output != "" {
		// Ensure path exists and is a directory.
		fi, err := os.Stat(opts.Output)
		if err != nil {
			if os.IsNotExist(err) {
				// If the directory is missing, create it.
				err = os.MkdirAll(opts.Output, 0755)
				if err != nil {
					return nil, "",
						fmt.Errorf("failed creating output directory: %v", err)
				}
			} else {
				return nil, "",
					fmt.Errorf("critically failed reading output path: %v", err)
			}
		} else {
			if !fi.IsDir() {
				return nil, "", fmt.Errorf("output path is not a directory")
			}
		}
	}

	opts.GemOptions = append(
		opts.GemOptions,
		gem.WithHorizontalRule(*horizontalRuleFlag),


@@ 136,9 161,6 @@ func ParseArgs(progname string, args []string) (*Opts, string, error) {
			*paragraphLinkFlag)
	}

	opts.Output = *outputFlag
	opts.TemplateArgs = *templateFlag

	if *linkRegexFlag != nil {
		if len(*linkRegexFlag) > 0 && len(*linkRegexFlag)%3 == 0 {
			var replacers []gem.LinkReplacer