Skip to content

Commit

Permalink
2022 upgrade (#5)
Browse files Browse the repository at this point in the history
* Update go-wrapper

* Update Dependencies

* Enhance test for future improvements

* Add Auto-Release Action
  • Loading branch information
furkilic authored Oct 27, 2022
1 parent 751be90 commit 94333cc
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 41 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Release

on:
push:
tags:
- "v*.*.*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Release
uses: softprops/action-gh-release@v1
with:
draft: true
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/furkilic/go-boot-config
go 1.14

require (
github.com/google/uuid v1.1.1
github.com/mitchellh/mapstructure v1.3.0
gopkg.in/yaml.v2 v2.2.8
github.com/google/uuid v1.3.0
github.com/mitchellh/mapstructure v1.5.0
gopkg.in/yaml.v2 v2.4.0
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
48 changes: 38 additions & 10 deletions gow
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fi
# This allows using the go-wrapper in projects that prohibit checking in binary data.
##########################################################################################

goVersionUrl="https://golang.org/dl/"
goVersionUrl="https://go.dev/dl/"
goWrapperPath="$BASE_DIR/.go/wrapper"
goWrapperProperties="$goWrapperPath/go-wrapper.properties"
goInstallPath="$goWrapperPath/go"
Expand Down Expand Up @@ -131,7 +131,9 @@ unpack_archive(){
else
tar -C "$goWrapperPath" -zxvf "$goZipPath"
fi
rm "$goVersionPath"
if [ -f "$goVersionPath" ]; then
rm "$goVersionPath"
fi
date > "$goWrapperDate"
}

Expand All @@ -151,8 +153,9 @@ get_local_go() {
fi
mkdir -p "$goTmpPath"
if [ -f "$goWrapperProperties" ]; then
while IFS="=" read key value; do
case "$key" in (distributionUrl) goUrl="$value"; break ;;
while IFS="=" read key value || [ "$key" ]; do
case "$key" in (distributionUrl) goUrl=`echo "$value" | sed 's/ *$//g'`; break ;;
(goVersion) go_version=$value; break ;;
esac
done < "$goWrapperProperties"
fi
Expand All @@ -162,6 +165,7 @@ get_local_go() {
echo "Found wget ... using wget"
fi
if [ -z "$goUrl" ]; then
if [ -z "$go_version" ]; then
if [ ! -f "$goVersionPath" ]; then
if [ -z "$GOW_USERNAME" ] || [ -z "$GOW_PASSWORD" ]; then
wget "$goVersionUrl" -O "$goVersionPath"
Expand All @@ -170,7 +174,8 @@ get_local_go() {
fi
fi
go_version=`retrieve_version "$goVersionPath"`
goUrl="$GOW_REPOURL/go$go_version.$os-$arch.$extension"
fi
goUrl="$GOW_REPOURL/go$go_version.$os-$arch.$extension"
fi
if [ "$GOW_VERBOSE" = true ]; then
echo "Downloading from: $goUrl"
Expand All @@ -186,15 +191,17 @@ get_local_go() {
echo "Found curl ... using curl"
fi
if [ -z "$goUrl" ]; then
if [ -z "$go_version" ]; then
if [ ! -f "$goVersionPath" ]; then
if [ -z "$GOW_USERNAME" ] || [ -z "$GOW_PASSWORD" ]; then
curl -o "$goVersionPath" "$goVersionUrl" -f
curl -o "$goVersionPath" "$goVersionUrl" -f -s
else
curl --user $GOW_USERNAME:$GOW_PASSWORD -o "$goVersionPath" "$goVersionUrl" -f
curl --user $GOW_USERNAME:$GOW_PASSWORD -o "$goVersionPath" "$goVersionUrl" -f -s
fi
fi
go_version=`retrieve_version "$goVersionPath"`
goUrl="$GOW_REPOURL/go$go_version.$os-$arch.$extension"
fi
goUrl="$GOW_REPOURL/go$go_version.$os-$arch.$extension"
fi
if [ "$GOW_VERBOSE" = true ]; then
echo "Downloading from: $goUrl"
Expand All @@ -219,15 +226,36 @@ if [ -z "$GOROOT" ]; then
GOROOT="$goInstallPath"
fi

# For Cygwin, switch paths to Windows format before running java
# Set GOPATH in case there is none
if [ -z "$GOPATH" ]; then
GOPATH=`$GOROOT/bin/go env GOPATH`
fi

# For Cygwin, switch paths to Windows format before running go
if $cygwin; then
[ -n "$GOROOT" ] &&
GOROOT=`cygpath --path --windows "$GOROOT"`
[ -n "$GOPATH" ] &&
GOPATH=`cygpath --path --windows "$GOPATH"`
fi

# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
GO_CMD_LINE_ARGS="$@"
export GO_CMD_LINE_ARGS

exec "$GOROOT/bin/go" "$@"
# Export Go Variables for downstream executions
export GOROOT
export GOPATH
export PATH="$GOROOT/bin:$GOPATH/bin:$PATH"

if [ "$1" = "printenv" ]; then
echo "export GOROOT=\"$GOROOT\""
echo "export GOPATH=\"$GOPATH\""
echo "export PATH=\"\$GOROOT/bin:\$GOPATH/bin:\$PATH\""
echo ''
echo '# Run this command to configure your shell:'
echo '# eval "$(./gow printenv)"'
else
exec "$GOROOT/bin/go" "$@"
fi
29 changes: 24 additions & 5 deletions gow.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ cd "%EXEC_DIR%"
:endDetectBaseDir

set GO_WRAPPER_PATH="%GO_PROJECTBASEDIR%\.go\wrapper"
set GO_WRAPPER_PROPERTIES="%GO_PROJECTBASEDIR%\.go\wrapper\go-wrapper.properties"
set GO_INSTALL_PATH="%GO_PROJECTBASEDIR%\.go\wrapper\go"
set GO_WRAPPER_PROPERTIES=%GO_PROJECTBASEDIR%\.go\wrapper\go-wrapper.properties
set GO_INSTALL_PATH=%GO_PROJECTBASEDIR%\.go\wrapper\go
set GO_TMP_PATH="%GO_PROJECTBASEDIR%\.go\wrapper\tmp"
set GO_WRAPPER_DATE="%GO_PROJECTBASEDIR%\.go\wrapper\go\go.date"
set GO_VERSION_URL="https://golang.org/dl/"
set GO_VERSION_URL="https://go.dev/dl/"
set GO_VERSION_PATH="%GO_PROJECTBASEDIR%\.go\wrapper\tmp\go.version"
set GO_ZIP_PATH="%GO_PROJECTBASEDIR%\.go\wrapper\tmp\go.zip"

Expand All @@ -91,6 +91,7 @@ set DOWNLOAD_URL=%DOWNLOAD_URL%
if not exist "%GO_WRAPPER_PROPERTIES%" goto extension
FOR /F "tokens=1,2 delims==" %%A IN (%GO_WRAPPER_PROPERTIES%) DO (
IF "%%A"=="distributionUrl" SET DOWNLOAD_URL=%%B
IF "%%A"=="goVersion" SET GO_LATEST_VERSION=%%B
)

@REM Extension to allow automatically downloading GO
Expand All @@ -109,6 +110,7 @@ if "%GOW_VERBOSE%" == "true" (

if exist %GO_ZIP_PATH% goto goZipDownloaded
if not "%DOWNLOAD_URL%" == "" goto goZipDownloadUrlReady
if not "%GO_LATEST_VERSION%" == "" goto buildDownloadUrl

@REM BAD Hack to retrieve latest version

Expand All @@ -131,7 +133,7 @@ set GO_LATEST_VERSION=%GO_LATEST_VERSION:~3,-2%
DEL %GO_VERSION_PATH%.tmp
DEL %GO_VERSION_PATH%


:buildDownloadUrl
set GO_DOWNLOAD_ARCH=amd64
echo %PROCESSOR_ARCHITECTURE% | find /i "x86" > nul
if %ERRORLEVEL%==0 set GO_DOWNLOAD_ARCH=386
Expand Down Expand Up @@ -166,14 +168,31 @@ if "%GOW_VERBOSE%" == "true" (
)

set GOROOT=%GO_INSTALL_PATH%

@REM Set GOPATH in case there is none
if "%GOPATH%" == "" (
for /f %%i in ('%GOROOT%\bin\go env GOPATH') do set GOPATH=%%i
)
@REM End of extension

@REM Provide a "standardized" way to retrieve the CLI args that will
@REM work with both Windows and non-Windows executions.
:run
set GO_CMD_LINE_ARGS=%*

%GOROOT%\bin\go %*
@REM Export Go Variables for downstream executions
set "PATH=%GOROOT%\bin;%GOPATH%\bin;%PATH%"

if "%1" == "printenv" (
echo set GOROOT=%GOROOT%
echo set GOPATH=%GOPATH%
echo set PATH=%%GOROOT%%\bin;%%GOPATH%%\bin;%%PATH%%
echo:
echo # Run this command to configure your shell: copy and paste the above values into your command prompt
) else (
%GOROOT%\bin\go %*
)

if ERRORLEVEL 1 goto error
goto end

Expand Down
20 changes: 14 additions & 6 deletions pkg/go-boot-config/file-property-source.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gobootconfig

import (
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -97,35 +98,42 @@ func filerActiveProfiles(activeProfiles []string, profiles []string) []string {
return tmpProfiles
}

func loadFrom(path, source string, loader func(string, func(io.Reader, map[string]interface{})) map[string]interface{}, parser func(io.Reader, map[string]interface{})) {
configMap := loader(path, parser)
func loadFrom(path, source string, loader func(string, func(io.Reader, map[string]interface{})) (map[string]interface{}, error), parser func(io.Reader, map[string]interface{})) {
configMap, err := loader(path, parser)
if err != nil {
log.Fatalf("Failed loading %s: %s", path, err)
return
}
for k, v := range configMap {
_addPropertySource(k, filePropertySource{source, path, k, v})
}
}

func loadFile(filePath string, read func(io.Reader, map[string]interface{})) map[string]interface{} {
func loadFile(filePath string, read func(io.Reader, map[string]interface{})) (map[string]interface{}, error) {
configs := make(map[string]interface{})
if ok, _ := pathExists(filePath, false); ok {
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Failed opening file %s: %s", filePath, err)
return configs, err
}
defer file.Close()
read(file, configs)
return configs, nil
}
return configs
return configs, errors.New("path not found")
}

func loadHttp(httpPath string, read func(io.Reader, map[string]interface{})) map[string]interface{} {
func loadHttp(httpPath string, read func(io.Reader, map[string]interface{})) (map[string]interface{}, error) {
configs := make(map[string]interface{})
resp, err := http.Get(httpPath)
if err != nil {
log.Fatalf("Failed opening http %s: %s", httpPath, err)
return configs, err
}
defer resp.Body.Close()
read(resp.Body, configs)
return configs
return configs, nil
}

func retrieveConfPath(profiles []string, extensions []string, forProfile bool) []string {
Expand Down
46 changes: 29 additions & 17 deletions pkg/go-boot-config/file-property-source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func Test_loadAllFrom(t *testing.T) {
}{
{"Nothing", createMap(), args{createArr(), "test", createArr(), true, parseProperties}, nil, true},
{"Properties From File Default", createMap(goConfigLocation, "../../test/default-properties/application.properties"), args{createArr(), "test", createArr("properties"), false, parseProperties}, "default-properties", false},
//{"Properties From HTTP", createMap(goConfigLocation, "http://localhost:3000/application.properties"), args{createArr(), "test", createArr("properties"), false, parseProperties}, "http-properties", false},
{"Properties From HTTP", createMap(goConfigLocation, "http://localhost:3000/application.properties"), args{createArr(), "test", createArr("properties"), false, parseProperties}, "http-properties", false},
}
for _, tt := range tests {
_clear()
Expand Down Expand Up @@ -291,15 +291,15 @@ func Test_filerActiveProfiles(t *testing.T) {
}

func Test_loadFrom(t *testing.T) {
f := func(m map[string]interface{}) func(string, func(io.Reader, map[string]interface{})) map[string]interface{} {
return func(s string, i func(io.Reader, map[string]interface{})) map[string]interface{} {
return m
f := func(m map[string]interface{}) func(string, func(io.Reader, map[string]interface{})) (map[string]interface{}, error) {
return func(s string, i func(io.Reader, map[string]interface{})) (map[string]interface{}, error) {
return m, nil
}
}
type args struct {
path string
source string
loader func(string, func(io.Reader, map[string]interface{})) map[string]interface{}
loader func(string, func(io.Reader, map[string]interface{})) (map[string]interface{}, error)
parser func(io.Reader, map[string]interface{})
}
tests := []struct {
Expand Down Expand Up @@ -333,16 +333,22 @@ func Test_loadFile(t *testing.T) {
read func(io.Reader, map[string]interface{})
}
tests := []struct {
name string
args args
want map[string]interface{}
name string
args args
want map[string]interface{}
wantErr bool
}{
{"None", args{"none.properties", parseProperties}, createMap()},
{"Simple", args{"../../test/default-properties/application.properties", parseProperties}, createMap("go.string", "default-properties")},
{"None", args{"none.properties", parseProperties}, createMap(), true},
{"Simple", args{"../../test/default-properties/application.properties", parseProperties}, createMap("go.string", "default-properties"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := loadFile(tt.args.filePath, tt.args.read); !reflect.DeepEqual(got, tt.want) {
got, err := loadFile(tt.args.filePath, tt.args.read)
if (err != nil) != tt.wantErr {
t.Errorf("loadFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("loadFile() = %v, want %v", got, tt.want)
}
})
Expand All @@ -356,16 +362,22 @@ func Test_loadHttp(t *testing.T) {
read func(io.Reader, map[string]interface{})
}
tests := []struct {
name string
args args
want map[string]interface{}
name string
args args
want map[string]interface{}
wantErr bool
}{
{"None", args{"http://localhost:3000/mamamia.properties", parseProperties}, createMap()},
{"Simple", args{"http://localhost:3000//application.properties", parseProperties}, createMap("go.string", "http-properties")},
{"None", args{"http://localhost:3000/mamamia.properties", parseProperties}, createMap(), false},
{"Simple", args{"http://localhost:3000//application.properties", parseProperties}, createMap("go.string", "http-properties"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := loadHttp(tt.args.httpPath, tt.args.read); !reflect.DeepEqual(got, tt.want) {
got, err := loadHttp(tt.args.httpPath, tt.args.read)
if (err != nil) != tt.wantErr {
t.Errorf("loadHttp() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("loadHttp() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 94333cc

Please sign in to comment.