Skip to content

Commit

Permalink
Move version to package, add version to rwp command
Browse files Browse the repository at this point in the history
  • Loading branch information
chocolatkey committed Feb 21, 2025
1 parent 0929180 commit f035f88
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
6 changes: 4 additions & 2 deletions cmd/rwp/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cmd
import (
"os"

"github.com/readium/go-toolkit/pkg/util/version"
"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "rwp",
Short: "Utilities for Readium Web Publications",
Use: "rwp",
Short: "Utilities for Readium Web Publications",
Version: version.Version,
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
18 changes: 2 additions & 16 deletions pkg/manifest/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package manifest

import (
"encoding/json"
"runtime/debug"
"strings"
"time"

"github.com/go-viper/mapstructure/v2"
"github.com/pkg/errors"
"github.com/readium/go-toolkit/pkg/internal/util"
"github.com/readium/go-toolkit/pkg/util/version"
)

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

const toolkitRepo = "github.com/readium/go-toolkit"

func (m Metadata) MarshalJSON() ([]byte, error) {
j := make(map[string]interface{})
if m.OtherMetadata != nil {
Expand All @@ -467,19 +465,7 @@ func (m Metadata) MarshalJSON() ([]byte, error) {
}

if ToolkitVersionKey != "" {
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Path == toolkitRepo {
// This is the toolkit itself
j[ToolkitVersionKey] = info.Main.Version
} else {
// This is a module that uses the toolkit
for _, dep := range info.Deps {
if dep.Path == toolkitRepo {
j[ToolkitVersionKey] = dep.Version
}
}
}
}
j[ToolkitVersionKey] = version.Version
}

if m.Presentation != nil {
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package version

import "runtime/debug"

const toolkitRepo = "github.com/readium/go-toolkit"

var Version = "unknown"

func init() {
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Path == toolkitRepo && info.Main.Version != "(devel)" {
// This is the toolkit itself
Version = info.Main.Version
} else {
// This is a module that uses the toolkit
for _, dep := range info.Deps {
if dep.Path == toolkitRepo {
Version = dep.Version
break
}
}
}
}
}

0 comments on commit f035f88

Please sign in to comment.