-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Go tweaks #251
Merged
Go tweaks #251
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d368a5f
add gofmt to pre-commit config
Esgrove ae94ac8
add readme entry for gofmt
Esgrove 913ade8
format code
Esgrove 369b834
gitignore go binary
Esgrove 7f18d11
add version info
Esgrove 49d0788
add go scripts
Esgrove aec43a5
check all go files
Esgrove 26c93ad
simplify project structure
Esgrove 5617958
enable data race detection
Esgrove c70c8a3
fix indent, why is my shfmt not working :thinking:
Esgrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,7 @@ node_modules/ | |
*.versionsBackup | ||
*~ | ||
dependency-reduced-pom.xml | ||
|
||
# Go binary | ||
/go/nitor-vault | ||
/go/nitor_vault |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/bin/bash | ||
set -eo pipefail | ||
|
||
# Import common functions | ||
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) | ||
# shellcheck source=../common.sh | ||
source "$DIR/../common.sh" | ||
|
||
USAGE="Usage: $(basename "$0") [OPTIONS] | ||
|
||
OPTIONS: All options are optional | ||
-h | --help | ||
Display these instructions. | ||
|
||
-v | --verbose | ||
Display commands being executed." | ||
|
||
init_options() { | ||
while [ $# -gt 0 ]; do | ||
case "$1" in | ||
-h | --help) | ||
echo "$USAGE" | ||
exit 1 | ||
;; | ||
-v | --verbose) | ||
set -x | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
# Get absolute path to repo root | ||
REPO_ROOT=$(git rev-parse --show-toplevel || (cd "$(dirname "../${BASH_SOURCE[0]}")" && pwd)) | ||
PROJECT_PATH="$REPO_ROOT/go" | ||
|
||
if [ "$PLATFORM" = windows ]; then | ||
EXECUTABLE="nitor-vault.exe" | ||
else | ||
EXECUTABLE="nitor-vault" | ||
fi | ||
} | ||
|
||
build_project() { | ||
print_magenta "Building Nitor Vault (Go)..." | ||
if [ -z "$(command -v go)" ]; then | ||
print_error_and_exit "go not found in path" | ||
else | ||
go version | ||
fi | ||
|
||
pushd "$PROJECT_PATH" > /dev/null | ||
rm -f "$EXECUTABLE" | ||
time go build -v -race -o nitor-vault | ||
|
||
file "$EXECUTABLE" | ||
./"$EXECUTABLE" --version | ||
popd > /dev/null | ||
} | ||
|
||
update_version_file() { | ||
set_version_info | ||
VERSION_FILE="$PROJECT_PATH/vault/version.go" | ||
CURRENT_VERSION="$(grep "const VersionNumber =" "$VERSION_FILE" | cut -d\" -f 2)" | ||
{ | ||
echo "package vault" | ||
echo "" | ||
echo "// Generated automatically; DO NOT EDIT MANUALLY." | ||
echo "" | ||
echo "const VersionNumber = \"$CURRENT_VERSION\"" | ||
echo "const GitBranch = \"$GIT_BRANCH\"" | ||
} > "$VERSION_FILE" | ||
} | ||
|
||
init_options "$@" | ||
update_version_file | ||
build_project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,28 +5,36 @@ import ( | |
"fmt" | ||
"log" | ||
"nitor_vault/vault" | ||
"os" | ||
"runtime/debug" | ||
) | ||
|
||
func main() { | ||
//TODO: replace "flag" implementation with e.g. https://github.com/spf13/cobra | ||
// TODO: replace "flag" implementation with e.g. https://github.com/spf13/cobra | ||
aFlag := flag.Bool("a", false, "list all flag") | ||
lFlag := flag.String("l", "", "lookup flag, usage: -l <key>") | ||
sFlag := flag.String("s", "", "store flag, usage together with -v: -s <key> -v <value string>") | ||
vFlag := flag.String("v", "", "value used with store flag") | ||
wFlag := flag.Bool("w", false, "overwrite flag used with store flag") | ||
versionFlag := flag.Bool("version", false, "print version information and exit") | ||
flag.Parse() | ||
|
||
if *versionFlag { | ||
fmt.Println(VersionInfo()) | ||
os.Exit(0) | ||
} | ||
|
||
// Check if the flags are provided and act accordingly | ||
if *aFlag { | ||
vault := initVault() | ||
all(vault) | ||
nVault := initVault() | ||
all(nVault) | ||
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} else if *lFlag != "" { | ||
vault := initVault() | ||
lookup(vault, lFlag) | ||
nVault := initVault() | ||
lookup(nVault, lFlag) | ||
} else if *sFlag != "" && *vFlag != "" { | ||
vault := initVault() | ||
nVault := initVault() | ||
if !*wFlag { | ||
exists, err := vault.Exists(*sFlag) | ||
exists, err := nVault.Exists(*sFlag) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
@@ -35,20 +43,21 @@ func main() { | |
return | ||
} | ||
} | ||
store(vault, sFlag, []byte(*vFlag)) | ||
store(nVault, sFlag, []byte(*vFlag)) | ||
} else { | ||
flag.CommandLine.Usage() | ||
} | ||
} | ||
|
||
// CLI helper funcs | ||
// CLI helper functions | ||
func initVault() vault.Vault { | ||
vault, err := vault.LoadVault() | ||
nVault, err := vault.LoadVault() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return vault | ||
return nVault | ||
} | ||
|
||
func all(vault vault.Vault) { | ||
all, err := vault.All() | ||
if err != nil { | ||
|
@@ -58,18 +67,41 @@ func all(vault vault.Vault) { | |
fmt.Println(key) | ||
} | ||
} | ||
func lookup(vault vault.Vault, key *string) { | ||
|
||
func lookup(vault vault.Vault, key *string) { | ||
res, err := vault.Lookup(*key) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("%s", res) | ||
} | ||
func store(vault vault.Vault, key *string, value []byte) { | ||
|
||
func store(vault vault.Vault, key *string, value []byte) { | ||
err := vault.Store(*key, value) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// VersionInfo Returns formatted build version info string. | ||
func VersionInfo() string { | ||
if info, ok := debug.ReadBuildInfo(); ok { | ||
goVersion := info.GoVersion | ||
commit := "unknown" | ||
timestamp := "unknown" | ||
arch := "unknown" | ||
for _, setting := range info.Settings { | ||
if setting.Key == "vcs.revision" { | ||
commit = setting.Value | ||
} | ||
if setting.Key == "vcs.time" { | ||
timestamp = setting.Value | ||
} | ||
if setting.Key == "GOARCH" { | ||
arch = setting.Value | ||
} | ||
} | ||
return fmt.Sprintf("%s %s %s %s %s %s", vault.VersionNumber, timestamp, vault.GitBranch, commit, goVersion, arch) | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/bin/bash | ||
set -eo pipefail | ||
|
||
# Import common functions | ||
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) | ||
# shellcheck source=../common.sh | ||
source "$DIR/../common.sh" | ||
|
||
USAGE="Usage: $(basename "$0") [OPTIONS] | ||
|
||
OPTIONS: All options are optional | ||
-h | --help | ||
Display these instructions. | ||
|
||
-m | --major | ||
Increment major version. | ||
|
||
-v | --version <VERSION> | ||
Use given version as the new version number. | ||
|
||
--verbose | ||
Display commands being executed." | ||
|
||
init_options() { | ||
INCREMENT_MAJOR=false | ||
while [ $# -gt 0 ]; do | ||
case "$1" in | ||
-h | --help) | ||
echo "$USAGE" | ||
exit 1 | ||
;; | ||
-m | --major) | ||
INCREMENT_MAJOR=true | ||
;; | ||
-v | --version) | ||
VERSION="$2" | ||
shift | ||
;; | ||
--verbose) | ||
set -x | ||
;; | ||
esac | ||
shift | ||
done | ||
} | ||
|
||
init_options "$@" | ||
|
||
VERSION_FILE="$DIR/vault/version.go" | ||
|
||
CURRENT_VERSION="$(grep "const VersionNumber =" "$VERSION_FILE" | cut -d\" -f 2)" | ||
MAJOR=$(echo "$CURRENT_VERSION" | cut -d '.' -f 1) | ||
MINOR=$(echo "$CURRENT_VERSION" | cut -d '.' -f 2) | ||
|
||
if [ -n "$VERSION" ] && [ "$INCREMENT_MAJOR" = true ]; then | ||
print_warn "Conflicting version arguments, using $VERSION" | ||
fi | ||
|
||
if [ -n "$VERSION" ]; then | ||
NEW_VERSION="$VERSION" | ||
elif [ "$INCREMENT_MAJOR" = true ]; then | ||
MAJOR=$((MAJOR + 1)) | ||
NEW_VERSION="$MAJOR.0.0" | ||
else | ||
echo "Incrementing minor version" | ||
MINOR=$((MINOR + 1)) | ||
NEW_VERSION="$MAJOR.$MINOR.0" | ||
fi | ||
|
||
echo "Current version: $CURRENT_VERSION" | ||
if [[ "$NEW_VERSION" =~ ^[0-9]+(\.[0-9]+){2}$ ]]; then | ||
print_green "New version number: $NEW_VERSION" | ||
else | ||
print_error_and_exit "Version number needs to be in format 'X.X.X', got: $NEW_VERSION" | ||
fi | ||
|
||
set_version_info | ||
{ | ||
echo "package vault" | ||
echo "" | ||
echo "// Generated automatically; DO NOT EDIT MANUALLY." | ||
echo "" | ||
echo "const VersionNumber = \"$NEW_VERSION\"" | ||
echo "const GitBranch = \"$GIT_BRANCH\"" | ||
} > "$VERSION_FILE" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.