Replace flag with go-arg because flag sucks
This changes the command line arguments from -target-hour, -target-minute, etc., to --hour, --minute, etc. and includes the short forms as well (eg, -h and -m). Also, added go.mod and go.sum.
This commit is contained in:
parent
6da16777cf
commit
619e4b9a07
|
@ -0,0 +1,8 @@
|
||||||
|
module countdown
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/alexflint/go-arg v1.4.3 // indirect
|
||||||
|
github.com/alexflint/go-scalar v1.1.0 // indirect
|
||||||
|
)
|
|
@ -0,0 +1,12 @@
|
||||||
|
github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo=
|
||||||
|
github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA=
|
||||||
|
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
|
||||||
|
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
120
main.go
120
main.go
|
@ -1,13 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/alexflint/go-arg"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -19,82 +20,59 @@ import (
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
type Arguments struct {
|
||||||
|
Hour int `arg:"--hour,-h" help:"Target hour" default:"-1"`
|
||||||
|
Minute int `arg:"--minute,-m" help:"Target minute" default:"-1"`
|
||||||
|
Year int `arg:"--year" help:"Target year" default:"-1"`
|
||||||
|
Day int `arg:"--day" help:"Target day" default:"-1"`
|
||||||
|
Month int `arg:"--month" help:"Target month" default:"-1"`
|
||||||
|
|
||||||
|
Output string `arg:"--output,-o" help:"Output filename" default:"/tmp/countdown.txt"`
|
||||||
|
Done string `arg:"--done,-d" help:"String to display when done" default:"MOVIE SIGN"`
|
||||||
|
Format string `arg:"--format,-f" help:"Format string for the countdown" default:"{{if .Hours}}{{.Hours}}h {{end}}{{if or .Minutes .Hours}}{{.Minutes}}m {{end}}{{.Seconds}}s"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
args := &Arguments{}
|
||||||
|
arg.MustParse(args)
|
||||||
|
|
||||||
var filename string
|
|
||||||
var untilStr string
|
|
||||||
var targetTimeStr string
|
|
||||||
var doneStr string
|
|
||||||
var format string
|
|
||||||
|
|
||||||
flag.StringVar(&filename, "o", "/tmp/countdown.txt", "Output file to write the countdown to.")
|
|
||||||
flag.StringVar(&untilStr, "u", "", "Countdown duration")
|
|
||||||
flag.StringVar(&targetTimeStr, "t", "", "Countdown to time")
|
|
||||||
flag.StringVar(&doneStr, "d", "00:00:00", "String to display when done")
|
|
||||||
//flag.StringVar(&format, "f", "%H:%M:%S", "Format for the countdown")
|
|
||||||
flag.StringVar(&format, "f", "{{if .Hours}}{{.Hours}}h {{end}}{{if or .Minutes .Hours}}{{.Minutes}}m {{end}}{{.Seconds}}s", "Format for the countdown")
|
|
||||||
|
|
||||||
var targetYear int
|
|
||||||
var targetDay int
|
|
||||||
var targetMonth int
|
|
||||||
|
|
||||||
flag.IntVar(&targetYear, "target-year", -1, "")
|
|
||||||
flag.IntVar(&targetDay, "target-day", -1, "")
|
|
||||||
flag.IntVar(&targetMonth, "target-month", -1, "")
|
|
||||||
|
|
||||||
var targetHour int
|
|
||||||
var targetMinute int
|
|
||||||
var targetSecond int
|
|
||||||
|
|
||||||
flag.IntVar(&targetHour, "target-hour", -1, "")
|
|
||||||
flag.IntVar(&targetMinute, "target-minute", 0, "")
|
|
||||||
flag.IntVar(&targetSecond, "target-second", 0, "")
|
|
||||||
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
var until time.Time
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
if untilStr != "" {
|
if args.Hour == -1 {
|
||||||
untilDuration, err := time.ParseDuration(untilStr)
|
args.Hour = now.Hour()
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
until = time.Now().Add(untilDuration)
|
|
||||||
} else {
|
|
||||||
if targetYear == -1 {
|
|
||||||
targetYear = now.Year()
|
|
||||||
}
|
|
||||||
if targetMonth == -1 {
|
|
||||||
targetMonth = int(now.Month())
|
|
||||||
}
|
|
||||||
if targetDay == -1 {
|
|
||||||
targetDay = now.Day()
|
|
||||||
}
|
|
||||||
|
|
||||||
if targetHour == -1 {
|
|
||||||
targetHour = now.Hour()
|
|
||||||
}
|
|
||||||
if targetMinute == -1 {
|
|
||||||
targetMinute = now.Minute()
|
|
||||||
}
|
|
||||||
if targetSecond == -1 {
|
|
||||||
targetSecond = now.Second()
|
|
||||||
}
|
|
||||||
|
|
||||||
until = time.Date(
|
|
||||||
targetYear, time.Month(targetMonth), targetDay,
|
|
||||||
targetHour, targetMinute, targetSecond,
|
|
||||||
0, time.Local,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Output file: %s\nCountdown to: %s\n", filename, until)
|
if args.Minute == -1 {
|
||||||
|
args.Minute = now.Minute()
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.Year == -1 {
|
||||||
|
args.Year = now.Year()
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.Month == -1 {
|
||||||
|
args.Month = int(now.Month())
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.Day == -1 {
|
||||||
|
args.Day = now.Day()
|
||||||
|
}
|
||||||
|
|
||||||
|
until := time.Date(
|
||||||
|
args.Year, time.Month(args.Month), args.Day,
|
||||||
|
args.Hour, args.Minute, 0, 0, time.Local,
|
||||||
|
)
|
||||||
|
|
||||||
|
if now.After(until) {
|
||||||
|
fmt.Println("time is in the past!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Output file: %s\nCountdown to: %s\n", args.Output, until)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
tp := template.New("time")
|
tp := template.New("time")
|
||||||
tp, err = tp.Parse(format)
|
tp, err = tp.Parse(args.Format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -121,7 +99,7 @@ func main() {
|
||||||
Seconds: s,
|
Seconds: s,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do I want to use this format as well as the complicated AF one?I want to use this format as well as the complicated AF template one?
|
// Do I want to use this format as well as the complicated AF one?
|
||||||
//str := format
|
//str := format
|
||||||
//if hide && h == 0 {
|
//if hide && h == 0 {
|
||||||
// str = strings.ReplaceAll(str, "%H", "")
|
// str = strings.ReplaceAll(str, "%H", "")
|
||||||
|
@ -144,7 +122,7 @@ func main() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile(filename, []byte(sb.String()), 0644)
|
err = os.WriteFile(args.Output, []byte(sb.String()), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("unable to write file: %v\n", err)
|
fmt.Println("unable to write file: %v\n", err)
|
||||||
}
|
}
|
||||||
|
@ -155,7 +133,7 @@ func main() {
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
err = os.WriteFile(filename, []byte(doneStr), 0644)
|
err = os.WriteFile(args.Output, []byte(args.Done), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("unable to write file: %v\n", err)
|
fmt.Println("unable to write file: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue