Skip to content

Commit

Permalink
fix: fixed outbound connections and added offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
idoavrah committed May 14, 2024
1 parent 5988517 commit 8060e73
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ With its latest version you can easily visualize the complete state tree, gainin

## Changelog (latest versions)

### Version 0.13
### Version 0.13.1

- [x] Added support for workspace switching
- [x] Added plan summary in the screen title
- [x] Empty tree is now shown when no state exists instead of program shutting down, allowing for plan creation
- [x] Added `-o` flag for offline mode (no outbound API calls)
- [x] Removed the default outbound call to PostHog when tracking is disabled

### Version 0.12

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "tftui"
version = "0.13.0"
version = "0.13.1"
description = "Terraform Textual User Interface"
authors = ["Ido Avraham"]
license = "Apache-2.0"
Expand Down
15 changes: 10 additions & 5 deletions tftui/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,6 @@ def _on_resize(self, event):
main_height = max(event.size.height - 11, 5)
self.switcher.styles.height = main_height
logger.debug("Main height: %s", main_height)
OutboundAPIs.post_usage(
"app resize", size=f"{event.size.width}x{event.size.height}"
)
super()._on_resize(event)


Expand All @@ -637,6 +634,12 @@ def parse_command_line() -> None:
"--var-file",
help="tfvars filename to be used in planning",
)
parser.add_argument(
"-o",
"--offline",
help="run in offline mode (i.e. no outbound API calls; default online)",
action="store_true",
)
parser.add_argument(
"-d",
"--disable-usage-tracking",
Expand All @@ -660,13 +663,15 @@ def parse_command_line() -> None:
)
args = parser.parse_args()

if args.offline or args.disable_usage_tracking:
OutboundAPIs.disable_usage_tracking()
if not args.offline:
OutboundAPIs.check_for_new_version()
if args.version:
print(
f"\ntftui v{OutboundAPIs.version}{' (new version available)' if OutboundAPIs.is_new_version_available else ''}\n"
)
exit(0)
if args.disable_usage_tracking:
OutboundAPIs.disable_usage_tracking()
if args.executable:
ApplicationGlobals.executable = args.executable
if args.var_file:
Expand Down
35 changes: 21 additions & 14 deletions tftui/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@
import hashlib
import importlib.metadata
import requests
from posthog import Posthog
from tftui.constants import nouns, adjectives


class OutboundAPIs:
is_new_version_available = False
is_usage_tracking_enabled = True
generated_handle = None
posthog = None
version = importlib.metadata.version("tftui")

POSTHOG_API_KEY = "phc_tjGzx7V6Y85JdNfOFWxQLXo5wtUs6MeVLvoVfybqz09" # + "uncomment-while-developing"

posthog = Posthog(
project_api_key=POSTHOG_API_KEY,
host="https://app.posthog.com",
disable_geoip=False,
)

response = requests.get("https://pypi.org/pypi/tftui/json")
if response.status_code == 200:
ver = response.json()["info"]["version"]
if ver != version:
is_new_version_available = True
@staticmethod
def check_for_new_version():
try:
response = requests.get("https://pypi.org/pypi/tftui/json")
if response.status_code == 200:
ver = response.json()["info"]["version"]
if ver != OutboundAPIs.version:
OutboundAPIs.is_new_version_available = True
except Exception:
pass

@staticmethod
def generate_handle():
Expand All @@ -43,6 +40,16 @@ def post_usage(message: str, error_message="", platform="", size="") -> None:
return
if not OutboundAPIs.generated_handle:
OutboundAPIs.generate_handle()
if not OutboundAPIs.posthog:
from posthog import Posthog

POSTHOG_API_KEY = "phc_tjGzx7V6Y85JdNfOFWxQLXo5wtUs6MeVLvoVfybqz09" # + "uncomment-while-developing"

OutboundAPIs.posthog = Posthog(
project_api_key=POSTHOG_API_KEY,
host="https://app.posthog.com",
disable_geoip=False,
)

OutboundAPIs.posthog.capture(
OutboundAPIs.generated_handle,
Expand Down

0 comments on commit 8060e73

Please sign in to comment.