| 
 | 1 | +// Copyright 2024 Google LLC  | 
 | 2 | +//  | 
 | 3 | +// Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 4 | +// you may not use this file except in compliance with the License.  | 
 | 5 | +// You may obtain a copy of the License at  | 
 | 6 | +//  | 
 | 7 | +//	https://www.apache.org/licenses/LICENSE-2.0  | 
 | 8 | +//  | 
 | 9 | +// Unless required by applicable law or agreed to in writing, software  | 
 | 10 | +// distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 12 | +// See the License for the specific language governing permissions and  | 
 | 13 | +// limitations under the License.  | 
 | 14 | + | 
 | 15 | +//go:build docgen  | 
 | 16 | + | 
 | 17 | +package main  | 
 | 18 | + | 
 | 19 | +import (  | 
 | 20 | +	"bytes"  | 
 | 21 | +	"errors"  | 
 | 22 | +	"fmt"  | 
 | 23 | +	"log"  | 
 | 24 | +	"os"  | 
 | 25 | +	"os/exec"  | 
 | 26 | +	"strings"  | 
 | 27 | +	"text/template"  | 
 | 28 | +)  | 
 | 29 | + | 
 | 30 | +const docTemplate = `// Copyright 2025 Google LLC  | 
 | 31 | +//  | 
 | 32 | +// Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 33 | +// you may not use this file except in compliance with the License.  | 
 | 34 | +// You may obtain a copy of the License at  | 
 | 35 | +//  | 
 | 36 | +//	https://www.apache.org/licenses/LICENSE-2.0  | 
 | 37 | +//  | 
 | 38 | +// Unless required by applicable law or agreed to in writing, software  | 
 | 39 | +// distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 40 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 41 | +// See the License for the specific language governing permissions and  | 
 | 42 | +// limitations under the License.  | 
 | 43 | +
  | 
 | 44 | +//go:generate go run -tags docgen doc_generate.go  | 
 | 45 | +
  | 
 | 46 | +/*  | 
 | 47 | +Package librarian contains the business logic for the Librarian CLI.  | 
 | 48 | +Implementation details for interacting with other systems (Git, GitHub,  | 
 | 49 | +Docker etc.) are abstracted into other packages.  | 
 | 50 | +
  | 
 | 51 | +Usage:  | 
 | 52 | +
  | 
 | 53 | +	librarian <command> [arguments]  | 
 | 54 | +
  | 
 | 55 | +The commands are:  | 
 | 56 | +{{.Commands}}  | 
 | 57 | +*/  | 
 | 58 | +package librarian  | 
 | 59 | +`  | 
 | 60 | + | 
 | 61 | +func main() {  | 
 | 62 | +	if err := run(); err != nil {  | 
 | 63 | +		log.Fatal(err)  | 
 | 64 | +	}  | 
 | 65 | +}  | 
 | 66 | + | 
 | 67 | +func run() error {  | 
 | 68 | +	if err := processFile(); err != nil {  | 
 | 69 | +		return err  | 
 | 70 | +	}  | 
 | 71 | +	cmd := exec.Command("goimports", "-w", "doc.go")  | 
 | 72 | +	if err := cmd.Run(); err != nil {  | 
 | 73 | +		log.Fatalf("goimports: %v", err)  | 
 | 74 | +	}  | 
 | 75 | +	return nil  | 
 | 76 | +}  | 
 | 77 | + | 
 | 78 | +func processFile() error {  | 
 | 79 | +	// Get the help text.  | 
 | 80 | +	cmd := exec.Command("go", "run", "../../cmd/librarian/")  | 
 | 81 | +	var out bytes.Buffer  | 
 | 82 | +	cmd.Stdout = &out  | 
 | 83 | +	cmd.Stderr = &out  | 
 | 84 | +	err := cmd.Run()  | 
 | 85 | +	if err != nil {  | 
 | 86 | +		// The command exits with status 1 if no subcommand is given, which is  | 
 | 87 | +		// the case when we are generating the help text. We can ignore the  | 
 | 88 | +		// error if there is output.  | 
 | 89 | +		if out.Len() == 0 {  | 
 | 90 | +			return fmt.Errorf("cmd.Run() failed with %s\n%s", err, out.String())  | 
 | 91 | +		}  | 
 | 92 | +	}  | 
 | 93 | +	helpText := out.Bytes()  | 
 | 94 | + | 
 | 95 | +	commands, err := extractCommands(helpText)  | 
 | 96 | + | 
 | 97 | +	docFile, err := os.Create("doc.go")  | 
 | 98 | +	if err != nil {  | 
 | 99 | +		return fmt.Errorf("could not create doc.go: %v", err)  | 
 | 100 | +	}  | 
 | 101 | +	defer docFile.Close()  | 
 | 102 | + | 
 | 103 | +	tmpl := template.Must(template.New("doc").Parse(docTemplate))  | 
 | 104 | +	if err := tmpl.Execute(docFile, struct{ Commands string }{Commands: string(commands)}); err != nil {  | 
 | 105 | +		return fmt.Errorf("could not execute template: %v", err)  | 
 | 106 | +	}  | 
 | 107 | +	return nil  | 
 | 108 | +}  | 
 | 109 | + | 
 | 110 | +func extractCommands(helpText []byte) ([]byte, error) {  | 
 | 111 | +	const (  | 
 | 112 | +		commandsHeader = "Commands:\n\n"  | 
 | 113 | +	)  | 
 | 114 | +	ss := string(helpText)  | 
 | 115 | +	start := strings.Index(ss, commandsHeader)  | 
 | 116 | +	if start == -1 {  | 
 | 117 | +		return nil, errors.New("could not find commands header")  | 
 | 118 | +	}  | 
 | 119 | +	start += len(commandsHeader)  | 
 | 120 | +	return []byte(ss[start:]), nil  | 
 | 121 | +}  | 
0 commit comments