/
docs.go
73 lines (61 loc) · 1.88 KB
/
docs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package docs
import (
"fmt"
"os"
"strings"
"github.com/containrrr/shoutrrr/pkg/router"
"github.com/spf13/cobra"
cli "github.com/containrrr/shoutrrr/cli/cmd"
f "github.com/containrrr/shoutrrr/pkg/format"
)
var serviceRouter router.ServiceRouter
var services = serviceRouter.ListServices()
// Cmd prints documentation for services
var Cmd = &cobra.Command{
Use: "docs",
Short: "Print documentation for services",
Run: Run,
Args: func(cmd *cobra.Command, args []string) error {
serviceList := strings.Join(services, ", ")
cmd.SetUsageTemplate(cmd.UsageTemplate() + "\nAvailable services: \n " + serviceList + "\n")
return cobra.MinimumNArgs(1)(cmd, args)
},
ValidArgs: services,
}
func init() {
Cmd.Flags().StringP("format", "f", "console", "Output format")
}
// Run the docs command
func Run(cmd *cobra.Command, args []string) {
format, _ := cmd.Flags().GetString("format")
res := printDocs(format, args)
if res.ExitCode != 0 {
_, _ = fmt.Fprintf(os.Stderr, res.Message)
}
os.Exit(res.ExitCode)
}
func printDocs(format string, services []string) cli.Result {
var renderer f.TreeRenderer
switch format {
case "console":
renderer = f.ConsoleTreeRenderer{WithValues: false}
case "markdown":
renderer = f.MarkdownTreeRenderer{
HeaderPrefix: "### ",
PropsDescription: "Props can be either supplied using the params argument, or through the URL using \n`?key=value&key=value` etc.\n",
PropsEmptyMessage: "*The services does not support any query/param props*",
}
default:
return cli.InvalidUsage("invalid format")
}
for _, scheme := range services {
service, err := serviceRouter.NewService(scheme)
if err != nil {
return cli.InvalidUsage("failed to init service: " + err.Error())
}
config := f.GetServiceConfig(service)
configNode := f.GetConfigFormat(config)
fmt.Println(renderer.RenderTree(configNode, scheme))
}
return cli.Success
}