Skip to content

Commit b9d076a

Browse files
formats
1 parent 5dbb353 commit b9d076a

10 files changed

Lines changed: 139 additions & 33 deletions

File tree

cli.code-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"titleBar.activeForeground": "#28075d",
1313
"statusBar.background": "#28075d"
1414
},
15-
"go.goroot": "/nix/store/0jjqri305gwmzg8j1zjpkjbgllfv209s-go-1.22.5/share/go"
15+
"go.goroot": "/nix/store/16g3awzjv3341s27hkkkv1vk5dw4206m-go-1.22.5/share/go"
1616
}
1717
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ require (
88
github.com/cenkalti/backoff/v4 v4.3.0
99
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
1010
github.com/go-git/go-git/v5 v5.12.0
11+
github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6
1112
github.com/google/uuid v1.6.0
1213
github.com/h2non/filetype v1.1.3
1314
github.com/itchyny/gojq v0.12.16
15+
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
1416
github.com/mitchellh/go-homedir v1.1.0
1517
github.com/nicksnyder/go-i18n v1.10.1
1618
github.com/nuvolaris/envsubst/cmd/envsubstmain v0.0.0-20230603163828-10ca029cdb58
@@ -76,6 +78,7 @@ require (
7678
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
7779
github.com/mitchellh/reflectwalk v1.0.2 // indirect
7880
github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5 // indirect
81+
github.com/olekukonko/tablewriter v0.0.5 // indirect
7982
github.com/pelletier/go-toml v1.8.1 // indirect
8083
github.com/pjbgf/sha1cd v0.3.0 // indirect
8184
github.com/pkg/errors v0.9.1 // indirect
@@ -89,6 +92,7 @@ require (
8992
github.com/skeema/knownhosts v1.2.2 // indirect
9093
github.com/spf13/cobra v1.1.3 // indirect
9194
github.com/spf13/pflag v1.0.5 // indirect
95+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
9296
github.com/tidwall/gjson v1.14.0 // indirect
9397
github.com/tidwall/match v1.1.1 // indirect
9498
github.com/tidwall/pretty v1.2.0 // indirect

go.sum

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
145145
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
146146
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
147147
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
148+
github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6 h1:ZPy+2XJ8u0bB3sNFi+I72gMEMS7MTg7aZCCXPOjV8iw=
149+
github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
148150
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
149151
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
150152
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
@@ -207,6 +209,8 @@ github.com/itchyny/gojq v0.12.16 h1:yLfgLxhIr/6sJNVmYfQjTIv0jGctu6/DgDoivmxTr7g=
207209
github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM=
208210
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
209211
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
212+
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 h1:iCHtR9CQyktQ5+f3dMVZfwD2KWJUgm7M0gdL9NGr8KA=
213+
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
210214
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
211215
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
212216
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
@@ -246,6 +250,7 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
246250
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
247251
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
248252
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
253+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
249254
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
250255
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
251256
github.com/mattn/go-zglob v0.0.4 h1:LQi2iOm0/fGgu80AioIJ/1j9w9Oh+9DZ39J4VAGzHQM=
@@ -289,6 +294,8 @@ github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI
289294
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
290295
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
291296
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
297+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
298+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
292299
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
293300
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
294301
github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4=
@@ -345,8 +352,6 @@ github.com/sciabarracom/openwhisk-wskdeploy v1.2.1 h1:t5hgca9OFEO39t4OqIxcWybYRj
345352
github.com/sciabarracom/openwhisk-wskdeploy v1.2.1/go.mod h1:bWjrIOIAzt8AcPqiJ0mWEecUxXbT4bY4ysMlJO6/q78=
346353
github.com/sciabarracom/sh/v3 v3.8.3 h1:5afHdZzNkW5vgUMP7gHUpBYHezn3BoQsyNgKXKdTpkM=
347354
github.com/sciabarracom/sh/v3 v3.8.3/go.mod h1:CeMVr3PRLhlUrS+VPLTp8dS5/6xNzmmX0h/WVHRR6DA=
348-
github.com/sciabarracom/task/v3 v3.38.10 h1:KSu6B2CuHWail7rlTx5W72qM1klJbED3z2k7RzBvMkE=
349-
github.com/sciabarracom/task/v3 v3.38.10/go.mod h1:EMjHs6YrTofApnomes/CNiGQ1odZUZF6PTqmYW9EBic=
350355
github.com/sciabarracom/task/v3 v3.38.11 h1:TZeaepSVc9Lx7TowAqIV3K9IZQh+jYgKNDpBNIg38uU=
351356
github.com/sciabarracom/task/v3 v3.38.11/go.mod h1:EMjHs6YrTofApnomes/CNiGQ1odZUZF6PTqmYW9EBic=
352357
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
@@ -374,6 +379,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
374379
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
375380
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
376381
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
382+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
383+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
377384
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
378385
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
379386
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=

tools/base64.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
package tools
1919

2020
import (
21+
_ "embed"
2122
"encoding/base64"
2223
"flag"
2324
"fmt"
2425
)
2526

27+
//go:embed base64.md
28+
var base64usage string
29+
2630
func base64Tool() error {
2731

2832
var (
@@ -32,12 +36,7 @@ func base64Tool() error {
3236
)
3337

3438
flag.Usage = func() {
35-
fmt.Println(`Usage: ops -base64 [options] <string>
36-
37-
Options:
38-
-h, --help Display this help message
39-
-e, --encode <string> Encode a string to base64
40-
-d, --decode <string> Decode a base64 string`)
39+
fmt.Println(MarkdownToText(base64usage))
4140
}
4241

4342
flag.BoolVar(&helpFlag, "h", false, "Display this help message")

tools/base64.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tool `-base64`
2+
3+
```text
4+
Usage:
5+
ops -base64 [options] <string>
6+
```
7+
8+
# Options
9+
10+
```
11+
-h, --help Display this help message
12+
-e, --encode <string> Encode a string to base64
13+
-d, --decode <string> Decode a base64 string
14+
```

tools/datefmt.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,17 @@
1818
package tools
1919

2020
import (
21+
_ "embed"
2122
"flag"
2223
"fmt"
24+
"os"
2325
"time"
24-
25-
"golang.org/x/exp/maps"
2626
)
2727

28-
func printDateFmtUsage() {
29-
fmts := maps.Keys(dateFormats)
30-
31-
fmt.Printf(`Usage:
32-
ops -datefmt [options] [arguments]
33-
34-
Print date with different formats. If no time stamp or date strings are given, uses current time.
35-
36-
-h, --help print this help info
37-
-t, --timestamp unix timestamp to format (default: current time)
38-
-s, --str date string to format
39-
--if input format to use with input date string (via --str)
40-
-f, --of output format to use (default: UnixDate)
41-
42-
Possible formats: %v
43-
`, fmts)
44-
}
28+
//go:embed datefmt.md
29+
var datefmtUsage string
4530

31+
// if you change this add it to the datefmt.md
4632
var dateFormats = map[string]string{
4733
"Layout": time.Layout,
4834
"ANSIC": time.ANSIC,
@@ -77,8 +63,8 @@ var (
7763
oFmtFlag string
7864
)
7965

80-
func DateFmtTool() error {
81-
flag.Usage = printDateFmtUsage
66+
func DateFmtTool(args []string) error {
67+
os.Args = args
8268

8369
flag.BoolVar(&helpFlag, "h", false, "print this help info")
8470
flag.BoolVar(&helpFlag, "help", false, "print this help info")
@@ -93,7 +79,7 @@ func DateFmtTool() error {
9379
flag.Parse()
9480

9581
if helpFlag {
96-
flag.Usage()
82+
fmt.Println(MarkdownToText(datefmtUsage))
9783
return nil
9884
}
9985

tools/datefmt.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Tool: datefmt
2+
3+
Print date with different formats. If no time stamp or date strings are given, uses current time
4+
5+
```text
6+
Usage:
7+
ops -datefmt [options] [arguments]
8+
```
9+
10+
# Options
11+
12+
```
13+
-h, --help print this help info
14+
-t, --timestamp unix timestamp to format (default: current time)
15+
-s, --str date string to format
16+
--if input format to use with input date string (via --str)
17+
-f, --of output format to use (default: UnixDate)
18+
```
19+
20+
Possible formats (they follows the standard naming of go time formats, with the addition of 'Millisecond' and 'ms'):
21+
22+
- Layout
23+
- ANSIC
24+
- UnixDate
25+
- RubyDate
26+
- RFC822
27+
- RFC822Z
28+
- RFC850
29+
- RFC1123
30+
- RFC1123Z
31+
- RFC3339
32+
- RFC3339Nano
33+
- Kitchen
34+
- Stamp
35+
- StampMilli
36+
- StampMicro
37+
- StampNano
38+
- DateTime
39+
- DateOnly
40+
- TimeOnly
41+
- Milliseconds
42+
- ms

tools/render.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tools
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/gomarkdown/markdown"
7+
"github.com/gomarkdown/markdown/html"
8+
"github.com/gomarkdown/markdown/parser"
9+
"github.com/jaytaylor/html2text"
10+
)
11+
12+
func ExtractUsage(input string) string {
13+
// Regular expression to match the block of text
14+
re := regexp.MustCompile("(?s)```text\n(.*?Usage:.*?[^`]*)```")
15+
16+
// Find the match
17+
matches := re.FindStringSubmatch(input)
18+
19+
// Return the match, or an empty string if no match is found
20+
if len(matches) > 0 {
21+
return matches[1]
22+
}
23+
return "Cannot find an Usage: block"
24+
}
25+
26+
func MarkdownToText(input string) string {
27+
// create markdown parser with extensions
28+
extensions := parser.CommonExtensions | parser.NoEmptyLineBeforeBlock
29+
p := parser.NewWithExtensions(extensions)
30+
doc := p.Parse([]byte(input))
31+
htmlFlags := html.CommonFlags | html.HrefTargetBlank
32+
opts := html.RendererOptions{Flags: htmlFlags}
33+
renderer := html.NewRenderer(opts)
34+
markdown := markdown.Render(doc, renderer)
35+
36+
text, err := html2text.FromString(string(markdown), html2text.Options{PrettyTables: true})
37+
if err != nil {
38+
return err.Error()
39+
}
40+
return text
41+
}

tools/render_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package tools
2+
3+
import "fmt"
4+
5+
func Example_markdown2text() {
6+
fmt.Println(len(MarkdownToText(base64usage)))
7+
// Output: 251
8+
}
9+
10+
func Example_extractUsage() {
11+
fmt.Println(len(ExtractUsage(base64usage)))
12+
// Output: 40
13+
14+
}

tools/tools.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ func RunTool(name string, args []string) (int, error) {
101101
}
102102

103103
case "datefmt":
104-
os.Args = append([]string{"datefmt"}, args...)
105-
if err := DateFmtTool(); err != nil {
104+
if err := DateFmtTool(append([]string{"datefmt"}, args...)); err != nil {
106105
return 1, err
107106
}
108107

0 commit comments

Comments
 (0)