forked from SpectoLabs/hoverfly
/
root.go
90 lines (66 loc) · 2.17 KB
/
root.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
package cmd
import (
"fmt"
"os"
log "github.com/Sirupsen/logrus"
"github.com/SpectoLabs/hoverfly/hoverctl/configuration"
"github.com/spf13/cobra"
)
var targetNameFlag string
var force, verbose, setDefaultTargetFlag bool
var hoverflyDirectory configuration.HoverflyDirectory
var config *configuration.Config
var target *configuration.Target
var version string
var RootCmd = &cobra.Command{
Use: "hoverctl",
Short: "hoverctl is the command line tool for Hoverfly",
Long: ``,
}
func Execute(hoverctlVersion string) {
version = hoverctlVersion
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
if setDefaultTargetFlag && target != nil {
config.DefaultTarget = target.Name
}
handleIfError(config.WriteToFile(hoverflyDirectory))
}
func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().BoolVar(&force, "force", false,
"Bypass any confirmation when using hoverctl")
RootCmd.Flag("force").Shorthand = "f"
RootCmd.PersistentFlags().StringVar(&targetNameFlag, "target", "",
"A name for an instance of Hoverfly you are trying to communicate with. Overrides the default target (default)")
RootCmd.PersistentFlags().BoolVar(&setDefaultTargetFlag, "set-default", false,
"Sets the current target as the default target for hoverctl")
RootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Verbose logging from hoverctl")
RootCmd.Flag("verbose").Shorthand = "v"
RootCmd.Flag("target").Shorthand = "t"
}
func initConfig() {
log.SetOutput(os.Stdout)
if verbose {
log.SetLevel(log.DebugLevel)
}
configuration.SetConfigurationDefaults()
configuration.SetConfigurationPaths()
config = configuration.GetConfig()
if config.GetTarget(config.DefaultTarget) == nil {
fmt.Printf("Default target `%v` not found, changing default target to `local`", config.DefaultTarget)
config.DefaultTarget = "local"
}
target = config.GetTarget(targetNameFlag)
if targetNameFlag == "" && target == nil {
target = configuration.NewDefaultTarget()
}
if verbose && target != nil {
fmt.Println("Current target: " + target.Name + "\n")
}
var err error
hoverflyDirectory, err = configuration.NewHoverflyDirectory(*config)
handleIfError(err)
}