Merge pull request #153 from monstermunchkin/issues/149-timeout

main: Add --timeout flag
This commit is contained in:
Stéphane Graber 2019-03-06 14:34:53 -05:00 committed by GitHub
commit d859adc687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -61,6 +61,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
@ -73,6 +74,7 @@ type cmdGlobal struct {
flagCleanup bool
flagCacheDir string
flagOptions []string
flagTimeout uint
definition *shared.Definition
sourceDir string
@ -113,6 +115,8 @@ func main() {
"", "Cache directory"+"``")
app.PersistentFlags().StringSliceVarP(&globalCmd.flagOptions, "options", "o",
[]string{}, "Override options (list of key=value)"+"``")
app.PersistentFlags().UintVarP(&globalCmd.flagTimeout, "timeout", "t", 0,
"Timeout in seconds"+"``")
// LXC sub-commands
LXCCmd := cmdLXC{global: &globalCmd}
@ -128,6 +132,18 @@ func main() {
buildDirCmd := cmdBuildDir{global: &globalCmd}
app.AddCommand(buildDirCmd.command())
// Timeout handler
go func() {
// No timeout set
if globalCmd.flagTimeout == 0 {
return
}
time.Sleep(time.Duration(globalCmd.flagTimeout) * time.Second)
fmt.Println("Timed out")
os.Exit(1)
}()
// Run the main command and handle errors
err := app.Execute()
if err != nil {