~samwhited/blogsync

fa8bf55a15afabb69b742ff761a6922d85b15a2b — Sam Whited 1 year, 9 months ago acf36a9
blogsync: return compiled template after publish

The compiled template will be required for updating blog posts in the
future in the preview command. Instead of having to compile it twice,
return all dependencies for updating a post after the initial publishing
of posts so that they can be reused.
2 files changed, 9 insertions(+), 9 deletions(-)

M preview.go
M publish.go
M preview.go => preview.go +2 -2
@@ 205,7 205,7 @@ https://writefreely.org/
			}
			debug.Printf("logged in as: %+v", authUser)

			collections, err := publish(opts, siteConfig, client, logger, debug)
			_, collections, err := publish(opts, siteConfig, client, logger, debug)
			if err != nil {
				return err
			}


@@ 243,7 243,7 @@ https://writefreely.org/
						}
					}

					collections, err = publish(opts, siteConfig, client, logger, debug)
					_, collections, err = publish(opts, siteConfig, client, logger, debug)
					if err != nil {
						logger.Printf("error republishing posts: %v", err)
					}

M publish.go => publish.go +7 -7
@@ 69,13 69,13 @@ func publishCmd(siteConfig Config, client *writeas.Client, logger, debug *log.Lo
Expects an API token to be exported as $%s.`, envToken),
		Flags: flags,
		Run: func(cmd *cli.Command, args ...string) error {
			_, err := publish(opts, siteConfig, client, logger, debug)
			_, _, err := publish(opts, siteConfig, client, logger, debug)
			return err
		},
	}
}

func publish(opts publishOptions, siteConfig Config, client *writeas.Client, logger, debug *log.Logger) ([]writeas.Collection, error) {
func publish(opts publishOptions, siteConfig Config, client *writeas.Client, logger, debug *log.Logger) (*template.Template, []writeas.Collection, error) {
	var collections []writeas.Collection
	if opts.createCollections {
		colls, err := client.GetUserCollections()


@@ 101,7 101,7 @@ func publish(opts publishOptions, siteConfig Config, client *writeas.Client, log
		// should load.
		compiledTmpl, err = compiledTmpl.ParseFiles(tmplFile)
		if err != nil {
			return nil, fmt.Errorf("error compiling template file %s: %v", tmplFile, err)
			return nil, nil, fmt.Errorf("error compiling template file %s: %v", tmplFile, err)
		}
		compiledTmpl = compiledTmpl.Lookup(tmplFile)
	} else {


@@ 109,14 109,14 @@ func publish(opts publishOptions, siteConfig Config, client *writeas.Client, log
		// Otherwise, it is a raw template and we should compile it.
		compiledTmpl, err = compiledTmpl.Parse(opts.tmpl)
		if err != nil {
			return nil, fmt.Errorf("error compiling template: %v", err)
			return nil, nil, fmt.Errorf("error compiling template: %v", err)
		}
	}

	var posts []writeas.Post
	p, err := client.GetUserPosts()
	if err != nil {
		return nil, fmt.Errorf("error fetching users posts: %v", err)
		return nil, nil, fmt.Errorf("error fetching users posts: %v", err)
	}
	// For now, the writeas SDK returns things with a lot of unnecessary
	// indirection that makes the library hard to use.


@@ 129,7 129,7 @@ func publish(opts publishOptions, siteConfig Config, client *writeas.Client, log
		return publishPost(pagePath, opts, siteConfig, posts, collections, compiledTmpl, client, logger, debug)
	})
	if err != nil {
		return nil, err
		return nil, nil, err
	}

	// Delete remaining posts for which we couldn't find a matching file.


@@ 147,7 147,7 @@ func publish(opts publishOptions, siteConfig Config, client *writeas.Client, log
		logger.Printf("no file found matching post %q, re-run with --delete to remove", post.Slug)
	}

	return collections, nil
	return compiledTmpl, collections, nil
}

func publishPost(pagePath string, opts publishOptions, siteConfig Config, posts []writeas.Post, collections []writeas.Collection, compiledTmpl *template.Template, client *writeas.Client, logger, debug *log.Logger) error {