main.go 990 Bytes
Newer Older
keks's avatar
keks committed
1 2 3
package main

import (
4
	"context"
keks's avatar
keks committed
5
	"fmt"
keks's avatar
keks committed
6 7
	"os"

tavit ohanian's avatar
tavit ohanian committed
8
	cmds "gitlab.dms3.io/dms3/public/go-dms3-cmds"
tavit ohanian's avatar
tavit ohanian committed
9
	"gitlab.dms3.io/dms3/public/go-dms3-cmds/examples/adder"
keks's avatar
keks committed
10

tavit ohanian's avatar
tavit ohanian committed
11
	"gitlab.dms3.io/dms3/public/go-dms3-cmds/cli"
keks's avatar
keks committed
12 13 14 15
)

func main() {
	// parse the command path, arguments and options from the command line
16
	req, err := cli.Parse(context.TODO(), os.Args[1:], os.Stdin, adder.RootCmd)
keks's avatar
keks committed
17 18 19
	if err != nil {
		panic(err)
	}
keks's avatar
fmt  
keks committed
20

21
	req.Options["encoding"] = cmds.Text
keks's avatar
keks committed
22 23

	// create an emitter
Steven Allen's avatar
Steven Allen committed
24
	cliRe, err := cli.NewResponseEmitter(os.Stdout, os.Stderr, req)
Steven Allen's avatar
Steven Allen committed
25 26 27
	if err != nil {
		panic(err)
	}
keks's avatar
keks committed
28

Steven Allen's avatar
Steven Allen committed
29 30
	wait := make(chan struct{})
	var re cmds.ResponseEmitter = cliRe
31
	if pr, ok := req.Command.PostRun[cmds.CLI]; ok {
keks's avatar
keks committed
32 33 34 35 36 37 38 39
		var (
			res   cmds.Response
			lower = re
		)

		re, res = cmds.NewChanResponsePair(req)

		go func() {
Steven Allen's avatar
Steven Allen committed
40
			defer close(wait)
keks's avatar
keks committed
41 42 43 44 45
			err := pr(res, lower)
			if err != nil {
				fmt.Println("error: ", err)
			}
		}()
Steven Allen's avatar
Steven Allen committed
46 47
	} else {
		close(wait)
keks's avatar
keks committed
48 49
	}

Steven Allen's avatar
Steven Allen committed
50
	adder.RootCmd.Call(req, re, nil)
keks's avatar
keks committed
51
	<-wait
keks's avatar
fmt  
keks committed
52

Steven Allen's avatar
Steven Allen committed
53
	os.Exit(cliRe.Status())
keks's avatar
keks committed
54
}