Skip to content

Commit

Permalink
use golang.org/x/mod in new command line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Oct 4, 2024
1 parent c78cd87 commit 0591549
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
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/Weborama/gomarkdoc for full documentation of this tool.
- [type PackageSpec](<#type-packagespec>)


## type [PackageSpec](<https://github.com/Weborama/gomarkdoc/blob/master/cmd/gomarkdoc/command.go#L30-L44>)
## type [PackageSpec](<https://github.com/Weborama/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
50 changes: 46 additions & 4 deletions cmd/gomarkdoc/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/mod/modfile"

"github.com/Weborama/gomarkdoc"
"github.com/Weborama/gomarkdoc/format"
Expand Down Expand Up @@ -60,6 +61,7 @@ type commandOptions struct {
check bool
embed bool
version bool
forceLocalDir bool
}

// Flags populated by goreleaser
Expand Down Expand Up @@ -235,6 +237,12 @@ func buildCommand() *cobra.Command {
false,
"Print the version.",
)
command.Flags().BoolVar(
&opts.forceLocalDir,
"force-local-dir",
false,
"If true, will read the 'go.mod' and use the package name to infer the .Dir",
)

// We ignore the errors here because they only happen if the specified flag doesn't exist
_ = viper.BindPFlag("includeUnexported", command.Flags().Lookup("include-unexported"))
Expand All @@ -253,6 +261,7 @@ func buildCommand() *cobra.Command {
_ = viper.BindPFlag("repository.defaultBranch", command.Flags().Lookup("repository.default-branch"))
_ = viper.BindPFlag("repository.path", command.Flags().Lookup("repository.path"))
_ = viper.BindPFlag("forceRelativeURLs", command.Flags().Lookup("force-relative-urls"))
_ = viper.BindPFlag("forceLocalDir", command.Flags().Lookup("force-local-dir"))

return command
}
Expand Down Expand Up @@ -301,7 +310,10 @@ func runCommand(paths []string, opts commandOptions) error {
return fmt.Errorf("gomarkdoc: invalid output template: %w", err)
}

specs := getSpecs(paths...)
specs, err := getSpecs(opts, paths...)
if err != nil {
return err
}

if err := resolveOutput(specs, outputTmpl); err != nil {
return err
Expand Down Expand Up @@ -467,7 +479,37 @@ func getBuildPackage(path string, tags []string) (*build.Package, error) {
return pkg, nil
}

func getSpecs(paths ...string) []*PackageSpec {
func getLocalDir(path, fullModule string) string {
const currDir = `.`

if fullModule == "" {
return currDir
}

if subpath, ok := strings.CutPrefix(path, fullModule); ok {
return currDir + subpath
}

return currDir
}

func getSpecs(opts commandOptions, paths ...string) ([]*PackageSpec, error) {
var fullModule string

if opts.forceLocalDir {
data, err := os.ReadFile("go.mod")
if err != nil {
return nil, fmt.Errorf("unable to read module file: %w", err)
}

f, err := modfile.Parse("go.mod", data, nil)
if err != nil {
return nil, fmt.Errorf("unable to parse module file: %w", err)
}

fullModule = f.Module.Mod.String()
}

var expanded []*PackageSpec
for _, path := range paths {
// Ensure that the path we're working with is normalized for the OS
Expand All @@ -481,7 +523,7 @@ func getSpecs(paths ...string) []*PackageSpec {
if isLocal {
dir = path
} else {
dir = "."
dir = getLocalDir(path, fullModule)
}
expanded = append(expanded, &PackageSpec{
Dir: dir,
Expand Down Expand Up @@ -558,7 +600,7 @@ func getSpecs(paths ...string) []*PackageSpec {
}
}

return expanded
return expanded, nil
}

var ignoredDirs = []string{".git"}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/mod v0.21.0
mvdan.cc/xurls/v2 v2.5.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down

0 comments on commit 0591549

Please sign in to comment.