1/*
2Copyright 2019 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package pause
18
19import (
20	"fmt"
21	"os"
22	"os/signal"
23	"syscall"
24
25	"github.com/spf13/cobra"
26)
27
28// CmdPause is used by agnhost Cobra.
29var CmdPause = &cobra.Command{
30	Use:   "pause",
31	Short: "Pauses the execution",
32	Long:  `Pauses the execution. Useful for keeping the containers running, so other commands can be executed.`,
33	Args:  cobra.MaximumNArgs(0),
34	Run:   pause,
35}
36
37func pause(cmd *cobra.Command, args []string) {
38	fmt.Println("Paused")
39	sigCh := make(chan os.Signal)
40	done := make(chan int, 1)
41	signal.Notify(sigCh, syscall.SIGINT)
42	signal.Notify(sigCh, syscall.SIGTERM)
43	go func() {
44		sig := <-sigCh
45		switch sig {
46		case syscall.SIGINT:
47			done <- 1
48			os.Exit(1)
49		case syscall.SIGTERM:
50			done <- 2
51			os.Exit(2)
52		}
53	}()
54	result := <-done
55	fmt.Printf("exiting %d\n", result)
56}
57