pax_global_header00006660000000000000000000000064133703404240014512gustar00rootroot0000000000000052 comment=a55db27f18335e11f995bfc0fb4a7ab38ce5669d go-cleanup-1.0.0/000077500000000000000000000000001337034042400135425ustar00rootroot00000000000000go-cleanup-1.0.0/LICENSE000066400000000000000000000027431337034042400145550ustar00rootroot00000000000000 Copyright (c) 2018, Cristian Maglie. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. go-cleanup-1.0.0/context.go000066400000000000000000000013071337034042400155560ustar00rootroot00000000000000// // Copyright 2018 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package cleanup import ( "context" "os" "os/signal" ) // InterruptableContext adds to a context the capability to be interrupted by the os.Interrupt signal. func InterruptableContext(inCtx context.Context) (context.Context, context.CancelFunc) { ctx, cancel := context.WithCancel(inCtx) go func() { ctrlC := make(chan os.Signal, 1) signal.Notify(ctrlC, os.Interrupt) select { case <-ctx.Done(): break // used to show test coverage case <-ctrlC: cancel() } signal.Stop(ctrlC) close(ctrlC) }() return ctx, cancel } go-cleanup-1.0.0/context_test.go000066400000000000000000000021141337034042400166120ustar00rootroot00000000000000// // Copyright 2018 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // package cleanup_test import ( "context" "errors" "os" "testing" "time" "github.com/stretchr/testify/require" "go.bug.st/cleanup" ) func TestInterruptableContext(t *testing.T) { ctx, cancel := cleanup.InterruptableContext(context.Background()) defer cancel() err := ctxDelay(ctx) require.NoError(t, err) } func TestInterruptableContextWithInterruption(t *testing.T) { ctx, cancel := cleanup.InterruptableContext(context.Background()) defer cancel() go func() { time.Sleep(100 * time.Millisecond) // Simulate CTRL+C pid := os.Getpid() p, err := os.FindProcess(pid) require.NoError(t, err) err = p.Signal(os.Interrupt) require.NoError(t, err) }() err := ctxDelay(ctx) require.Error(t, err) require.Equal(t, "interrupted", err.Error()) } func ctxDelay(ctx context.Context) error { select { case <-ctx.Done(): return errors.New("interrupted") case <-time.After(time.Second): return nil } } go-cleanup-1.0.0/go.mod000066400000000000000000000002531337034042400146500ustar00rootroot00000000000000module go.bug.st/cleanup require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.2 ) go-cleanup-1.0.0/go.sum000066400000000000000000000010111337034042400146660ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=