diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-04-10 19:16:09 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-04-11 09:50:19 +0200 |
commit | e8d6ca9531d19e4e898c57d77d2fd627ea38ade0 (patch) | |
tree | fb38c183444396520810d05deffed2699fc0e7ff /commands/convert.go | |
parent | 4d32f2fa8969f368b088dc9bcedb45f2c986cb27 (diff) | |
download | hugo-e8d6ca9531d19e4e898c57d77d2fd627ea38ade0.tar.gz hugo-e8d6ca9531d19e4e898c57d77d2fd627ea38ade0.zip |
commands: Add CLI tests
See #4598
Diffstat (limited to 'commands/convert.go')
-rw-r--r-- | commands/convert.go | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/commands/convert.go b/commands/convert.go index 9e0a66026..202cc1111 100644 --- a/commands/convert.go +++ b/commands/convert.go @@ -32,17 +32,17 @@ var ( _ cmder = (*convertCmd)(nil) ) -// TODO(bep) cli refactor -var outputDir string -var unsafe bool - type convertCmd struct { + outputDir string + unsafe bool + *baseBuilderCmd } func newConvertCmd() *convertCmd { cc := &convertCmd{} + // TODO(bep) cli refactor this is more than it had cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{ Use: "convert", Short: "Convert your content to different formats", @@ -82,17 +82,16 @@ to use YAML for the front matter.`, }, ) - // TODO(bep) cli refactor - // cmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "filesystem path to write files to") - // cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from") - // cmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "enable less safe operations, please backup first") + cc.cmd.PersistentFlags().StringVarP(&cc.outputDir, "output", "o", "", "filesystem path to write files to") + cc.cmd.PersistentFlags().StringVarP(&cc.source, "source", "s", "", "filesystem path to read files relative from") + cc.cmd.PersistentFlags().BoolVar(&cc.unsafe, "unsafe", false, "enable less safe operations, please backup first") cc.cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) return cc } func (cc *convertCmd) convertContents(mark rune) error { - if outputDir == "" && !unsafe { + if cc.outputDir == "" && !cc.unsafe { return newUserError("Unsafe operation not allowed, use --unsafe or set a different output path") } @@ -114,17 +113,17 @@ func (cc *convertCmd) convertContents(mark rune) error { site.Log.FEEDBACK.Println("processing", len(site.AllPages), "content files") for _, p := range site.AllPages { - if err := convertAndSavePage(p, site, mark); err != nil { + if err := cc.convertAndSavePage(p, site, mark); err != nil { return err } } return nil } -func convertAndSavePage(p *hugolib.Page, site *hugolib.Site, mark rune) error { +func (cc *convertCmd) convertAndSavePage(p *hugolib.Page, site *hugolib.Site, mark rune) error { // The resources are not in .Site.AllPages. for _, r := range p.Resources.ByType("page") { - if err := convertAndSavePage(r.(*hugolib.Page), site, mark); err != nil { + if err := cc.convertAndSavePage(r.(*hugolib.Page), site, mark); err != nil { return err } } @@ -182,8 +181,8 @@ func convertAndSavePage(p *hugolib.Page, site *hugolib.Site, mark rune) error { } newFilename := p.Filename() - if outputDir != "" { - newFilename = filepath.Join(outputDir, p.Dir(), newPage.LogicalName()) + if cc.outputDir != "" { + newFilename = filepath.Join(cc.outputDir, p.Dir(), newPage.LogicalName()) } if err = newPage.SaveSourceAs(newFilename); err != nil { |