Skip to content

Commit

Permalink
Set default version header as 0.0.0-DEV (#202)
Browse files Browse the repository at this point in the history
* Set default version header as 0.0.0-DEV

* .
  • Loading branch information
feedmeapples authored and samanbarghi committed Apr 19, 2023
1 parent 8188c1f commit 34ece66
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 211 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ PINNED_DEPENDENCIES := \

build:
@printf $(COLOR) "Building Temporal CLI with OS: $(GOOS), ARCH: $(GOARCH)..."
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/temporalio/cli/headers.Version=0.0.0" ./cmd/temporal
CGO_ENABLED=0 go build -ldflags "-s -w" ./cmd/temporal
@printf $(COLOR) "Building docs generation tool: $(GOOS), ARCH: $(GOARCH)..."
CGO_ENABLED=0 go build -o temporal-doc-gen ./cmd/docgen

Expand Down
83 changes: 2 additions & 81 deletions headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
)

// Set by GoReleaser using ldflags
var Version = "DEV"
var Version = "0.0.0-DEV"

const (
ClientNameCLI = "temporal-cli"
Expand All @@ -26,18 +26,6 @@ const (
)

var (
// propagateHeaders are the headers to propagate from the frontend to other services.
propagateHeaders = []string{
ClientNameHeaderName,
ClientVersionHeaderName,
SupportedServerVersionsHeaderName,
SupportedFeaturesHeaderName,
}

internalVersionHeaders = metadata.New(map[string]string{
SupportedServerVersionsHeaderName: SupportedServerVersions,
})

cliVersionHeaders = metadata.New(map[string]string{
ClientNameHeaderName: ClientNameCLI,
ClientVersionHeaderName: Version,
Expand All @@ -48,81 +36,14 @@ var (
)

func Init() {
if Version == "DEV" {
if Version == "0.0.0-DEV" {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
Version = info.Main.Version
}
}
}

// GetValues returns header values for passed header names.
// It always returns slice of the same size as number of passed header names.
func GetValues(ctx context.Context, headerNames ...string) []string {
headerValues := make([]string, len(headerNames))

if md, ok := metadata.FromIncomingContext(ctx); ok {
for i, headerName := range headerNames {
headerValues[i] = getSingleHeaderValue(md, headerName)
}
}

return headerValues
}

// Propagate propagates version headers from incoming context to outgoing context.
// It copies all version headers to outgoing context only if they are exist in incoming context
// and doesn't exist in outgoing context already.
func Propagate(ctx context.Context) context.Context {
if mdIncoming, ok := metadata.FromIncomingContext(ctx); ok {
var headersToAppend []string
mdOutgoing, mdOutgoingExist := metadata.FromOutgoingContext(ctx)
for _, headerName := range propagateHeaders {
if incomingValue := mdIncoming.Get(headerName); len(incomingValue) > 0 {
if mdOutgoingExist {
if outgoingValue := mdOutgoing.Get(headerName); len(outgoingValue) > 0 {
continue
}
}
headersToAppend = append(headersToAppend, headerName, incomingValue[0])
}
}
if headersToAppend != nil {
if mdOutgoingExist {
ctx = metadata.AppendToOutgoingContext(ctx, headersToAppend...)
} else {
ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs(headersToAppend...))
}
}
}
return ctx
}

// SetVersions sets headers for internal communications.
func SetVersions(ctx context.Context) context.Context {
return metadata.NewOutgoingContext(ctx, internalVersionHeaders)
}

// SetCLIVersions sets headers for CLI requests.
func SetCLIVersions(ctx context.Context) context.Context {
return metadata.NewOutgoingContext(ctx, cliVersionHeaders)
}

// SetVersionsForTests sets headers as they would be received from the client.
// Must be used in tests only.
func SetVersionsForTests(ctx context.Context, clientVersion, clientName, supportedServerVersions, supportedFeatures string) context.Context {
return metadata.NewIncomingContext(ctx, metadata.New(map[string]string{
ClientNameHeaderName: clientName,
ClientVersionHeaderName: clientVersion,
SupportedServerVersionsHeaderName: supportedServerVersions,
SupportedFeaturesHeaderName: supportedFeatures,
}))
}

func getSingleHeaderValue(md metadata.MD, headerName string) string {
values := md.Get(headerName)
if len(values) == 0 {
return ""
}

return values[0]
}
129 changes: 0 additions & 129 deletions headers/headers_test.go

This file was deleted.

0 comments on commit 34ece66

Please sign in to comment.