Skip to content

Commit

Permalink
use embed instead of base64 encode for defaults (#137)
Browse files Browse the repository at this point in the history
* use embed instead of base64 encode for defaults

* explain blank import
  • Loading branch information
brycahta authored Aug 25, 2021
1 parent a2fdeaf commit bac3a29
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ SUPPORTED_PLATFORMS_WINDOWS ?= "windows/amd64"
MAKEFILE_PATH = $(dir $(realpath -s $(firstword $(MAKEFILE_LIST))))
BUILD_DIR_PATH = ${MAKEFILE_PATH}/build
BINARY_NAME ?= ec2-metadata-mock
METADATA_DEFAULTS_FILE=${MAKEFILE_PATH}/pkg/config/defaults/aemm-metadata-default-values.json
ENCODED_METADATA_DEFAULTS=$(shell cat ${METADATA_DEFAULTS_FILE} | base64 | tr -d '\040\011\012\015')
DEFAULT_VALUES_VAR=github.com/aws/amazon-ec2-metadata-mock/pkg/config/defaults.encodedDefaultValues
ROOT_VERSION_VAR=github.com/aws/amazon-ec2-metadata-mock/pkg/cmd/root.version

$(shell mkdir -p ${BUILD_DIR_PATH} && touch ${BUILD_DIR_PATH}/_go.mod)
Expand Down Expand Up @@ -50,7 +47,7 @@ clean:

compile:
@echo ${MAKEFILE_PATH}
go build -a -tags aemm${GOOS} -ldflags '-X "${DEFAULT_VALUES_VAR}=${ENCODED_METADATA_DEFAULTS}" -X "${ROOT_VERSION_VAR}=${VERSION}"' -o ${BUILD_DIR_PATH}/${BINARY_NAME} ${MAKEFILE_PATH}/cmd/amazon-ec2-metadata-mock.go
go build -a -tags aemm${GOOS} -ldflags '-X "${ROOT_VERSION_VAR}=${VERSION}"' -o ${BUILD_DIR_PATH}/${BINARY_NAME} ${MAKEFILE_PATH}/cmd/amazon-ec2-metadata-mock.go

validate-json:
${MAKEFILE_PATH}/scripts/validators/json-validator
Expand Down
13 changes: 5 additions & 8 deletions pkg/config/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
package defaults

import (
"encoding/base64"
// Blank import else compiler complains it's unused
_ "embed"
)

// DO NOT EDIT: encodedDefaultValues is populated by the Makefile.
var (
encodedDefaultValues = "{}"
decodedDefaultValues []byte
)
//go:embed aemm-metadata-default-values.json
var defaultValues []byte

// GetDefaultValues returns default metadata values populated via aemm-metadata-default-values.json
func GetDefaultValues() []byte {
decodedDefaultValues, _ := base64.StdEncoding.DecodeString(encodedDefaultValues)
return decodedDefaultValues
return defaultValues
}

0 comments on commit bac3a29

Please sign in to comment.