From 0591549a4725b5528844cb5fcbd5a61c43295579 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Fri, 4 Oct 2024 14:32:44 +0200 Subject: [PATCH 1/3] use golang.org/x/mod in new command line tool --- cmd/gomarkdoc/README.md | 2 +- cmd/gomarkdoc/command.go | 50 ++++++++++++++++++++++++++++++++++++---- go.mod | 1 + go.sum | 2 ++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/cmd/gomarkdoc/README.md b/cmd/gomarkdoc/README.md index ab0d964..1bff6d6 100644 --- a/cmd/gomarkdoc/README.md +++ b/cmd/gomarkdoc/README.md @@ -15,7 +15,7 @@ See https://github.com/Weborama/gomarkdoc for full documentation of this tool. - [type PackageSpec](<#type-packagespec>) -## type [PackageSpec]() +## type [PackageSpec]() PackageSpec defines the data available to the \-\-output option's template. Information is recomputed for each package generated. diff --git a/cmd/gomarkdoc/command.go b/cmd/gomarkdoc/command.go index 6bb3bad..1899165 100644 --- a/cmd/gomarkdoc/command.go +++ b/cmd/gomarkdoc/command.go @@ -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" @@ -60,6 +61,7 @@ type commandOptions struct { check bool embed bool version bool + forceLocalDir bool } // Flags populated by goreleaser @@ -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")) @@ -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 } @@ -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 @@ -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 @@ -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, @@ -558,7 +600,7 @@ func getSpecs(paths ...string) []*PackageSpec { } } - return expanded + return expanded, nil } var ignoredDirs = []string{".git"} diff --git a/go.mod b/go.mod index 1d6a2bc..fac5ddb 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index e13fe71..6c0d5eb 100644 --- a/go.sum +++ b/go.sum @@ -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= From f49a412bf94ba3bd645b6673c0eb78142c2c2422 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Fri, 4 Oct 2024 15:26:55 +0200 Subject: [PATCH 2/3] add another command line tool --- cmd/gomarkdoc/command.go | 45 +++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/cmd/gomarkdoc/command.go b/cmd/gomarkdoc/command.go index 1899165..a54a68a 100644 --- a/cmd/gomarkdoc/command.go +++ b/cmd/gomarkdoc/command.go @@ -52,6 +52,7 @@ type commandOptions struct { footer string footerFile string format string + fullPackageName string tags []string templateOverrides map[string]string templateFileOverrides map[string]string @@ -237,11 +238,17 @@ func buildCommand() *cobra.Command { false, "Print the version.", ) + command.Flags().StringVar( + &opts.fullPackageName, + "full-package-name", + "", + "will be used by option force-local-dir, instead read from go.mod file", + ) 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", + "if true, will use the full-package-name to infer the .Dir", ) // We ignore the errors here because they only happen if the specified flag doesn't exist @@ -261,6 +268,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("fullPackageName", command.Flags().Lookup("full-package-name")) _ = viper.BindPFlag("forceLocalDir", command.Flags().Lookup("force-local-dir")) return command @@ -493,21 +501,34 @@ func getLocalDir(path, fullModule string) string { return currDir } -func getSpecs(opts commandOptions, paths ...string) ([]*PackageSpec, error) { - var fullModule string +func getFullPackageName(opts commandOptions) (string, error) { + var fullPackageName string if opts.forceLocalDir { - data, err := os.ReadFile("go.mod") - if err != nil { - return nil, fmt.Errorf("unable to read module file: %w", err) - } + if opts.fullPackageName != "" { + fullPackageName = opts.fullPackageName + } else { + data, err := os.ReadFile("go.mod") + if err != nil { + return "", 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) + f, err := modfile.Parse("go.mod", data, nil) + if err != nil { + return "", fmt.Errorf("unable to parse module file: %w", err) + } + + fullPackageName = f.Module.Mod.String() } + } - fullModule = f.Module.Mod.String() + return fullPackageName, nil +} + +func getSpecs(opts commandOptions, paths ...string) ([]*PackageSpec, error) { + fullPackageName, err := getFullPackageName(opts) + if err != nil { + return nil, err } var expanded []*PackageSpec @@ -523,7 +544,7 @@ func getSpecs(opts commandOptions, paths ...string) ([]*PackageSpec, error) { if isLocal { dir = path } else { - dir = getLocalDir(path, fullModule) + dir = getLocalDir(path, fullPackageName) } expanded = append(expanded, &PackageSpec{ Dir: dir, From e981d27fa184fe9b43cf240834ba632b62d12f96 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Fri, 4 Oct 2024 15:28:59 +0200 Subject: [PATCH 3/3] refactor --- cmd/gomarkdoc/command.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/cmd/gomarkdoc/command.go b/cmd/gomarkdoc/command.go index a54a68a..4446015 100644 --- a/cmd/gomarkdoc/command.go +++ b/cmd/gomarkdoc/command.go @@ -502,27 +502,25 @@ func getLocalDir(path, fullModule string) string { } func getFullPackageName(opts commandOptions) (string, error) { - var fullPackageName string + if !opts.forceLocalDir { + return "", nil + } - if opts.forceLocalDir { - if opts.fullPackageName != "" { - fullPackageName = opts.fullPackageName - } else { - data, err := os.ReadFile("go.mod") - if err != nil { - return "", fmt.Errorf("unable to read module file: %w", err) - } + if opts.fullPackageName != "" { + return opts.fullPackageName, nil + } - f, err := modfile.Parse("go.mod", data, nil) - if err != nil { - return "", fmt.Errorf("unable to parse module file: %w", err) - } + data, err := os.ReadFile("go.mod") + if err != nil { + return "", fmt.Errorf("unable to read module file: %w", err) + } - fullPackageName = f.Module.Mod.String() - } + f, err := modfile.Parse("go.mod", data, nil) + if err != nil { + return "", fmt.Errorf("unable to parse module file: %w", err) } - return fullPackageName, nil + return f.Module.Mod.String(), nil } func getSpecs(opts commandOptions, paths ...string) ([]*PackageSpec, error) {