Skip to content
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
merged 10 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ node_modules/
*.versionsBackup
*~
dependency-reduced-pom.xml

# Go binary
/go/nitor-vault
/go/nitor_vault
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ repos:
types: [file, rust]
language: system
files: ^rust/

- id: gofmt
name: go format
description: Run gofmt on files included in the commit.
entry: bash -c 'cd go && gofmt -s -w .'
pass_filenames: false
types: [file, go]
language: system
files: ^go/
11 changes: 10 additions & 1 deletion common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set -eo pipefail

# Common shell functions and definitions

export REPO_ROOT=$(git rev-parse --show-toplevel || (cd "$(dirname "${BASH_SOURCE[0]}")" && pwd))
REPO_ROOT=$(git rev-parse --show-toplevel || (cd "$(dirname "${BASH_SOURCE[0]}")" && pwd))
export REPO_ROOT

# Check platform
case "$(uname -s)" in
Expand Down Expand Up @@ -81,3 +82,11 @@ run_command() {
"$@"
fi
}

# Set variables GIT_HASH and GIT_BRANCH
set_version_info() {
GIT_HASH=$(git -C "$REPO_ROOT" rev-parse --short HEAD)
GIT_BRANCH=$(git -C "$REPO_ROOT" branch --show-current)
export GIT_HASH
export GIT_BRANCH
}
33 changes: 32 additions & 1 deletion go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,35 @@ Before you begin, ensure you have met the following requirements:
To build the `nitor-vault` tool, follow these steps:

```shell
go build -o nitor-vault ./cmd/nitor_vault
./build.sh
```

Or manually:

```shell
go build -v -o nitor-vault
```

## Format code

Using [gofmt](https://pkg.go.dev/cmd/gofmt)

```shell
gofmt -s -w .
```

## Update version number

Increment minor version:

```shell
./update_version.sh
```

Override version manually:

```shell
./update_version.sh --version 1.2.3
# this also works
VERSION=1.2.3 ./update_version.sh
```
76 changes: 76 additions & 0 deletions go/build.sh
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
58 changes: 45 additions & 13 deletions go/cmd/nitor_vault/main.go → go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Comment on lines +22 to +25
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➜  go git:(go-tweaks) ✗
./nitor-vault --version
1.0.0 2023-11-06T10:52:07Z go-tweaks 56179584315cbe480e377a54bdac7d2d7f4fe863 go1.21.3 arm64


// 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2023-11-06 at 11 30 31

} 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)
}
Expand All @@ -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 {
Expand All @@ -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 ""
}
85 changes: 85 additions & 0 deletions go/update_version.sh
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"
Loading
Loading