Files
yuki/update.go
2026-01-21 15:57:27 -03:00

104 lines
2.7 KiB
Go

package main
import (
"log"
"code.laidback.moe/go-writefreely"
"github.com/spf13/cobra"
"github.com/tj/go-editor"
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update a post on the WriteFreely instance (this will override contents)",
Run: func(cmd *cobra.Command, args []string) {
collection, err := cmd.Flags().GetString("collection")
if err != nil {
log.Fatal("Couldn't get the collection flag")
}
font, err := cmd.Flags().GetString("font")
if err != nil {
log.Fatal("Couldn't get the font flag")
}
id, err := cmd.Flags().GetString("id")
if err != nil {
log.Fatal("Couldn't get the id flag")
}
lang, err := cmd.Flags().GetString("lang")
if err != nil {
log.Fatal("Couldn't get the language flag")
}
rtl, err := cmd.Flags().GetBool("rtl")
if err != nil {
log.Fatal("Couldn't get the right-to-left flag")
}
title, err := cmd.Flags().GetString("title")
if err != nil {
log.Fatal("Couldn't get the title flag")
}
DoUpdate(id, collection, font, lang, rtl, title, args)
},
}
func init() {
ConfInit()
updateCmd.Flags().StringP("collection", "b", "", "Location for the post, if unset, it will stay in Drafts, otherwise it gets moved to the specified collection")
updateCmd.Flags().StringP("font", "f", "mono", "Which font to use for presentation (e.g. sans, serif, wrap, code)")
updateCmd.Flags().StringP("id", "i", "", "Post ID")
updateCmd.Flags().StringP("lang", "l", "en", "ISO 639-1 language code (e.g. en or de)")
updateCmd.Flags().BoolP("rtl", "r", false, "Whether to use right-to-left writing style (common in Middle Eastern languages)")
updateCmd.Flags().StringP("title", "t", "", "Title for the post. Supplying a parameter but leaving it blank will remove any title currently in the post")
if err := updateCmd.MarkFlagRequired("id"); err != nil { panic(err) }
rootCmd.AddCommand(updateCmd)
}
func DoUpdate(id, collection, font, lang string, rtl bool, title string, args []string) {
writefreely.InstanceURL = Config.Host
var err error
var w *writefreely.Post
if rtl {
log.Println("Using right-to-left writing style")
}
data, err := editor.Read()
if err != nil {
log.Fatal("Unable to read content from editor")
}
post := string(data)
c := writefreely.NewClient()
c.SetToken(Config.Token)
if len(collection) == 0 {
w, err = c.UpdatePost(&writefreely.PostParams{
Title: title,
Content: post,
Font: font,
Language: &lang,
IsRTL: &rtl,
ID: id,
})
} else {
w, err = c.UpdatePost(&writefreely.PostParams{
Title: title,
Content: post,
Font: font,
Collection: collection,
Language: &lang,
IsRTL: &rtl,
ID: id,
})
}
if err != nil {
log.Fatal(err)
}
log.Printf("[%s] Updated\n", w.ID)
}