github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/internal/sh/builtin/exit.go (about) 1 package builtin 2 3 import ( 4 "io" 5 "os" 6 "strconv" 7 8 "github.com/madlambda/nash/errors" 9 "github.com/madlambda/nash/sh" 10 ) 11 12 type ( 13 exitFn struct { 14 status int 15 } 16 ) 17 18 func newExit() Fn { 19 return &exitFn{} 20 } 21 22 func (e *exitFn) ArgNames() []sh.FnArg { 23 return []sh.FnArg{ 24 sh.NewFnArg("status", false), 25 } 26 } 27 28 func (e *exitFn) Run(in io.Reader, out io.Writer, err io.Writer) ([]sh.Obj, error) { 29 os.Exit(e.status) 30 return nil, nil //Unrecheable code 31 } 32 33 func (e *exitFn) SetArgs(args []sh.Obj) error { 34 if len(args) != 1 { 35 return errors.NewError("exit expects 1 argument") 36 } 37 38 obj := args[0] 39 if obj.Type() != sh.StringType { 40 return errors.NewError( 41 "exit expects a status string, but a %s was provided", 42 obj.Type(), 43 ) 44 } 45 statusstr := obj.(*sh.StrObj).Str() 46 status, err := strconv.Atoi(statusstr) 47 if err != nil { 48 return errors.NewError( 49 "exit:error[%s] converting status[%s] to int", 50 err, 51 statusstr, 52 ) 53 54 } 55 e.status = status 56 return nil 57 }