You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openserverless.apache.org by ms...@apache.org on 2024/08/09 14:10:09 UTC
(openserverless-cli) 01/02: formats
This is an automated email from the ASF dual-hosted git repository.
msciabarra pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/openserverless-cli.git
commit b9d076ab7d52199757d5eca13c5c7c5eee69797b
Author: Michele Sciabarra <mi...@sciabarra.com>
AuthorDate: Fri Aug 9 15:23:14 2024 +0200
formats
---
cli.code-workspace | 2 +-
go.mod | 4 ++++
go.sum | 11 +++++++++--
tools/base64.go | 11 +++++------
tools/base64.md | 14 ++++++++++++++
tools/datefmt.go | 30 ++++++++----------------------
tools/datefmt.md | 42 ++++++++++++++++++++++++++++++++++++++++++
tools/render.go | 41 +++++++++++++++++++++++++++++++++++++++++
tools/render_test.go | 14 ++++++++++++++
tools/tools.go | 3 +--
10 files changed, 139 insertions(+), 33 deletions(-)
diff --git a/cli.code-workspace b/cli.code-workspace
index cd70ba5..68e91f6 100644
--- a/cli.code-workspace
+++ b/cli.code-workspace
@@ -12,6 +12,6 @@
"titleBar.activeForeground": "#28075d",
"statusBar.background": "#28075d"
},
- "go.goroot": "/nix/store/0jjqri305gwmzg8j1zjpkjbgllfv209s-go-1.22.5/share/go"
+ "go.goroot": "/nix/store/16g3awzjv3341s27hkkkv1vk5dw4206m-go-1.22.5/share/go"
}
}
diff --git a/go.mod b/go.mod
index 62b0d83..ed7fcd0 100644
--- a/go.mod
+++ b/go.mod
@@ -8,9 +8,11 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/go-git/go-git/v5 v5.12.0
+ github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6
github.com/google/uuid v1.6.0
github.com/h2non/filetype v1.1.3
github.com/itchyny/gojq v0.12.16
+ github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
github.com/mitchellh/go-homedir v1.1.0
github.com/nicksnyder/go-i18n v1.10.1
github.com/nuvolaris/envsubst/cmd/envsubstmain v0.0.0-20230603163828-10ca029cdb58
@@ -76,6 +78,7 @@ require (
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/nwidger/jsoncolor v0.0.0-20170215171346-75a6de4340e5 // indirect
+ github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
@@ -89,6 +92,7 @@ require (
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/spf13/cobra v1.1.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
+ github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/tidwall/gjson v1.14.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
diff --git a/go.sum b/go.sum
index d55390a..183df70 100644
--- a/go.sum
+++ b/go.sum
@@ -145,6 +145,8 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
+github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6 h1:ZPy+2XJ8u0bB3sNFi+I72gMEMS7MTg7aZCCXPOjV8iw=
+github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
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=
github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM=
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
+github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 h1:iCHtR9CQyktQ5+f3dMVZfwD2KWJUgm7M0gdL9NGr8KA=
+github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
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
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
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
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
+github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
+github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4=
@@ -345,8 +352,6 @@ github.com/sciabarracom/openwhisk-wskdeploy v1.2.1 h1:t5hgca9OFEO39t4OqIxcWybYRj
github.com/sciabarracom/openwhisk-wskdeploy v1.2.1/go.mod h1:bWjrIOIAzt8AcPqiJ0mWEecUxXbT4bY4ysMlJO6/q78=
github.com/sciabarracom/sh/v3 v3.8.3 h1:5afHdZzNkW5vgUMP7gHUpBYHezn3BoQsyNgKXKdTpkM=
github.com/sciabarracom/sh/v3 v3.8.3/go.mod h1:CeMVr3PRLhlUrS+VPLTp8dS5/6xNzmmX0h/WVHRR6DA=
-github.com/sciabarracom/task/v3 v3.38.10 h1:KSu6B2CuHWail7rlTx5W72qM1klJbED3z2k7RzBvMkE=
-github.com/sciabarracom/task/v3 v3.38.10/go.mod h1:EMjHs6YrTofApnomes/CNiGQ1odZUZF6PTqmYW9EBic=
github.com/sciabarracom/task/v3 v3.38.11 h1:TZeaepSVc9Lx7TowAqIV3K9IZQh+jYgKNDpBNIg38uU=
github.com/sciabarracom/task/v3 v3.38.11/go.mod h1:EMjHs6YrTofApnomes/CNiGQ1odZUZF6PTqmYW9EBic=
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=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
+github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
+github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
diff --git a/tools/base64.go b/tools/base64.go
index 47a414a..1e286c1 100644
--- a/tools/base64.go
+++ b/tools/base64.go
@@ -18,11 +18,15 @@
package tools
import (
+ _ "embed"
"encoding/base64"
"flag"
"fmt"
)
+//go:embed base64.md
+var base64usage string
+
func base64Tool() error {
var (
@@ -32,12 +36,7 @@ func base64Tool() error {
)
flag.Usage = func() {
- fmt.Println(`Usage: ops -base64 [options] <string>
-
-Options:
- -h, --help Display this help message
- -e, --encode <string> Encode a string to base64
- -d, --decode <string> Decode a base64 string`)
+ fmt.Println(MarkdownToText(base64usage))
}
flag.BoolVar(&helpFlag, "h", false, "Display this help message")
diff --git a/tools/base64.md b/tools/base64.md
new file mode 100644
index 0000000..03506ec
--- /dev/null
+++ b/tools/base64.md
@@ -0,0 +1,14 @@
+# Tool `-base64`
+
+```text
+Usage:
+ ops -base64 [options] <string>
+```
+
+# Options
+
+```
+ -h, --help Display this help message
+ -e, --encode <string> Encode a string to base64
+ -d, --decode <string> Decode a base64 string
+```
\ No newline at end of file
diff --git a/tools/datefmt.go b/tools/datefmt.go
index 432605e..cc62552 100644
--- a/tools/datefmt.go
+++ b/tools/datefmt.go
@@ -18,31 +18,17 @@
package tools
import (
+ _ "embed"
"flag"
"fmt"
+ "os"
"time"
-
- "golang.org/x/exp/maps"
)
-func printDateFmtUsage() {
- fmts := maps.Keys(dateFormats)
-
- fmt.Printf(`Usage:
-ops -datefmt [options] [arguments]
-
-Print date with different formats. If no time stamp or date strings are given, uses current time.
-
--h, --help print this help info
--t, --timestamp unix timestamp to format (default: current time)
--s, --str date string to format
---if input format to use with input date string (via --str)
--f, --of output format to use (default: UnixDate)
-
-Possible formats: %v
-`, fmts)
-}
+//go:embed datefmt.md
+var datefmtUsage string
+// if you change this add it to the datefmt.md
var dateFormats = map[string]string{
"Layout": time.Layout,
"ANSIC": time.ANSIC,
@@ -77,8 +63,8 @@ var (
oFmtFlag string
)
-func DateFmtTool() error {
- flag.Usage = printDateFmtUsage
+func DateFmtTool(args []string) error {
+ os.Args = args
flag.BoolVar(&helpFlag, "h", false, "print this help info")
flag.BoolVar(&helpFlag, "help", false, "print this help info")
@@ -93,7 +79,7 @@ func DateFmtTool() error {
flag.Parse()
if helpFlag {
- flag.Usage()
+ fmt.Println(MarkdownToText(datefmtUsage))
return nil
}
diff --git a/tools/datefmt.md b/tools/datefmt.md
new file mode 100644
index 0000000..79d4158
--- /dev/null
+++ b/tools/datefmt.md
@@ -0,0 +1,42 @@
+# Tool: datefmt
+
+Print date with different formats. If no time stamp or date strings are given, uses current time
+
+```text
+Usage:
+ ops -datefmt [options] [arguments]
+```
+
+# Options
+
+```
+ -h, --help print this help info
+ -t, --timestamp unix timestamp to format (default: current time)
+ -s, --str date string to format
+ --if input format to use with input date string (via --str)
+ -f, --of output format to use (default: UnixDate)
+```
+
+Possible formats (they follows the standard naming of go time formats, with the addition of 'Millisecond' and 'ms'):
+
+- Layout
+- ANSIC
+- UnixDate
+- RubyDate
+- RFC822
+- RFC822Z
+- RFC850
+- RFC1123
+- RFC1123Z
+- RFC3339
+- RFC3339Nano
+- Kitchen
+- Stamp
+- StampMilli
+- StampMicro
+- StampNano
+- DateTime
+- DateOnly
+- TimeOnly
+- Milliseconds
+- ms
diff --git a/tools/render.go b/tools/render.go
new file mode 100644
index 0000000..479838c
--- /dev/null
+++ b/tools/render.go
@@ -0,0 +1,41 @@
+package tools
+
+import (
+ "regexp"
+
+ "github.com/gomarkdown/markdown"
+ "github.com/gomarkdown/markdown/html"
+ "github.com/gomarkdown/markdown/parser"
+ "github.com/jaytaylor/html2text"
+)
+
+func ExtractUsage(input string) string {
+ // Regular expression to match the block of text
+ re := regexp.MustCompile("(?s)```text\n(.*?Usage:.*?[^`]*)```")
+
+ // Find the match
+ matches := re.FindStringSubmatch(input)
+
+ // Return the match, or an empty string if no match is found
+ if len(matches) > 0 {
+ return matches[1]
+ }
+ return "Cannot find an Usage: block"
+}
+
+func MarkdownToText(input string) string {
+ // create markdown parser with extensions
+ extensions := parser.CommonExtensions | parser.NoEmptyLineBeforeBlock
+ p := parser.NewWithExtensions(extensions)
+ doc := p.Parse([]byte(input))
+ htmlFlags := html.CommonFlags | html.HrefTargetBlank
+ opts := html.RendererOptions{Flags: htmlFlags}
+ renderer := html.NewRenderer(opts)
+ markdown := markdown.Render(doc, renderer)
+
+ text, err := html2text.FromString(string(markdown), html2text.Options{PrettyTables: true})
+ if err != nil {
+ return err.Error()
+ }
+ return text
+}
diff --git a/tools/render_test.go b/tools/render_test.go
new file mode 100644
index 0000000..994fbb9
--- /dev/null
+++ b/tools/render_test.go
@@ -0,0 +1,14 @@
+package tools
+
+import "fmt"
+
+func Example_markdown2text() {
+ fmt.Println(len(MarkdownToText(base64usage)))
+ // Output: 251
+}
+
+func Example_extractUsage() {
+ fmt.Println(len(ExtractUsage(base64usage)))
+ // Output: 40
+
+}
diff --git a/tools/tools.go b/tools/tools.go
index 397cea8..4c8257a 100644
--- a/tools/tools.go
+++ b/tools/tools.go
@@ -101,8 +101,7 @@ func RunTool(name string, args []string) (int, error) {
}
case "datefmt":
- os.Args = append([]string{"datefmt"}, args...)
- if err := DateFmtTool(); err != nil {
+ if err := DateFmtTool(append([]string{"datefmt"}, args...)); err != nil {
return 1, err
}