github.com/scottcagno/storage@v1.8.0/cmd/signals/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/scottcagno/storage/pkg/util"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  )
    10  
    11  func main() {
    12  
    13  	// Setup our Ctrl+C handler
    14  	util.ShutdownHook(func() {
    15  		// remove all files
    16  		DeleteFiles()
    17  	})
    18  
    19  	// Run our program... We create a file to clean up then sleep
    20  	CreateFile()
    21  	for {
    22  		fmt.Println("- Sleeping")
    23  		time.Sleep(10 * time.Second)
    24  	}
    25  }
    26  
    27  const FileNameExample = "cmd/signals/files/go-example.txt"
    28  
    29  // DeleteFiles is used to simulate a 'clean up' function to run on shutdown. Because
    30  // it's just an example it doesn't have any error handling.
    31  func DeleteFiles() {
    32  	fmt.Println("- Run Clean Up - Delete Our Example File")
    33  	_ = os.Remove(FileNameExample)
    34  	fmt.Println("- Good bye!")
    35  }
    36  
    37  // CreateFile creates a file so that we have something to clean up when we close our program.
    38  func CreateFile() {
    39  	fmt.Println("- Create Our Example File")
    40  	_ = os.MkdirAll(filepath.Dir(FileNameExample), os.ModeDir)
    41  	file, _ := os.Create(FileNameExample)
    42  	defer file.Close()
    43  }