mdtogo: support for alternate license headers

This commit is contained in:
Phillip Wittrock
2019-11-25 20:32:27 -08:00
parent 7fe518b0c6
commit 23f9f819eb
3 changed files with 31 additions and 6 deletions

View File

@@ -12,7 +12,7 @@ linters:
- bodyclose
- deadcode
- depguard
- dogsled
# - dogsled
- dupl
# - errcheck
# - funlen

View File

@@ -1,8 +1,8 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
//go:generate $GOBIN/mdtogo docs/api-conventions cmddocs/api --full=true
//go:generate $GOBIN/mdtogo docs/commands cmddocs/commands
//go:generate $GOBIN/mdtogo docs/api-conventions cmddocs/api --full=true --license=none
//go:generate $GOBIN/mdtogo docs/commands cmddocs/commands --license=none
package main
import (

View File

@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// Package main generates cobra.Command go variables containing documentation read from .md files.
// Usage: mdtogo SOURCE_MD_DIR/ DEST_GO_DIR/ [--full=true]
// Usage: mdtogo SOURCE_MD_DIR/ DEST_GO_DIR/ [--full=true] [--license=license.txt|none]
//
// The command will create a docs.go file under DEST_GO_DIR/ containing string variables to be
// used by cobra commands for documentation.The variable names are generated from the SOURCE_MD_DIR/
@@ -28,6 +28,13 @@
// ## cmd
//
// All sections will be parsed into a Long string.
//
// Flags:
// --full=true
// Create a Long variable from the full .md files, rather than separate sections.
// --license
// Controls the license header added to the files. Specify a path to a license file,
// or "none" to skip adding a license.
package main
import (
@@ -42,12 +49,16 @@ import (
)
var full bool
var licenseFile string
func main() {
for _, a := range os.Args {
if a == "--full=true" {
full = true
}
if strings.HasPrefix(a, "--license=") {
licenseFile = strings.ReplaceAll(a, "--license=", "")
}
}
if len(os.Args) < 3 {
@@ -77,9 +88,23 @@ func main() {
docs = append(docs, parse(f.Name(), string(b)))
}
out := []string{`// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
var license string
if licenseFile == "" {
license = `// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0`
} else if licenseFile == "none" {
// no license -- maybe added by another tool
} else {
b, err := ioutil.ReadFile(licenseFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
license = string(b)
}
out := []string{license, `
// Code generated by "mdtogo"; DO NOT EDIT.
package ` + filepath.Base(dest) + "\n"}