forked from grafana/k6
/
inspect.go
103 lines (93 loc) · 2.5 KB
/
inspect.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/loadimpact/k6/js"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/loader"
)
// inspectCmd represents the resume command
var inspectCmd = &cobra.Command{
Use: "inspect [file]",
Short: "Inspect a script or archive",
Long: `Inspect a script or archive.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
filesystems := loader.CreateFilesystems()
// TODO: don't use the Global logger
src, err := loader.ReadSource(logrus.StandardLogger(), args[0], pwd, filesystems, os.Stdin)
if err != nil {
return err
}
typ := runType
if typ == "" {
typ = detectType(src.Data)
}
runtimeOptions, err := getRuntimeOptions(cmd.Flags(), buildEnvMap(os.Environ()))
if err != nil {
return err
}
var (
opts lib.Options
b *js.Bundle
)
switch typ {
case typeArchive:
var arc *lib.Archive
arc, err = lib.ReadArchive(bytes.NewBuffer(src.Data))
if err != nil {
return err
}
b, err = js.NewBundleFromArchive(arc, runtimeOptions)
if err != nil {
return err
}
opts = b.Options
case typeJS:
b, err = js.NewBundle(src, filesystems, runtimeOptions)
if err != nil {
return err
}
opts = b.Options
}
data, err := json.MarshalIndent(opts, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
},
}
func init() {
RootCmd.AddCommand(inspectCmd)
inspectCmd.Flags().SortFlags = false
inspectCmd.Flags().AddFlagSet(runtimeOptionFlagSet(false))
inspectCmd.Flags().StringVarP(&runType, "type", "t", runType, "override file `type`, \"js\" or \"archive\"")
}