This repository has been archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facade.go
111 lines (99 loc) · 2.18 KB
/
facade.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
101
102
103
104
105
106
107
108
109
110
111
package facade
import (
"os"
"runtime"
"github.com/spf13/cobra"
"github.com/spiegel-im-spiegel/gocli"
"github.com/spiegel-im-spiegel/reldemo"
)
const (
//Name is applicatin name
Name = "godump"
//Version is version for applicatin
Version = "v0.0.x"
)
//ExitCode is OS exit code enumeration class
type ExitCode int
const (
//Normal is OS exit code "normal"
Normal ExitCode = iota
//Abnormal is OS exit code "abnormal"
Abnormal
)
//Int convert integer value
func (c ExitCode) Int() int {
return int(c)
}
//Stringer method
func (c ExitCode) String() string {
switch c {
case Normal:
return "normal end"
case Abnormal:
return "abnormal end"
default:
return "unknown"
}
}
var (
cui = gocli.NewUI()
versionFlag = false
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: Name + " [flags] [binary file]",
RunE: func(cmd *cobra.Command, args []string) error {
if versionFlag {
cui.Outputln(Name, Version)
return nil
}
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
reader := cui.Reader()
if len(args) > 0 {
file, err2 := os.Open(args[0]) //args[0] is maybe file path
if err2 != nil {
return err
}
defer file.Close()
reader = file
}
result, err := reldemo.DumpBytes(reader, name)
if err != nil {
return err
}
cui.WriteFrom(result)
return nil
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(ui *gocli.UI) (exit ExitCode) {
defer func() {
//panic hundling
if r := recover(); r != nil {
cui.OutputErrln("Panic:", r)
for depth := 0; ; depth++ {
pc, _, line, ok := runtime.Caller(depth)
if !ok {
break
}
cui.OutputErrln(" ->", depth, ":", runtime.FuncForPC(pc).Name(), ": line", line)
}
exit = Abnormal
}
}()
//execution
exit = Normal
cui = ui
if err := rootCmd.Execute(); err != nil {
exit = Abnormal
}
return
}
func init() {
rootCmd.Flags().BoolVarP(&versionFlag, "vaersion", "v", false, "output version")
rootCmd.Flags().StringP("name", "n", "dumpList", "value name")
}