text-pic/cmd/wfgraphic-cli/main.go

91 lines
2.4 KiB
Go
Raw Normal View History

2021-01-19 16:06:54 +00:00
/*
* Copyright © 2021 A Bunch Tell LLC.
*
* This file is part of text-pic.
*
* text-pic is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, included
* in the LICENSE file in this source code package.
*/
2021-01-19 13:09:12 +00:00
package main
import (
"flag"
"io/ioutil"
2021-01-19 13:09:12 +00:00
"os"
2021-01-20 13:42:04 +00:00
"strconv"
"strings"
2021-01-19 13:09:12 +00:00
"time"
"github.com/writeas/web-core/log"
2021-01-20 14:15:16 +00:00
"github.com/writefreely/text-pic"
2021-01-19 13:09:12 +00:00
)
var (
outputFile = flag.String("o", "out.png", "Image output filename")
2021-01-19 17:36:16 +00:00
font = flag.String("font", "serif", "Post font (options: \"serif\", \"sans\", \"mono\")")
2021-01-20 13:56:32 +00:00
instance = flag.String("h", "", "WriteFreely instance hostname (e.g. \"pencil.writefree.ly\")")
author = flag.String("u", "", "WriteFreely author username (for multi-user instances)")
2021-01-20 13:42:04 +00:00
size = flag.String("size", "1024", "Image size, either a single number for a square (e.g. \"900\") or a combined width and height (e.g. \"1080x1920\")")
)
2021-01-19 13:09:12 +00:00
func main() {
flag.Parse()
2021-01-19 17:36:16 +00:00
// Validate input
2021-01-20 13:54:54 +00:00
if *instance == "" {
log.Info("Aborting. Instance name (-i instance.tld) is required")
flag.Usage()
os.Exit(1)
}
2021-01-19 17:36:16 +00:00
if !textpic.IsValidFont(*font) {
2021-01-20 13:42:23 +00:00
log.Info("Invalid --font given. Options: \"serif\", \"sans\", \"mono\"")
2021-01-19 17:36:16 +00:00
os.Exit(1)
}
2021-01-20 13:42:04 +00:00
// Parse image dimensions and validate
var w, h int
if strings.ContainsRune(*size, 'x') {
parts := strings.Split(*size, "x")
if len(parts) != 2 {
log.Info("Invalid --size given. Must be a single number for a square (e.g. \"900\") or a combined width and height (e.g. \"1080x1920\")")
os.Exit(1)
}
var err error
w, err = strconv.Atoi(parts[0])
if err != nil {
log.Info("Unable to parse --size dimension '%s': %s", parts[0], err)
os.Exit(1)
}
h, err = strconv.Atoi(parts[1])
if err != nil {
log.Info("Unable to parse --size dimension '%s': %s", parts[1], err)
os.Exit(1)
}
} else {
sq, err := strconv.Atoi(*size)
if err != nil {
log.Info("Unable to parse --size: %s", err)
os.Exit(1)
}
w = sq
h = sq
}
2021-01-19 17:36:16 +00:00
log.Info("Reading input...")
in, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Error("read: %s", err)
os.Exit(1)
}
log.Info("Generating image...")
2021-01-19 13:09:12 +00:00
start := time.Now()
2021-01-20 13:42:04 +00:00
err = textpic.GenerateImage(textpic.NewContentOptions(*instance, *author, false, *font, string(in)), w, h, *outputFile)
2021-01-19 13:09:12 +00:00
if err != nil {
log.Error("%s", err)
os.Exit(1)
}
log.Info("Completed in %s", time.Since(start))
}