Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/cloudwatch_exporter/docs/doc.go
5327 views
1
package main
2
3
import (
4
_ "embed"
5
"fmt"
6
"log"
7
"os"
8
"strings"
9
10
yaceConf "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"
11
)
12
13
//go:embed template.md
14
var docTemplate string
15
16
const servicesListReplacement = "{{SERVICE_LIST}}"
17
18
// main is used for programmatically generating a documentation section containing all AWS services supported in cloudwatch
19
// exporter discovery jobs.
20
func main() {
21
programName := os.Args[0]
22
argsWithoutProgram := os.Args[1:]
23
if len(argsWithoutProgram) < 1 {
24
log.Println("Missing arguments: generate OR check <file>")
25
os.Exit(1)
26
}
27
doc := generateServicesDocSection()
28
switch argsWithoutProgram[0] {
29
case "generate":
30
fmt.Println(doc)
31
case "check":
32
if len(argsWithoutProgram) < 2 {
33
log.Println("Missing arguments: check <file>")
34
os.Exit(1)
35
}
36
fileToCheck := argsWithoutProgram[1]
37
if err := checkServicesDocSection(fileToCheck, doc); err != nil {
38
log.Printf("Check failed: %s\n", err)
39
log.Printf("Try updating %s with the services section produced by `%s generate`\n", fileToCheck, programName)
40
os.Exit(1)
41
}
42
log.Println("Check successful!")
43
default:
44
log.Printf("Unknown command: %s\n", argsWithoutProgram[0])
45
os.Exit(1)
46
}
47
}
48
49
func checkServicesDocSection(path string, expectedDoc string) error {
50
contents, err := os.ReadFile(path)
51
if err != nil {
52
return fmt.Errorf("failed to read file to check: %w", err)
53
}
54
if !strings.Contains(string(contents), strings.TrimRight(expectedDoc, "\n")) {
55
return fmt.Errorf("doc has no services section, or is out of date")
56
}
57
return nil
58
}
59
60
func generateServicesDocSection() string {
61
var sb strings.Builder
62
for _, supportedSvc := range yaceConf.SupportedServices {
63
sb.WriteString(
64
fmt.Sprintf("- Namespace: `%s` or Alias: `%s`\n", supportedSvc.Namespace, supportedSvc.Alias),
65
)
66
}
67
doc := strings.Replace(docTemplate, servicesListReplacement, sb.String(), 1)
68
return doc
69
}
70
71