-
Notifications
You must be signed in to change notification settings - Fork 2
/
wireframe_main.go
137 lines (114 loc) · 3.14 KB
/
wireframe_main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
////////////////////////////////////////////////////////////////////////////
// Program: wireframe
// Purpose: wire framing
// Authors: Tong Sun (c) 2017, All rights reserved
////////////////////////////////////////////////////////////////////////////
package main
//go:generate sh -v wireframe_cliGen.sh
import (
"fmt"
"os"
"strings"
"github.com/labstack/gommon/color"
"github.com/mkideal/cli"
)
////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions
// The OptsT type defines all the configurable options from cli.
type OptsT struct {
Self *rootT
Host string
Port int
Daemonize bool
Verbose int
}
////////////////////////////////////////////////////////////////////////////
// Global variables definitions
var (
progname = "wireframe"
version = "0.1.0"
date = "2017-09-04"
rootArgv *rootT
// Opts store all the configurable options
Opts OptsT
)
////////////////////////////////////////////////////////////////////////////
// Function definitions
// Function main
func main() {
// cli.SetUsageStyle(cli.ManualStyle) // up-down, for left-right, use NormalStyle
cli.SetUsageStyle(cli.DenseNormalStyle) // for left-right
//NOTE: You can set any writer implements io.Writer
// default writer is os.Stdout
if err := cli.Root(root,
cli.Tree(putDef),
cli.Tree(getDef)).Run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println("")
}
//==========================================================================
// Main dispatcher
func Wireframe(ctx *cli.Context) error {
ctx.JSON(ctx.RootArgv())
ctx.JSON(ctx.Argv())
fmt.Println()
return nil
}
// SUPPORT-FUNCTIONS
//==========================================================================
// support functions
/*
a, b := 2, 3
max := iif(a > b, a, b).(int)
*/
func iif(condition bool, trueVal, falseVal interface{}) interface{} {
if condition {
return trueVal
}
return falseVal
}
// Abs returns the absolute value of x.
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}
// Basename returns the file name without extension.
func Basename(s string) string {
n := strings.LastIndexByte(s, '.')
if n > 0 {
return s[:n]
}
return s
}
// IsExist checks if the given file exist
func IsExist(fileName string) bool {
_, err := os.Stat(fileName)
return err == nil || os.IsExist(err)
}
func warning(m string) {
fmt.Fprintf(os.Stderr, "[%s] %s: %s\n", progname, color.Yellow("Warning"), m)
}
func warnOn(errCase string, e error) {
if e != nil {
fmt.Fprintf(os.Stderr, "[%s] %s, %s: %v\n",
color.White(progname), color.Yellow("Error"), errCase, e)
}
}
// abortOn will quit on anticipated errors gracefully without stack trace
func abortOn(errCase string, e error) {
if e != nil {
fmt.Fprintf(os.Stderr, "[%s] %s, %s: %v\n",
color.White(progname), color.Red("Error"), errCase, e)
os.Exit(1)
}
}
// verbose will print info to stderr according to the verbose level setting
func verbose(levelSet int, format string, args ...interface{}) {
if Opts.Verbose >= levelSet {
fmt.Fprintf(os.Stderr, "[%s] ", color.White(progname))
fmt.Fprintf(os.Stderr, format+"\n", args...)
}
}