Compare commits

...

3 Commits

Author SHA1 Message Date
kts of kettek (nyaa) 8ef90ffd66 Detect if in TTY; output version info 2021-02-11 15:49:58 -08:00
kts of kettek (nyaa) 9c73761827 Add wonky git version id generation 2021-02-11 15:49:44 -08:00
kts of kettek (nyaa) 2ad513de61 Add istty pkg 2021-02-11 15:49:30 -08:00
6 changed files with 83 additions and 3 deletions

View File

@ -8,6 +8,8 @@ import (
"os"
"git.kettek.net/accord/service/internal/cfg"
"git.kettek.net/accord/service/internal/version"
"git.kettek.net/accord/service/pkg/istty"
)
func main() {
@ -18,9 +20,14 @@ func main() {
readChannel := make(chan AMessage)
quitChannel := make(chan error)
// Start our STDIN read loop
if cfg.UsePipes {
go readLoop(os.Stdin, quitChannel, readChannel)
if istty.Is() {
fmt.Println("Running from TTY, starting as stand-alone service.")
fmt.Printf(" Git Id: %s\n", version.GitCommitId)
} else {
// Start our STDIN read loop
if cfg.UsePipes {
go readLoop(os.Stdin, quitChannel, readChannel)
}
}
// Begin waiting for data on our channels.

View File

@ -0,0 +1,4 @@
// +build darwin,dragonfly,freebsd,linux,netbsd,openbsd,solaris
package version
//go:generate sh ./generate_nix.sh

View File

@ -0,0 +1,9 @@
echo "// Code generated automatically; DO NOT EDIT." > version.go
echo "package version" >> version.go
echo "" >> version.go
echo "var (" >> version.go
echo " // GitCommitId provides the git commit id used to build this service." >> version.go
echo -n " GitCommitId string = \"" >> version.go
git log -n1 --format="%h" | tr -d '\n' >> version.go
echo "\"" >> version.go
echo ")" >> version.go

View File

@ -0,0 +1,7 @@
// Code generated automatically; DO NOT EDIT.
package version
var (
// GitCommitId provides the git commit id used to build this service.
GitCommitId string = "c198177"
)

17
pkg/istty/istty.go 100644
View File

@ -0,0 +1,17 @@
package istty
// #cgo CFLAGS: -g -Wall
// #include "istty.h"
import "C"
// Is returns if the process context is a TTY.
func Is() bool {
t := C.isTTY()
return bool(t)
}
// Close closes the TTY if possible.
func Close() {
C.closeTTY()
}

36
pkg/istty/istty.h 100644
View File

@ -0,0 +1,36 @@
#ifdef _WIN32
#if _WIN32_WINNT < 0x0500
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#include <windows.h>
#include <stdbool.h>
#include "Wincon.h"
bool isTTY() {
HWND consoleWnd = GetConsoleWindow();
DWORD dwProcessId;
GetWindowThreadProcessId(consoleWnd, &dwProcessId);
if (GetCurrentProcessId()==dwProcessId) {
return false;
}
return true;
}
void closeTTY() {
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
#elif defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
bool isTTY() {
if (isatty(fileno(stdin))) {
return true;
}
return false;
}
void closeTTY() {}
#endif