forked from hwaf/hwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_dump_env.go
100 lines (82 loc) · 1.97 KB
/
cmd_dump_env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
// "path/filepath"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
func hwaf_make_cmd_dump_env() *commander.Command {
cmd := &commander.Command{
Run: hwaf_run_cmd_dump_env,
UsageLine: "dump-env [options]",
Short: "print the environment on STDOUT",
Long: `
dump-env prints the environment on STDOUT.
ex:
$ hwaf dump-env
$ hwaf dump-env -shell=sh
$ hwaf dump-env -shell=csh
`,
Flag: *flag.NewFlagSet("hwaf-dump-env", flag.ExitOnError),
//CustomFlags: true,
}
cmd.Flag.Bool("v", false, "enable verbose output")
cmd.Flag.String("shell", "", "type of shell to print the environment for (default=sh)")
return cmd
}
func hwaf_run_cmd_dump_env(cmd *commander.Command, args []string) {
var err error
n := "hwaf-" + cmd.Name()
switch len(args) {
case 0:
// ok
default:
err = fmt.Errorf("%s: does not take any argument\n", n)
handle_err(err)
}
var export_var func(k string) string
shell := cmd.Flag.Lookup("shell").Value.Get().(string)
//verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
if shell == "" {
shell = "sh"
}
switch shell {
default:
err = fmt.Errorf("%s: shell of type [%s] is unknown", n, shell)
handle_err(err)
case "sh":
export_var = func(k string) string { return fmt.Sprintf("export %s=", k) }
case "csh":
export_var = func(k string) string { return fmt.Sprintf("setenv %s ", k) }
}
if export_var == nil {
err = fmt.Errorf("%s: could find a suitable shell", n)
}
bin, err := exec.LookPath(os.Args[0])
handle_err(err)
buf := new(bytes.Buffer)
waf := exec.Command(
bin, "waf", "dump-env",
)
waf.Stdin = os.Stdin
waf.Stdout = buf
waf.Stderr = os.Stderr
err = waf.Run()
handle_err(err)
env := make(map[string]string)
err = json.Unmarshal(buf.Bytes(), &env)
handle_err(err)
//fmt.Printf("%v\n", env)
for k, v := range env {
if k == "_" || k == "PS1" {
continue
}
fmt.Printf("%s%q\n", export_var(k), v)
}
return
}
// EOF