-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopics.go
111 lines (93 loc) · 3.04 KB
/
topics.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2024 The listen.dev team <engineering@garnet.ai>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package help
import (
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/listendev/lstn/pkg/text"
"github.com/spf13/cobra"
)
var Topics = map[string]map[string]string{
"config": {
"short": "Details about the ~/.lstn.yaml config file",
},
"environment": {
"alias": "env",
"short": "Which environment variables you can use with lstn",
},
"exit": {
"short": "Details about the lstn exit codes",
"long": heredoc.Doc(`
The lstn CLI follows the usual conventions regarding exit codes.
Meaning:
* when a command completes successfully, the exit code will be 0
* when a command fails for any reason, the exit code will be 1
* when a command is running but gets cancelled, the exit code will be 2
* when a command meets an authentication issue, the exit code will be 4
Notice that it's possible that a particular command may have more exit codes,
so it's a good practice to check the docs for the specific command
in case you're relying on the exit codes to control some behaviour.
`),
},
"manual": {
"short": "A comprehensive reference of all the lstn commands",
},
"reporters": {
"short": "A comprehensive guide to the `lstn` reporting mechanisms",
},
}
type TopicFunc func(*cobra.Command, []string)
var topicsHelpFuncs = map[string]func() TopicFunc{
"manual": manualHelpTopicFunc,
"environment": envHelpTopicFunc,
"config": configHelpTopicFunc,
"reporters": reportersHelpTopicFunc,
}
// TODO > print out markdown
func NewTopic(topic string) *cobra.Command {
c := &cobra.Command{
Use: topic,
DisableFlagsInUseLine: true,
Short: Topics[topic]["short"],
Long: Topics[topic]["long"],
Example: Topics[topic]["example"],
// TODO > remove these if unused
Annotations: map[string]string{
"markdown:generate": "true",
"markdown:basename": "lstn_help_" + topic,
},
}
if Topics[topic]["alias"] != "" {
c.Aliases = []string{Topics[topic]["alias"]}
}
c.SetHelpFunc(func(c *cobra.Command, args []string) {
if c.Long != "" {
c.Print(c.Long)
if c.Example != "" {
fmt.Fprintf(os.Stdout, "\n\nExamples:\n")
fmt.Fprintf(os.Stdout, "%s", text.Indent(c.Example, " "))
}
} else if topicsHelpFuncs[c.Use] != nil {
topicsHelpFuncs[c.Use]()(c, args)
}
})
c.SetUsageFunc(func(c *cobra.Command) error {
fmt.Fprintf(os.Stdout, "%s", c.Use)
return nil
})
return c
}