[24] | 1 | package main
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "strconv"
|
---|
| 5 | "strings"
|
---|
| 6 | "time"
|
---|
| 7 |
|
---|
| 8 | "github.com/drhodes/golorem"
|
---|
| 9 | )
|
---|
| 10 |
|
---|
| 11 | // zs var <filename> -- returns list of variables and their values
|
---|
| 12 | // zs var <filename> <var...> -- returns list of variable values
|
---|
| 13 | func Var(args []string) string {
|
---|
| 14 | if len(args) == 0 {
|
---|
| 15 | return "var: filename expected"
|
---|
| 16 | } else {
|
---|
| 17 | s := ""
|
---|
| 18 | if vars, _, err := md(args[0], globals()); err != nil {
|
---|
| 19 | return "var: " + err.Error()
|
---|
| 20 | } else {
|
---|
| 21 | if len(args) > 1 {
|
---|
| 22 | for _, a := range args[1:] {
|
---|
| 23 | s = s + vars[a] + "\n"
|
---|
| 24 | }
|
---|
| 25 | } else {
|
---|
| 26 | for k, v := range vars {
|
---|
| 27 | s = s + k + ":" + v + "\n"
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 | return strings.TrimSpace(s)
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | // zs lorem <n> -- returns <n> random lorem ipsum sentences
|
---|
| 36 | func Lorem(args []string) string {
|
---|
| 37 | if len(args) > 1 {
|
---|
| 38 | return "lorem: invalid usage"
|
---|
| 39 | }
|
---|
| 40 | if len(args) == 0 {
|
---|
| 41 | return lorem.Paragraph(5, 5)
|
---|
| 42 | }
|
---|
| 43 | if n, err := strconv.Atoi(args[0]); err == nil {
|
---|
| 44 | return lorem.Paragraph(n, n)
|
---|
| 45 | } else {
|
---|
| 46 | return "lorem: " + err.Error()
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | // zs datefmt <fmt> <date> -- returns formatted date from unix time
|
---|
| 51 | func DateFmt(args []string) string {
|
---|
| 52 | if len(args) == 0 || len(args) > 2 {
|
---|
| 53 | return "datefmt: invalid usage"
|
---|
| 54 | }
|
---|
| 55 | if n, err := strconv.ParseInt(args[1], 10, 64); err == nil {
|
---|
| 56 | return time.Unix(n, 0).Format(args[0])
|
---|
| 57 | } else {
|
---|
| 58 | return "datefmt: " + err.Error()
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // zs dateparse <fmt> <date> -- returns unix time from the formatted date
|
---|
| 63 | func DateParse(args []string) string {
|
---|
| 64 | if len(args) == 0 || len(args) > 2 {
|
---|
| 65 | return "dateparse: invalid usage"
|
---|
| 66 | }
|
---|
| 67 | if d, err := time.Parse(args[0], args[1]); err != nil {
|
---|
| 68 | return "dateparse: " + err.Error()
|
---|
| 69 | } else {
|
---|
| 70 | return strconv.FormatInt(d.Unix(), 10)
|
---|
| 71 | }
|
---|
| 72 | }
|
---|