~samwhited/blogsync

024c9d6747f4e06e0e6fc393b07ff550a2baecaa ā€” Sam Whited 1 year, 9 months ago e56e57e
blogsync: create collections when previewing

Previously posts in collections other than the main site would fail to
publish to the preview instance because the collection didn't exist.
This patch allows creating collectinos that don't exist during
publishing (but only if we're in preview mode, we don't want every typo
to a collection name resulting in a new collection in production).
3 files changed, 41 insertions(+), 14 deletions(-)

M main.go
M preview.go
M publish.go
M main.go => main.go +1 -1
@@ 115,7 115,7 @@ To get a token, use the "token" command.`, os.Args[0], userConfig, envToken),
			collectionsCmd(client, logger, debug),
			convertCmd(logger, debug),
			previewCmd(siteConfig, logger, debug),
			publishCmd(siteConfig, client, logger, debug),
			publishCmd(false, siteConfig, client, logger, debug),
			tokenCmd(apiBase, torPort, logger, debug),

			// Help articles

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

			// TODO: add a mechanism for forcing the creation of collections that
			// don't exist during publishing, and then rely on that.
			_, err = client.CreateCollection(&writeas.CollectionParams{
				Alias:       siteConfig.Collection,
				Title:       siteConfig.Title,
				Description: siteConfig.Description,
			})
			if err != nil {
				return err
			}

			err = publishCmd(siteConfig, client, logger, debug).Exec()
			err = publishCmd(true, siteConfig, client, logger, debug).Exec()
			if err != nil {
				return err
			}

M publish.go => publish.go +39 -1
@@ 29,7 29,7 @@ type tmplData struct {
	Config Config
}

func publishCmd(siteConfig Config, client *writeas.Client, logger, debug *log.Logger) *cli.Command {
func publishCmd(createCollections bool, siteConfig Config, client *writeas.Client, logger, debug *log.Logger) *cli.Command {
	var (
		collection = ""
		del        = false


@@ 46,6 46,7 @@ func publishCmd(siteConfig Config, client *writeas.Client, logger, debug *log.Lo
	flags.StringVar(&content, "content", orDef(siteConfig.Content, content), "A directory containing pages")
	flags.StringVar(&tmpl, "tmpl", orDef(siteConfig.Tmpl, tmpl), "A template using Go's html/template format, to load from a file use @filename")

	var collections []writeas.Collection
	return &cli.Command{
		Usage: "publish [options]",
		Description: fmt.Sprintf(`Publishes Markdown files to write.as.


@@ 53,6 54,20 @@ 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 {
			if createCollections {
				colls, err := client.GetUserCollections()
				if err != nil {
					logger.Printf("error fetching existing collections: %v", err)
				}
				collections = *colls

				collections = createCollectionIfNotExist(collections, client, debug, &writeas.CollectionParams{
					Alias:       siteConfig.Collection,
					Title:       siteConfig.Title,
					Description: siteConfig.Description,
				})
			}

			compiledTmpl := template.New("root")
			var err error
			if tmplFile := strings.TrimPrefix(tmpl, "@"); tmpl != tmplFile {


@@ 216,6 231,12 @@ Expects an API token to be exported as $%s.`, envToken),
				}

				if !dryRun && !skipUpdate {
					if createCollections {
						collections = createCollectionIfNotExist(collections, client, debug, &writeas.CollectionParams{
							Alias: params.Collection,
							Title: params.Collection,
						})
					}
					if postID == "" {
						post, err := client.CreatePost(params)
						if err != nil {


@@ 289,3 310,20 @@ Expects an API token to be exported as $%s.`, envToken),
		},
	}
}

func createCollectionIfNotExist(colls []writeas.Collection, client *writeas.Client, debug *log.Logger, coll *writeas.CollectionParams) []writeas.Collection {
	for _, c := range colls {
		if c.Alias == coll.Alias {
			return colls
		}
	}
	debug.Printf("creating collection %sā€¦", coll.Alias)
	newColl, err := client.CreateCollection(coll)
	if err != nil {
		debug.Printf("error creating collection %s: %v", coll.Alias, err)
	}
	if coll != nil {
		colls = append(colls, *newColl)
	}
	return colls
}