Skip to content

Commit f035f88

Browse files
committedFeb 21, 2025·
Move version to package, add version to rwp command
1 parent 0929180 commit f035f88

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed
 

‎cmd/rwp/cmd/root.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package cmd
33
import (
44
"os"
55

6+
"github.com/readium/go-toolkit/pkg/util/version"
67
"github.com/spf13/cobra"
78
)
89

910
// rootCmd represents the base command when called without any subcommands
1011
var rootCmd = &cobra.Command{
11-
Use: "rwp",
12-
Short: "Utilities for Readium Web Publications",
12+
Use: "rwp",
13+
Short: "Utilities for Readium Web Publications",
14+
Version: version.Version,
1315
}
1416

1517
// Execute adds all child commands to the root command and sets flags appropriately.

‎pkg/manifest/metadata.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package manifest
22

33
import (
44
"encoding/json"
5-
"runtime/debug"
65
"strings"
76
"time"
87

98
"github.com/go-viper/mapstructure/v2"
109
"github.com/pkg/errors"
1110
"github.com/readium/go-toolkit/pkg/internal/util"
11+
"github.com/readium/go-toolkit/pkg/util/version"
1212
)
1313

1414
// TODO replace with generic
@@ -456,8 +456,6 @@ func (m *Metadata) UnmarshalJSON(b []byte) error {
456456
// If you really don't want the version info in your manifest, you can blank this value.
457457
var ToolkitVersionKey = "https://github.com/readium/go-toolkit/releases"
458458

459-
const toolkitRepo = "github.com/readium/go-toolkit"
460-
461459
func (m Metadata) MarshalJSON() ([]byte, error) {
462460
j := make(map[string]interface{})
463461
if m.OtherMetadata != nil {
@@ -467,19 +465,7 @@ func (m Metadata) MarshalJSON() ([]byte, error) {
467465
}
468466

469467
if ToolkitVersionKey != "" {
470-
if info, ok := debug.ReadBuildInfo(); ok {
471-
if info.Main.Path == toolkitRepo {
472-
// This is the toolkit itself
473-
j[ToolkitVersionKey] = info.Main.Version
474-
} else {
475-
// This is a module that uses the toolkit
476-
for _, dep := range info.Deps {
477-
if dep.Path == toolkitRepo {
478-
j[ToolkitVersionKey] = dep.Version
479-
}
480-
}
481-
}
482-
}
468+
j[ToolkitVersionKey] = version.Version
483469
}
484470

485471
if m.Presentation != nil {

‎pkg/util/version/version.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package version
2+
3+
import "runtime/debug"
4+
5+
const toolkitRepo = "github.com/readium/go-toolkit"
6+
7+
var Version = "unknown"
8+
9+
func init() {
10+
if info, ok := debug.ReadBuildInfo(); ok {
11+
if info.Main.Path == toolkitRepo && info.Main.Version != "(devel)" {
12+
// This is the toolkit itself
13+
Version = info.Main.Version
14+
} else {
15+
// This is a module that uses the toolkit
16+
for _, dep := range info.Deps {
17+
if dep.Path == toolkitRepo {
18+
Version = dep.Version
19+
break
20+
}
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.