Skip to content

Commit 63ab7db

Browse files
authored
feat(cmd): add support for build tags (#52)
1 parent 33854af commit 63ab7db

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Flags:
4949
--repository.default-branch string Manual override for the git repository URL used in place of automatic detection.
5050
--repository.path string Manual override for the path from the root of the git repository used in place of automatic detection.
5151
--repository.url string Manual override for the git repository URL used in place of automatic detection.
52+
--tags strings Set of build tags to apply when choosing which files to include for documentation generation.
5253
-t, --template stringToString Custom template string to use for the provided template name instead of the default template. (default [])
5354
--template-file stringToString Custom template file to use for the provided template name instead of the default template. (default [])
5455
-v, --verbose count Log additional output from the execution of the command. Can be chained for additional verbosity.
@@ -131,6 +132,12 @@ As with the godoc tool itself\, only exported symbols will be shown in documenta
131132
gomarkdoc -u -o README.md .
132133
```
133134

135+
If you would like to include files that are part of a build tag\, you can specify build tags with the \-\-tags flag\. Tags are also supported through GOFLAGS\, though command line and configuration file definitions override tags specified through GOFLAGS\.
136+
137+
```
138+
gomarkdoc --tags sometag .
139+
```
140+
134141
You can also run gomarkdoc in a verification mode with the \-\-check/\-c flag\. This is particularly useful for continuous integration when you want to make sure that a commit correctly updated the generated documentation\. This flag is only supported when the \-\-output/\-o flag is specified\, as the file provided there is what the tool is checking:
135142

136143
```

cmd/gomarkdoc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See https://github.com/princjef/gomarkdoc for full documentation of this tool\.
1515
- [type PackageSpec](<#type-packagespec>)
1616

1717

18-
## type [PackageSpec](<https://github.com/princjef/gomarkdoc/blob/master/cmd/gomarkdoc/command.go#L30-L44>)
18+
## type [PackageSpec](<https://github.com/princjef/gomarkdoc/blob/master/cmd/gomarkdoc/command.go#L31-L45>)
1919

2020
PackageSpec defines the data available to the \-\-output option's template\. Information is recomputed for each package generated\.
2121

cmd/gomarkdoc/command.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"container/list"
66
"errors"
7+
"flag"
78
"fmt"
89
"go/build"
910
"hash/fnv"
@@ -51,6 +52,7 @@ type commandOptions struct {
5152
footer string
5253
footerFile string
5354
format string
55+
tags []string
5456
templateOverrides map[string]string
5557
templateFileOverrides map[string]string
5658
verbosity int
@@ -90,6 +92,7 @@ func buildCommand() *cobra.Command {
9092
opts.headerFile = viper.GetString("headerFile")
9193
opts.footer = viper.GetString("footer")
9294
opts.footerFile = viper.GetString("footerFile")
95+
opts.tags = viper.GetStringSlice("tags")
9396
opts.repository.Remote = viper.GetString("repository.url")
9497
opts.repository.DefaultBranch = viper.GetString("repository.defaultBranch")
9598
opts.repository.PathFromRoot = viper.GetString("repository.path")
@@ -180,6 +183,12 @@ func buildCommand() *cobra.Command {
180183
"",
181184
"File containing additional content to inject at the end of each output file.",
182185
)
186+
command.Flags().StringSliceVar(
187+
&opts.tags,
188+
"tags",
189+
defaultTags(),
190+
"Set of build tags to apply when choosing which files to include for documentation generation.",
191+
)
183192
command.Flags().CountVarP(
184193
&opts.verbosity,
185194
"verbose",
@@ -222,13 +231,34 @@ func buildCommand() *cobra.Command {
222231
_ = viper.BindPFlag("headerFile", command.Flags().Lookup("header-file"))
223232
_ = viper.BindPFlag("footer", command.Flags().Lookup("footer"))
224233
_ = viper.BindPFlag("footerFile", command.Flags().Lookup("footer-file"))
234+
_ = viper.BindPFlag("tags", command.Flags().Lookup("tags"))
225235
_ = viper.BindPFlag("repository.url", command.Flags().Lookup("repository.url"))
226236
_ = viper.BindPFlag("repository.defaultBranch", command.Flags().Lookup("repository.default-branch"))
227237
_ = viper.BindPFlag("repository.path", command.Flags().Lookup("repository.path"))
228238

229239
return command
230240
}
231241

242+
func defaultTags() []string {
243+
f, ok := os.LookupEnv("GOFLAGS")
244+
if !ok {
245+
return nil
246+
}
247+
248+
fs := flag.NewFlagSet("goflags", flag.ContinueOnError)
249+
tags := fs.String("tags", "", "")
250+
251+
if err := fs.Parse(strings.Fields(f)); err != nil {
252+
return nil
253+
}
254+
255+
if tags == nil {
256+
return nil
257+
}
258+
259+
return strings.Split(*tags, ",")
260+
}
261+
232262
func buildConfig(configFile string) {
233263
if configFile != "" {
234264
viper.SetConfigFile(configFile)
@@ -364,7 +394,7 @@ func loadPackages(specs []*PackageSpec, opts commandOptions) error {
364394
for _, spec := range specs {
365395
log := logger.New(getLogLevel(opts.verbosity), logger.WithField("dir", spec.Dir))
366396

367-
buildPkg, err := getBuildPackage(spec.ImportPath)
397+
buildPkg, err := getBuildPackage(spec.ImportPath, opts.tags)
368398
if err != nil {
369399
log.Debugf("unable to load package in directory: %s", err)
370400
// We don't care if a wildcard path produces nothing
@@ -493,9 +523,12 @@ func checkFile(b *bytes.Buffer, path string) error {
493523
return nil
494524
}
495525

496-
func getBuildPackage(path string) (*build.Package, error) {
526+
func getBuildPackage(path string, tags []string) (*build.Package, error) {
527+
ctx := build.Default
528+
ctx.BuildTags = tags
529+
497530
if isLocalPath(path) {
498-
pkg, err := build.ImportDir(path, build.ImportComment)
531+
pkg, err := ctx.ImportDir(path, build.ImportComment)
499532
if err != nil {
500533
return nil, fmt.Errorf("gomarkdoc: invalid package in directory: %s", path)
501534
}
@@ -508,7 +541,7 @@ func getBuildPackage(path string) (*build.Package, error) {
508541
return nil, err
509542
}
510543

511-
pkg, err := build.Import(path, wd, build.ImportComment)
544+
pkg, err := ctx.Import(path, wd, build.ImportComment)
512545
if err != nil {
513546
return nil, fmt.Errorf("gomarkdoc: invalid package at import path: %s", path)
514547
}

doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// --repository.default-branch string Manual override for the git repository URL used in place of automatic detection.
3939
// --repository.path string Manual override for the path from the root of the git repository used in place of automatic detection.
4040
// --repository.url string Manual override for the git repository URL used in place of automatic detection.
41+
// --tags strings Set of build tags to apply when choosing which files to include for documentation generation.
4142
// -t, --template stringToString Custom template string to use for the provided template name instead of the default template. (default [])
4243
// --template-file stringToString Custom template file to use for the provided template name instead of the default template. (default [])
4344
// -v, --verbose count Log additional output from the execution of the command. Can be chained for additional verbosity.
@@ -137,6 +138,13 @@
137138
//
138139
// gomarkdoc -u -o README.md .
139140
//
141+
// If you would like to include files that are part of a build tag, you can
142+
// specify build tags with the --tags flag. Tags are also supported through
143+
// GOFLAGS, though command line and configuration file definitions override tags
144+
// specified through GOFLAGS.
145+
//
146+
// gomarkdoc --tags sometag .
147+
//
140148
// You can also run gomarkdoc in a verification mode with the --check/-c flag.
141149
// This is particularly useful for continuous integration when you want to make
142150
// sure that a commit correctly updated the generated documentation. This flag

0 commit comments

Comments
 (0)