go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/huectl/pkg/command/pause.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package command
     9  
    10  import (
    11  	"fmt"
    12  	"time"
    13  
    14  	"github.com/urfave/cli/v2"
    15  )
    16  
    17  // Pause returns a command.
    18  func Pause() *cli.Command {
    19  	return &cli.Command{
    20  		Name:  "pause",
    21  		Usage: "Pause script execution for a given duration.",
    22  		Flags: []cli.Flag{
    23  			&cli.DurationFlag{
    24  				Name:  "for",
    25  				Usage: "The duration to wait for as a duration string",
    26  				Value: 100 * time.Millisecond,
    27  			},
    28  		},
    29  		Action: func(c *cli.Context) error {
    30  			pauseFor := c.Duration("for")
    31  			select {
    32  			case <-c.Context.Done():
    33  				if !c.Bool("quiet") {
    34  					fmt.Println("pause aborting")
    35  				}
    36  				return nil
    37  			case <-time.After(pauseFor):
    38  				if !c.Bool("quiet") {
    39  					fmt.Printf("paused for %v\n", pauseFor)
    40  				}
    41  				return nil
    42  			}
    43  		},
    44  	}
    45  }