...

Source file src/go.mongodb.org/mongo-driver/cmd/parse-api-report/main.go

Documentation: go.mongodb.org/mongo-driver/cmd/parse-api-report

     1  // Copyright (C) MongoDB, Inc. 2023-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package main
     8  
     9  import (
    10  	"bufio"
    11  	"fmt"
    12  	"log"
    13  	"os"
    14  	"strings"
    15  )
    16  
    17  func main() {
    18  	var line string
    19  	var suppress bool
    20  	var foundChange = false
    21  	var foundSummary = false
    22  
    23  	// open file to read
    24  	fRead, err := os.Open("api-report.txt")
    25  	if err != nil {
    26  		log.Fatal(err)
    27  	}
    28  	// remember to close the file at the end of the program
    29  	defer fRead.Close()
    30  
    31  	// open file to write
    32  	fWrite, err := os.Create("api-report.md")
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  	// remember to close the file at the end of the program
    37  	defer fWrite.Close()
    38  
    39  	fmt.Fprint(fWrite, "## API Change Report\n")
    40  
    41  	// read the file line by line using scanner
    42  	scanner := bufio.NewScanner(fRead)
    43  
    44  	for scanner.Scan() {
    45  		// do something with a line
    46  		line = scanner.Text()
    47  		if strings.Index(line, "## ") == 0 {
    48  			line = "##" + line
    49  		}
    50  
    51  		if strings.Contains(line, "/mongo/integration/") {
    52  			suppress = true
    53  		}
    54  		if strings.Index(line, "# summary") == 0 {
    55  			suppress = true
    56  			foundSummary = true
    57  		}
    58  
    59  		if strings.Contains(line, "go.mongodb.org/mongo-driver") {
    60  			line = strings.Replace(line, "go.mongodb.org/mongo-driver", ".", -1)
    61  			line = "##" + line
    62  		}
    63  		if !suppress {
    64  			fmt.Fprintf(fWrite, "%s\n", line)
    65  			foundChange = true
    66  		}
    67  		if len(line) == 0 {
    68  			suppress = false
    69  		}
    70  	}
    71  
    72  	if !foundChange {
    73  		fmt.Fprint(fWrite, "No changes found!\n")
    74  	}
    75  
    76  	if !foundSummary {
    77  		log.Fatal("Could not parse api summary")
    78  	}
    79  
    80  	if err := scanner.Err(); err != nil {
    81  		log.Fatal(err)
    82  	}
    83  
    84  }
    85  

View as plain text