From 3ae571312d74c10127453a608d65d75e0cb0275f Mon Sep 17 00:00:00 2001
From: Benjamin Drung <bdrung@debian.org>
Date: Thu, 31 Dec 2020 22:14:45 +0100
Subject: Replace go-shell

go-shell is a tiny library and misses copyright information.

Therefore just replace the single function used from go-shell.

Signed-off-by: Benjamin Drung <bdrung@debian.org>
---
 examples/example1.go | 10 +---------
 sht3x.go             | 24 +++++++++++++++++-------
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/examples/example1.go b/examples/example1.go
index 88482ab..19b6fa6 100644
--- a/examples/example1.go
+++ b/examples/example1.go
@@ -2,13 +2,10 @@ package main
 
 import (
 	"context"
-	"os"
-	"syscall"
 	"time"
 
 	i2c "github.com/d2r2/go-i2c"
 	logger "github.com/d2r2/go-logger"
-	shell "github.com/d2r2/go-shell"
 	sht3x "github.com/d2r2/go-sht3x"
 )
 
@@ -182,13 +179,8 @@ func main() {
 	// use done channel as a trigger to exit from signal waiting goroutine
 	done := make(chan struct{})
 	defer close(done)
-	// build actual signal list to control
-	signals := []os.Signal{os.Kill, os.Interrupt}
-	if shell.IsLinuxMacOSFreeBSD() {
-		signals = append(signals, syscall.SIGTERM)
-	}
 	// run goroutine waiting for OS termination events, including keyboard Ctrl+C
-	shell.CloseContextOnSignals(cancel, done, signals...)
+	sht3x.CloseContextOnSignals(cancel, done)
 
 	err = sensor.SetHeaterStatus(i2c, true)
 	if err != nil {
diff --git a/sht3x.go b/sht3x.go
index ae20b04..0b86202 100644
--- a/sht3x.go
+++ b/sht3x.go
@@ -27,12 +27,12 @@ import (
 	"encoding/binary"
 	"errors"
 	"os"
+	"os/signal"
 	"reflect"
 	"syscall"
 	"time"
 
 	i2c "github.com/d2r2/go-i2c"
-	shell "github.com/d2r2/go-shell"
 	"github.com/davecgh/go-spew/spew"
 )
 
@@ -579,6 +579,21 @@ func (v *SHT3X) FetchUncompTemperatureAndHumidity(i2c *i2c.I2C) (ut uint16, uh u
 		i2c)
 }
 
+func CloseContextOnSignals(cancel context.CancelFunc, quit chan struct{}) {
+	channel := make(chan os.Signal, 1)
+	signal.Notify(channel, os.Kill, os.Interrupt, syscall.SIGTERM)
+	go func() {
+		select {
+		case <-channel:
+			if cancel != nil {
+				cancel()
+			}
+		case <-quit:
+			// exit
+		}
+	}()
+}
+
 // FetchUncompTemperatureAndHumidityWithContext return
 // uncompensated temperature and humidity obtained from sensor.
 // Use context parameter, since operation is time consuming
@@ -600,13 +615,8 @@ func (v *SHT3X) FetchUncompTemperatureAndHumidityWithContext(parent context.Cont
 	// use done channel as a trigger to exit from signal waiting goroutine
 	done := make(chan struct{})
 	defer close(done)
-	// build actual signal list to control
-	signals := []os.Signal{os.Kill, os.Interrupt}
-	if shell.IsLinuxMacOSFreeBSD() {
-		signals = append(signals, syscall.SIGTERM)
-	}
 	// run goroutine waiting for OS termination events, including keyboard Ctrl+C.
-	shell.CloseContextOnSignals(cancel, done, signals...)
+	CloseContextOnSignals(cancel, done)
 
 	retryCount := 5
 	var data []uint16
-- 
2.27.0

