Skip to content

Commit

Permalink
chore: move versioning outside the main package (celestiaorg#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs authored Jul 14, 2023
1 parent 11dced4 commit 05e00e8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 37 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SHELL=/usr/bin/env bash
PROJECTNAME=$(shell basename "$(PWD)")
LDFLAGS=-ldflags="-X 'main.buildTime=$(shell date)' -X 'main.lastCommit=$(shell git rev-parse HEAD)' -X 'main.semanticVersion=$(shell git describe --tags --dirty=-dev 2>/dev/null || git rev-parse --abbrev-ref HEAD)'"
versioningPath := "github.com/celestiaorg/celestia-node/nodebuilder/node"
LDFLAGS=-ldflags="-X '$(versioningPath).buildTime=$(shell date)' -X '$(versioningPath).lastCommit=$(shell git rev-parse HEAD)' -X '$(versioningPath).semanticVersion=$(shell git describe --tags --dirty=-dev 2>/dev/null || git rev-parse --abbrev-ref HEAD)'"
ifeq (${PREFIX},)
PREFIX := /usr/local
endif
Expand Down
6 changes: 0 additions & 6 deletions cmd/celestia/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ func persistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) err
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)
ctx = cmdnode.WithNodeBuildInfo(ctx, &node.BuildInfo{
LastCommit: lastCommit,
SemanticVersion: semanticVersion,
SystemVersion: systemVersion,
GolangVersion: golangVersion,
})

// loads existing config into the environment
ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
Expand Down
21 changes: 7 additions & 14 deletions cmd/celestia/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ package main

import (
"fmt"
"runtime"

"github.com/spf13/cobra"
)

var (
buildTime string
lastCommit string
semanticVersion string

systemVersion = fmt.Sprintf("%s/%s", runtime.GOARCH, runtime.GOOS)
golangVersion = runtime.Version()
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

var versionCmd = &cobra.Command{
Expand All @@ -24,9 +16,10 @@ var versionCmd = &cobra.Command{
}

func printBuildInfo(_ *cobra.Command, _ []string) {
fmt.Printf("Semantic version: %s\n", semanticVersion)
fmt.Printf("Commit: %s\n", lastCommit)
fmt.Printf("Build Date: %s\n", buildTime)
fmt.Printf("System version: %s\n", systemVersion)
fmt.Printf("Golang version: %s\n", golangVersion)
buildInfo := node.GetBuildInfo()
fmt.Printf("Semantic version: %s\n", buildInfo.SemanticVersion)
fmt.Printf("Commit: %s\n", buildInfo.LastCommit)
fmt.Printf("Build Date: %s\n", buildInfo.BuildTime)
fmt.Printf("System version: %s\n", buildInfo.SystemVersion)
fmt.Printf("Golang version: %s\n", buildInfo.GolangVersion)
}
11 changes: 0 additions & 11 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ func NodeConfig(ctx context.Context) nodebuilder.Config {
return cfg
}

// NodeInfo reads the node build inforamtion from the context.
func NodeInfo(ctx context.Context) node.BuildInfo {
return ctx.Value(buildInfo{}).(node.BuildInfo)
}

// WithNodeType sets the node type in the given context.
func WithNodeType(ctx context.Context, tp node.Type) context.Context {
return context.WithValue(ctx, nodeTypeKey{}, tp)
Expand Down Expand Up @@ -78,16 +73,10 @@ func WithNodeConfig(ctx context.Context, config *nodebuilder.Config) context.Con
return context.WithValue(ctx, configKey{}, *config)
}

// WithNodeConfig sets the node config build information.
func WithNodeBuildInfo(ctx context.Context, info *node.BuildInfo) context.Context {
return context.WithValue(ctx, buildInfo{}, *info)
}

type (
optionsKey struct{}
configKey struct{}
storePathKey struct{}
nodeTypeKey struct{}
networkKey struct{}
buildInfo struct{}
)
3 changes: 2 additions & 1 deletion cmd/flags_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/celestiaorg/celestia-node/logs"
"github.com/celestiaorg/celestia-node/nodebuilder"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
modp2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

Expand Down Expand Up @@ -264,7 +265,7 @@ func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
opts = append(opts, otlpmetrichttp.WithInsecure())
}

ctx = WithNodeOptions(ctx, nodebuilder.WithMetrics(opts, NodeType(ctx), NodeInfo(ctx)))
ctx = WithNodeOptions(ctx, nodebuilder.WithMetrics(opts, NodeType(ctx), node.GetBuildInfo()))
}

ok, err = cmd.Flags().GetBool(p2pMetrics)
Expand Down
28 changes: 27 additions & 1 deletion nodebuilder/node/buildInfo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
package node

// BuildInfo stores all necessary information for the current build.
import (
"fmt"
"runtime"
)

var (
buildTime string
lastCommit string
semanticVersion string

systemVersion = fmt.Sprintf("%s/%s", runtime.GOARCH, runtime.GOOS)
golangVersion = runtime.Version()
)

// BuildInfo represents all necessary information about current build.
type BuildInfo struct {
BuildTime string
LastCommit string
SemanticVersion string
SystemVersion string
GolangVersion string
}

// GetBuildInfo returns information about current build.
func GetBuildInfo() *BuildInfo {
return &BuildInfo{
buildTime,
lastCommit,
semanticVersion,
systemVersion,
golangVersion,
}
}
2 changes: 1 addition & 1 deletion nodebuilder/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestLifecycle_WithMetrics(t *testing.T) {
otlpmetrichttp.WithInsecure(),
},
tt.tp,
node.BuildInfo{},
&node.BuildInfo{},
),
)
require.NotNil(t, node)
Expand Down
4 changes: 2 additions & 2 deletions nodebuilder/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func WithPyroscope(endpoint string, nodeType node.Type) fx.Option {
}

// WithMetrics enables metrics exporting for the node.
func WithMetrics(metricOpts []otlpmetrichttp.Option, nodeType node.Type, buildInfo node.BuildInfo) fx.Option {
func WithMetrics(metricOpts []otlpmetrichttp.Option, nodeType node.Type, buildInfo *node.BuildInfo) fx.Option {
baseComponents := fx.Options(
fx.Supply(metricOpts),
fx.Supply(buildInfo),
Expand Down Expand Up @@ -111,7 +111,7 @@ func initializeMetrics(
lc fx.Lifecycle,
peerID peer.ID,
nodeType node.Type,
buildInfo node.BuildInfo,
buildInfo *node.BuildInfo,
opts []otlpmetrichttp.Option,
) error {
exp, err := otlpmetrichttp.New(ctx, opts...)
Expand Down

0 comments on commit 05e00e8

Please sign in to comment.