From 77af83ea857c37410ad987c697889419ff8e7310 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Tue, 4 Feb 2025 08:37:17 -0500 Subject: [PATCH] feat(windows): detect double-click launch Signed-off-by: Chris Gianelloni --- cmd/adder/main.go | 32 ++++++++++++++++++++++++++++---- go.mod | 1 + go.sum | 2 ++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cmd/adder/main.go b/cmd/adder/main.go index 87378d5..c9f5e64 100644 --- a/cmd/adder/main.go +++ b/cmd/adder/main.go @@ -1,4 +1,4 @@ -// Copyright 2024 Blink Labs Software +// Copyright 2025 Blink Labs Software // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,7 +19,10 @@ import ( "log/slog" "net/http" "os" + "runtime" + "time" + "github.com/inconshreveable/mousetrap" "go.uber.org/automaxprocs/maxprocs" "github.com/blinklabs-io/adder/api" @@ -33,14 +36,35 @@ import ( "github.com/blinklabs-io/adder/plugin" ) -const ( - programName = "adder" -) +var programName string = "adder" func slogPrintf(format string, v ...any) { slog.Info(fmt.Sprintf(format, v...)) } +func init() { + if os.Args != nil && os.Args[0] != programName { + programName = os.Args[0] + } + + // Bail if we were run via double click on Windows, borrowed from ngrok + if runtime.GOOS == "windows" { + if mousetrap.StartedByExplorer() { + fmt.Println("Adder is a command line program.") + fmt.Printf( + "You need to open cmd.exe and run %s from the command line.\n", + programName, + ) + fmt.Printf( + "Try %s --help to get program usage information.\n", + programName, + ) + time.Sleep(30 * time.Second) + os.Exit(1) + } + } +} + func main() { cfg := config.GetConfig() diff --git a/go.mod b/go.mod index c00501c..bbf0b7a 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/btcsuite/btcd/btcutil v1.1.6 github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd github.com/gin-gonic/gin v1.10.0 + github.com/inconshreveable/mousetrap v1.1.0 github.com/kelseyhightower/envconfig v1.4.0 github.com/stretchr/testify v1.10.0 github.com/swaggo/files v1.0.1 diff --git a/go.sum b/go.sum index 608af81..cf53a2b 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,8 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=