-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
225 lines (196 loc) · 5.55 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime/pprof"
"strings"
"syscall"
"github.com/nokia/ntt/internal/env"
"github.com/nokia/ntt/internal/log"
"github.com/nokia/ntt/internal/proc"
"github.com/nokia/ntt/project"
"github.com/spf13/cobra"
)
var (
RootCommand = &cobra.Command{
Use: "ntt",
Short: "ntt is a tool for managing TTCN-3 source code and tests",
// Support for custom commands
SilenceErrors: true,
SilenceUsage: true,
DisableFlagParsing: true,
DisableFlagsInUseLine: true,
Args: cobra.ArbitraryArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if lvl := Verbosity(); lvl != log.GlobalLevel() {
log.SetGlobalLevel(Verbosity())
}
if chdir != "" {
if err := os.Chdir(chdir); err != nil {
return fmt.Errorf("chdir: %w", err)
}
log.Debugf("chdir: %s", chdir)
}
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
return err
}
if err := pprof.StartCPUProfile(f); err != nil {
return err
}
}
// If we have a k3 installtion, we'll prepend libexec before PATH
conf, err := project.NewConfig(project.WithK3())
if conf != nil && conf.K3.Root != "" {
os.Setenv("PATH", fmt.Sprintf("%s%c%s", filepath.Join(conf.K3.Root, "libexec"), os.PathListSeparator, os.Getenv("PATH")))
}
// Skip opening the project if we're running a custom command or version.
if cmd.Use == "ntt" || cmd.Use == "version" || cmd.Use == "stdout" || strings.HasPrefix(cmd.Use, "help") || cmd.Use == "docs" || cmd.Use == "objdump" || cmd.Use == "t3xfasm" {
// first arg is either an external subkommand of the form
// k3-Arg[0] or ntt-Arg[0] or unknown
return nil
}
files, _ := splitArgs(args, cmd.ArgsLenAtDash())
p, err := project.Open(files...)
if err != nil {
return err
}
Project = p
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 && args[0][0] != '-' {
if path, err := exec.LookPath("ntt-" + args[0]); err == nil {
return proc.Exec(path, args[1:]...)
}
if path, err := exec.LookPath("k3-" + args[0]); err == nil {
return proc.Exec(path, args[1:]...)
}
return fmt.Errorf("ntt: unknown command: %s", args[0])
}
if err := cmd.Flags().Parse(args); err != nil {
return err
}
if interactive, _ := cmd.Flags().GetBool("interactive"); interactive {
return repl()
}
return cmd.Help()
},
}
verbose int
ShSetup bool
dumb bool
outputQuiet bool
outputJSON bool
outputPlain bool
outputProgress bool
outputTAP bool
testsFiles []string
chdir string
version = "devel"
commit = "none"
date = "unknown"
cpuprofile = ""
Project *project.Config
)
func init() {
root := RootCommand
flags := root.PersistentFlags()
flags.CountVarP(&verbose, "verbose", "v", "verbose output")
flags.BoolVarP(&outputQuiet, "quiet", "q", false, "quiet output")
flags.BoolVarP(&outputJSON, "json", "", false, "output in JSON format")
flags.BoolVarP(&outputPlain, "plain", "", false, "output in plain format (for grep and awk)")
RunCommand.PersistentFlags().BoolVarP(&outputTAP, "tap", "", false, "output in test anything (TAP) format")
flags.StringVarP(&cpuprofile, "cpuprofile", "", "", "write cpu profile to `file`")
flags.StringVarP(&chdir, "chdir", "C", "", "change to DIR before doing anything else")
RootCommand.Flags().BoolP("interactive", "i", false, "run in interactive mode")
root.AddCommand(BuildCommand)
root.AddCommand(CompileCommand)
root.AddCommand(DumpCommand)
root.AddCommand(FormatCommand)
root.AddCommand(LangserverCommand)
root.AddCommand(LintCommand)
root.AddCommand(ListCommand)
root.AddCommand(LocateFileCommand)
root.AddCommand(ReportCommand)
root.AddCommand(RunCommand)
root.AddCommand(ShowCommand)
root.AddCommand(TagsCommand)
root.AddCommand(ObjdumpCommand)
root.AddCommand(T3xfasmCommand)
ShowCommand.PersistentFlags().BoolVarP(&ShSetup, "sh", "", false, "output test suite data for shell consumption")
ShowCommand.PersistentFlags().BoolVarP(&dumb, "dumb", "", false, "do not evaluate testcase configuration")
}
func Format() string {
switch {
case outputQuiet:
return "quiet"
case outputPlain:
return "plain"
case outputJSON:
return "json"
case outputProgress:
return "progress"
case outputTAP:
return "tap"
case outputTTCN3:
return "ttcn3"
case outputDot:
return "dot"
default:
return "text"
}
}
func Verbosity() log.Level {
switch {
case env.Getenv("NTT_TRACE") != "":
return log.TraceLevel
case env.Getenv("NTT_DEBUG") != "":
return log.DebugLevel
case outputQuiet:
return log.DisabledLevel
default:
lvl := log.PrintLevel + log.Level(verbose)
if lvl > log.TraceLevel {
lvl = log.TraceLevel
}
return lvl
}
}
func main() {
defer log.Close()
err := RootCommand.Execute()
if cpuprofile != "" {
pprof.StopCPUProfile()
}
if err != nil {
fatal(err)
}
}
// PrintError is a utility function that prints a list of errors to w,
// one error per line, if the err parameter is an ErrorList. Otherwise
// it prints the err string.
func PrintError(w io.Writer, err error) {
fmt.Fprintf(w, "%s\n", err)
}
func fatal(err error) {
switch err := err.(type) {
case *exec.ExitError:
waitStatus := err.Sys().(syscall.WaitStatus)
if waitStatus.ExitStatus() == -1 {
PrintError(os.Stderr, err)
}
os.Exit(waitStatus.ExitStatus())
default:
// Run command has its own error logging.
if !errors.Is(err, ErrCommandFailed) {
fmt.Fprintln(os.Stderr, err.Error())
}
}
os.Exit(1)
}