add `schedule` subcommand

This commit is contained in:
Stefan Majewsky 2023-11-10 00:09:06 +01:00
parent a1c1a08ff7
commit b76992e395
2 changed files with 70 additions and 5 deletions

32
main.go
View File

@ -10,19 +10,40 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
)
func main() {
if len(os.Args) != 2 {
if len(os.Args) < 2 {
usage(1)
}
switch os.Args[1] {
subcommand, args := os.Args[1], os.Args[2:]
switch subcommand {
case "upload":
if len(args) != 0 {
usage(1)
}
Upload(Scan())
case "generate":
if len(args) != 0 {
usage(1)
}
Generate(Scan())
case "schedule":
if len(args) != 1 {
usage(1)
}
count, err := strconv.ParseUint(args[0], 10, 32)
must(err)
PrintSchedule(count)
case "help", "--help":
if len(args) != 0 {
usage(1)
}
usage(0)
default:
usage(1)
}
}
@ -43,9 +64,10 @@ func fail(msg string, args ...interface{}) {
func usage(exitCode int) {
fmt.Fprintf(os.Stderr,
"Usage:\n\n"+
" %[1]s upload - Upload all relevant files for one episode to <sftp://ftp.c3d2.de>.\n"+
" %[1]s generate - Generate the c3d2-web news entry XML file for one episode.\n\n"+
"All relevant files (audios, chapter marks file, shownotes Markdown file) must be in the working directory.\n",
" %[1]s upload - Upload all relevant files for one episode to <sftp://ftp.c3d2.de>.\n"+
" %[1]s generate - Generate the c3d2-web news entry XML file for one episode.\n"+
" %[1]s schedule <N> - Print the next N airdates.\n\n"+
"For `upload` and `generate`, all relevant files (audios, chapter marks file, shownotes Markdown file) must be in the working directory.\n",
filepath.Base(os.Args[0]),
)
os.Exit(exitCode)

43
schedule.go Normal file
View File

@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright 2023 Stefan Majewsky <majewsky@gmx.net>
* SPDX-License-Identifier: GPL-3.0-only
* Refer to the file "LICENSE" for details.
*******************************************************************************/
package main
import (
"fmt"
"time"
)
func PrintSchedule(count uint64) {
now := time.Now().UTC()
date := time.Date(
now.Year(), now.Month(), now.Day(),
12, 0, 0, 0,
now.Location(),
)
for printed := uint64(0); printed < count; date = date.AddDate(0, 0, 1) {
if date.Weekday() == time.Tuesday && date.Day() > 21 && date.Day() <= 28 {
printDate(date)
printed++
}
}
}
var germanMonths = []string{
"Januar", "Februar", "März", "April", "Mai", "Juni",
"Juli", "August", "September", "Oktober", "November", "Dezember",
}
func printDate(t time.Time) {
fmt.Printf(
"%s = %d. %s %d\n",
t.Format(time.DateOnly),
t.Day(),
germanMonths[t.Month()-1],
t.Year(),
)
}