Skip to content

Commit

Permalink
fix: Example values containing "=" were ignored
Browse files Browse the repository at this point in the history
- Add new '--version' flag
- Add new BUILD_DATE for use in --version output
  • Loading branch information
acaloiaro committed Jan 19, 2024
1 parent a521398 commit 141a715
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@ jobs:
git config --global user.email "actions@github.com"
git config --global user.name "Github Actions"
go install git.sr.ht/~jcmuller/semver-bumper@latest
OLD_TAG=$(git tag --list 'v*' | semver-bumper -s)
NEW_TAG=$(git tag --list 'v*' | semver-bumper --increment minor)
sed -i "s/$OLD_TAG/$NEW_TAG/g" default.nix
newTag=$(git tag --list 'v*' | semver-bumper --increment minor)
buildDate=$(date --iso-8601=seconds)
sed -i -e "s@BUILD_DATE = \".\+\"@BUILD_DATE = \"${buildDate/v}\"@" main.go
sed -i -e "s@VERSION = \".\+\"@VERSION = \"${newTag/v}\"@" main.go
sed -i -e "s@version = \".\+\"@version = \"${newTag/v}\"@" default.nix
git add default.nix
git commit -m "Bump version in default.nix" --allow-empty
git add main.go
git commit -m "Bump release version"
git tag $NEW_TAG
git push
git push --tags
- name: Checkout after code mods
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
Expand Down
46 changes: 39 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"path/filepath"
Expand All @@ -17,6 +18,9 @@ import (
"github.com/hashicorp/go-envparse"
)

const VERSION = "2.10.0"
const BUILD_DATE = "2024-01-19T12:19:09-07:00"

type exampleFlag map[string]string

func (e exampleFlag) String() string {
Expand All @@ -29,27 +33,32 @@ func (e exampleFlag) String() string {
}

func (e exampleFlag) Set(value string) (err error) {
exampleParts := strings.Split(value, "=")
if len(exampleParts) != 2 {
key, value, found := strings.Cut(value, "=")
if !found {
err = errors.New("examples must be provided in KEY=VALUE format")
return
}

e[exampleParts[0]] = exampleParts[1]
e[key] = value

return nil
}

var (
examplesFlag = make(exampleFlag)
debugFlag bool
envFileFlag string
logLevel = slog.LevelInfo
sampleFileFlag string
examplesFlag = make(exampleFlag)
versionFlag bool
)

func init() {
flag.StringVar(&envFileFlag, "env-file", ".env", "-env-file=.env_file")
flag.StringVar(&sampleFileFlag, "sample-file", "env.sample", "-sample-file=env_var.sample")
flag.Var(examplesFlag, "example", "--example=FOO=\"my foo value\" --example=BAR=\"my bar value\"")
flag.StringVar(&envFileFlag, "env-file", ".env", "set the path to your env file: ess -env-file=.env_file [sync|install]")
flag.StringVar(&sampleFileFlag, "sample-file", "env.sample", "set the path to your sample file: ess -sample-file=env_var.sample [sync|install]")
flag.BoolVar(&debugFlag, "debug", false, "print debug logs: ess --debug [sync|install]")
flag.Var(examplesFlag, "example", "set example values for samples: ess --example=BAR=\"my bar value\" [sync|install]")
flag.BoolVar(&versionFlag, "version", false, "print the current ess version: ess --version")

flag.Usage = func() {
cmd := os.Args[0]
Expand All @@ -59,6 +68,16 @@ func init() {
}

flag.Parse()

if versionFlag {
fmt.Fprintf(os.Stdout, "ess version: %s built at: %s\n", VERSION, BUILD_DATE)
os.Exit(0)
}

if debugFlag {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
}

}

func main() {
Expand All @@ -74,12 +93,14 @@ func main() {
case "sync":
sync(projectPath)
cmd := exec.Command("git", "add", filepath.Join(projectPath, sampleFileFlag))
slog.Debug("running git command", "args", cmd.Args)
err := cmd.Run()
if err != nil {
fmt.Printf("unable to add sample file '%s' to git: %v", sampleFileFlag, err)
os.Exit(1)
}
case "install":
slog.Debug("installing hook to", "path", gitDirPath)
err := installHook(gitDirPath)
if err != nil {
fmt.Println("unable to install pre-commit hook:", err)
Expand All @@ -88,13 +109,16 @@ func main() {
default:
fmt.Printf("ess: unknown command '%s'\n\n", command)
flag.Usage()
os.Exit(1)
}
}

func sync(dir string) {
envFilePath := filepath.Join(dir, envFileFlag)
sampleFilePath := filepath.Join(dir, sampleFileFlag)

slog.Debug("syncing env file with sample", "env_file", envFilePath, "sample_file", sampleFilePath)

envFileReader, err := os.Open(envFilePath)
if err != nil {
fmt.Printf("env file '%s' was not found. skipping sync.\n", envFilePath)
Expand All @@ -113,6 +137,8 @@ func sync(dir string) {
fmt.Println(err)
os.Exit(1)
}

slog.Debug("sample file written", "sample_file", sampleFilePath)
}

func writeSampleFile(sampleFileContent map[string]string, envFilePath, sampleFilePath string) (err error) {
Expand Down Expand Up @@ -148,6 +174,7 @@ func writeSampleFile(sampleFileContent map[string]string, envFilePath, sampleFil
}

func scrubEnvFile(envFile map[string]string, examples map[string]string) {
slog.Debug("scrubbing env file of secrets")
for envFileKey := range envFile {
exampleVal, ok := examples[envFileKey]
if ok {
Expand All @@ -168,6 +195,7 @@ func replaceSecrets(envFileEntry string, sampleFileContent map[string]string) (n
for secretKey, secretPlaceholder := range sampleFileContent {
r = regexp.MustCompile(fmt.Sprintf("(%s.*=.*)", secretKey))
if r.MatchString(envFileEntry) {
slog.Debug("replacing secrets with sample values", "secret_key", secretKey, "placeholder", secretPlaceholder)
newLine = r.ReplaceAllString(envFileEntry, fmt.Sprintf("%s=%s", secretKey, secretPlaceholder))
}
}
Expand Down Expand Up @@ -232,9 +260,11 @@ func installHook(gitDirPath string) (err error) {
var response string
fmt.Scanln(&response)
if response == "c" || (response != "o" && response != "a") {
slog.Debug("user declined to overwrite the existing pre-commit hook")
os.Exit(0)
}
if response == "a" {
slog.Debug("will append pre-commit hook script to existing script", "script_path", preCommitHookScriptPath)
// read the existing content of the pre-commit file
oldPreCommitScript, err = os.ReadFile(preCommitHookScriptPath)
if err != nil {
Expand All @@ -252,6 +282,7 @@ func installHook(gitDirPath string) (err error) {
}
}

slog.Debug("hook will be installed in", "dir_path", hooksScriptDirPath)
preCommitHookPath := filepath.Join(hooksScriptDirPath, "0-ess")
preCommitHook, err := os.OpenFile(preCommitHookPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)
if err != nil {
Expand Down Expand Up @@ -291,6 +322,7 @@ func installHook(gitDirPath string) (err error) {
OldPreCommitScript: string(oldPreCommitScript),
}

slog.Debug("hook metadata", "data", templateValues)
err = tmpl.Execute(buff, templateValues)
if err != nil {
return err
Expand Down

0 comments on commit 141a715

Please sign in to comment.