Skip to content

Commit

Permalink
feat(cmd): add support for build tags (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
princjef authored Nov 15, 2021
1 parent 33854af commit 63ab7db
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Flags:
--repository.default-branch string Manual override for the git repository URL used in place of automatic detection.
--repository.path string Manual override for the path from the root of the git repository used in place of automatic detection.
--repository.url string Manual override for the git repository URL used in place of automatic detection.
--tags strings Set of build tags to apply when choosing which files to include for documentation generation.
-t, --template stringToString Custom template string to use for the provided template name instead of the default template. (default [])
--template-file stringToString Custom template file to use for the provided template name instead of the default template. (default [])
-v, --verbose count Log additional output from the execution of the command. Can be chained for additional verbosity.
Expand Down Expand Up @@ -131,6 +132,12 @@ As with the godoc tool itself\, only exported symbols will be shown in documenta
gomarkdoc -u -o README.md .
```

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\.

```
gomarkdoc --tags sometag .
```

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:

```
Expand Down
2 changes: 1 addition & 1 deletion cmd/gomarkdoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ See https://github.com/princjef/gomarkdoc for full documentation of this tool\.
- [type PackageSpec](<#type-packagespec>)


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

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

Expand Down
41 changes: 37 additions & 4 deletions cmd/gomarkdoc/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"container/list"
"errors"
"flag"
"fmt"
"go/build"
"hash/fnv"
Expand Down Expand Up @@ -51,6 +52,7 @@ type commandOptions struct {
footer string
footerFile string
format string
tags []string
templateOverrides map[string]string
templateFileOverrides map[string]string
verbosity int
Expand Down Expand Up @@ -90,6 +92,7 @@ func buildCommand() *cobra.Command {
opts.headerFile = viper.GetString("headerFile")
opts.footer = viper.GetString("footer")
opts.footerFile = viper.GetString("footerFile")
opts.tags = viper.GetStringSlice("tags")
opts.repository.Remote = viper.GetString("repository.url")
opts.repository.DefaultBranch = viper.GetString("repository.defaultBranch")
opts.repository.PathFromRoot = viper.GetString("repository.path")
Expand Down Expand Up @@ -180,6 +183,12 @@ func buildCommand() *cobra.Command {
"",
"File containing additional content to inject at the end of each output file.",
)
command.Flags().StringSliceVar(
&opts.tags,
"tags",
defaultTags(),
"Set of build tags to apply when choosing which files to include for documentation generation.",
)
command.Flags().CountVarP(
&opts.verbosity,
"verbose",
Expand Down Expand Up @@ -222,13 +231,34 @@ func buildCommand() *cobra.Command {
_ = viper.BindPFlag("headerFile", command.Flags().Lookup("header-file"))
_ = viper.BindPFlag("footer", command.Flags().Lookup("footer"))
_ = viper.BindPFlag("footerFile", command.Flags().Lookup("footer-file"))
_ = viper.BindPFlag("tags", command.Flags().Lookup("tags"))
_ = viper.BindPFlag("repository.url", command.Flags().Lookup("repository.url"))
_ = viper.BindPFlag("repository.defaultBranch", command.Flags().Lookup("repository.default-branch"))
_ = viper.BindPFlag("repository.path", command.Flags().Lookup("repository.path"))

return command
}

func defaultTags() []string {
f, ok := os.LookupEnv("GOFLAGS")
if !ok {
return nil
}

fs := flag.NewFlagSet("goflags", flag.ContinueOnError)
tags := fs.String("tags", "", "")

if err := fs.Parse(strings.Fields(f)); err != nil {
return nil
}

if tags == nil {
return nil
}

return strings.Split(*tags, ",")
}

func buildConfig(configFile string) {
if configFile != "" {
viper.SetConfigFile(configFile)
Expand Down Expand Up @@ -364,7 +394,7 @@ func loadPackages(specs []*PackageSpec, opts commandOptions) error {
for _, spec := range specs {
log := logger.New(getLogLevel(opts.verbosity), logger.WithField("dir", spec.Dir))

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

func getBuildPackage(path string) (*build.Package, error) {
func getBuildPackage(path string, tags []string) (*build.Package, error) {
ctx := build.Default
ctx.BuildTags = tags

if isLocalPath(path) {
pkg, err := build.ImportDir(path, build.ImportComment)
pkg, err := ctx.ImportDir(path, build.ImportComment)
if err != nil {
return nil, fmt.Errorf("gomarkdoc: invalid package in directory: %s", path)
}
Expand All @@ -508,7 +541,7 @@ func getBuildPackage(path string) (*build.Package, error) {
return nil, err
}

pkg, err := build.Import(path, wd, build.ImportComment)
pkg, err := ctx.Import(path, wd, build.ImportComment)
if err != nil {
return nil, fmt.Errorf("gomarkdoc: invalid package at import path: %s", path)
}
Expand Down
8 changes: 8 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// --repository.default-branch string Manual override for the git repository URL used in place of automatic detection.
// --repository.path string Manual override for the path from the root of the git repository used in place of automatic detection.
// --repository.url string Manual override for the git repository URL used in place of automatic detection.
// --tags strings Set of build tags to apply when choosing which files to include for documentation generation.
// -t, --template stringToString Custom template string to use for the provided template name instead of the default template. (default [])
// --template-file stringToString Custom template file to use for the provided template name instead of the default template. (default [])
// -v, --verbose count Log additional output from the execution of the command. Can be chained for additional verbosity.
Expand Down Expand Up @@ -137,6 +138,13 @@
//
// gomarkdoc -u -o README.md .
//
// 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.
//
// gomarkdoc --tags sometag .
//
// 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
Expand Down

0 comments on commit 63ab7db

Please sign in to comment.