Path: blob/main/pkg/integrations/cloudwatch_exporter/docs/doc.go
5327 views
package main12import (3_ "embed"4"fmt"5"log"6"os"7"strings"89yaceConf "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"10)1112//go:embed template.md13var docTemplate string1415const servicesListReplacement = "{{SERVICE_LIST}}"1617// main is used for programmatically generating a documentation section containing all AWS services supported in cloudwatch18// exporter discovery jobs.19func main() {20programName := os.Args[0]21argsWithoutProgram := os.Args[1:]22if len(argsWithoutProgram) < 1 {23log.Println("Missing arguments: generate OR check <file>")24os.Exit(1)25}26doc := generateServicesDocSection()27switch argsWithoutProgram[0] {28case "generate":29fmt.Println(doc)30case "check":31if len(argsWithoutProgram) < 2 {32log.Println("Missing arguments: check <file>")33os.Exit(1)34}35fileToCheck := argsWithoutProgram[1]36if err := checkServicesDocSection(fileToCheck, doc); err != nil {37log.Printf("Check failed: %s\n", err)38log.Printf("Try updating %s with the services section produced by `%s generate`\n", fileToCheck, programName)39os.Exit(1)40}41log.Println("Check successful!")42default:43log.Printf("Unknown command: %s\n", argsWithoutProgram[0])44os.Exit(1)45}46}4748func checkServicesDocSection(path string, expectedDoc string) error {49contents, err := os.ReadFile(path)50if err != nil {51return fmt.Errorf("failed to read file to check: %w", err)52}53if !strings.Contains(string(contents), strings.TrimRight(expectedDoc, "\n")) {54return fmt.Errorf("doc has no services section, or is out of date")55}56return nil57}5859func generateServicesDocSection() string {60var sb strings.Builder61for _, supportedSvc := range yaceConf.SupportedServices {62sb.WriteString(63fmt.Sprintf("- Namespace: `%s` or Alias: `%s`\n", supportedSvc.Namespace, supportedSvc.Alias),64)65}66doc := strings.Replace(docTemplate, servicesListReplacement, sb.String(), 1)67return doc68}697071