src

Go monorepo.
Log | Files | Refs

commit c18e97e8345054ee67db1cf4416be9c866c0d919
parent fcea75e60c9f415bcf2d58bec25a4867e5ae3d38
Author: dwrz <dwrz@dwrz.net>
Date:   Fri,  2 Dec 2022 14:47:22 +0000

Add ymd cmd

Diffstat:
Acmd/ymd/main.go | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+), 0 deletions(-)

diff --git a/cmd/ymd/main.go b/cmd/ymd/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "flag" + "fmt" + "os" + "time" +) + +var ( + now = time.Now() + + year = flag.Int( + "y", 0, + "year; defaults to current year", + ) + month = flag.Int( + "m", 0, + "month; defaults to current month", + ) + + ym = flag.Bool("ym", false, "create YY-MM directories") +) + +// TODO: path arguments. +func main() { + flag.Parse() + + if *year < 0 { + fmt.Fprintf(os.Stderr, "invalid year: %d\n", *year) + return + } + if *year == 0 { + y := now.Year() + year = &y + } + + // Create YY-MM directories. + if *ym { + mkdirYM(*year) + return + } + + // Otherwise, create YYYY-MM-DD directories. + if *month < 0 || *month > 12 { + fmt.Fprintf(os.Stderr, "invalid month: %d\n", month) + return + } + if *month == 0 { + m := int(now.Month()) + month = &m + } + + mkdirYMD(*year, *month) +} + +func mkdirYM(year int) { + for m := 1; m <= 12; m++ { + name := fmt.Sprintf("%d-%02d", year, m) + if err := os.Mkdir(name, os.ModePerm); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + } + } +} + +func mkdirYMD(year, month int) { + // Calculate the last day of the month in the input year. + last := time.Date( + year, time.Month(month), 1, 0, 0, 0, 0, time.Local, + ).AddDate(0, 1, 0).Add(-time.Nanosecond).Day() + + // Create directories for each day of the month. + for d := 1; d <= last; d++ { + name := fmt.Sprintf("%d-%02d-%02d", year, month, d) + if err := os.Mkdir(name, os.ModePerm); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + } + } +}