Skip to content

Commit

Permalink
Make local.env creation idempotent in task setup
Browse files Browse the repository at this point in the history
  • Loading branch information
rmunn committed May 27, 2024
1 parent 774f253 commit 924adac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ tasks:
deps: [ setup-win, setup-unix ]
cmds:
- git config blame.ignoreRevsFile .git-blame-ignore-revs
- echo "HONEYCOMB_API_KEY=__REPLACE__" >> deployment/local-dev/local.env
- echo "#OTEL_SDK_DISABLED=true" >> deployment/local-dev/local.env
- echo "GOOGLE_OAUTH_CLIENT_ID=__REPLACE__.apps.googleusercontent.com" >> deployment/local-dev/local.env
- echo "GOOGLE_OAUTH_CLIENT_SECRET=__REPLACE__" >> deployment/local-dev/local.env
- ./ensure.py deployment/local-dev/local.env HONEYCOMB_API_KEY __REPLACE__
- ./ensure.py deployment/local-dev/local.env '#OTEL_SDK_DISABLED' true
- ./ensure.py deployment/local-dev/local.env GOOGLE_OAUTH_CLIENT_ID __REPLACE__.apps.googleusercontent.com
- ./ensure.py deployment/local-dev/local.env GOOGLE_OAUTH_CLIENT_SECRET __REPLACE__
- kubectl --context=docker-desktop apply -f deployment/setup/namespace.yaml
setup-win:
platforms: [ windows ]
Expand Down
40 changes: 40 additions & 0 deletions ensure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

import os
import sys

def usage():
name = os.path.basename(sys.argv[0])
print(f"Usage: {name} file key value")
print("")
print(f"E.g., {name} local.env HONEYCOMB_API_KEY __REPLACE__")

def ensure(fname, search_str, replace_str):
# Ensure search_str is present. If not found, add replace_str to end of file
found = False
try:
with open(fname, encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if search_str in line:
found = True
break
except FileNotFoundError:
lines = []
need_extra_newline = False
if lines and lines[-1] and not lines[-1].endswith('\n'):
need_extra_newline = True
if not found:
if need_extra_newline:
lines[-1] = lines[-1] + '\n'
lines.append(replace_str + '\n')
with open(fname, 'w', encoding='utf-8') as f:
f.writelines(lines)

if __name__ == '__main__':
if (len(sys.argv) < 4):
usage()
(fname, key, value) = sys.argv[1:4]
search_str = f"{key}="
replace_str = f"{key}={value}"
ensure(fname, search_str, replace_str)

0 comments on commit 924adac

Please sign in to comment.