MCPcopy Create free account
hub / github.com/AdRoll/baker / MainCLI

Function MainCLI

baker_cli.go:29–103  ·  view source on GitHub ↗

MainCLI provides a handy way to quickly create a command-line interface to Baker by providing the list of components available to build and run a topology. The function includes many utilities that can be configured by command line arguments: -help: Prints available options and components -v: verbo

(components Components)

Source from the content-addressed store, hash-verified

27// The function also expects the first non-positional argument to represent the path to
28// the Baker Topology file
29func MainCLI(components Components) error {
30 log.SetFormatter(&log.JSONFormatter{})
31 log.SetOutput(os.Stderr)
32
33 // Create a new flagset to avoid conflict with other libraries
34 flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
35 var (
36 flagHelpConfig = flag.String("help", "", "show help for a `component` (input/filter/output/upload) (use '*' to dump all)")
37 flagVerbose = flag.Bool("v", false, "verbose logging (debug level)")
38 flagQuiet = flag.Bool("q", false, "quiet logging (warn level)")
39 flagPretty = flag.Bool("pretty", false, "human-readable logging (unstructured logging)")
40 flagPProf = flag.String("pprof", "", `run pprof server on host port provided (disabled if ""), use "localhost:" for a free port`)
41 )
42
43 // Seed pseudo-random number generation using seconds since the epoch
44 rand.Seed(time.Now().Unix())
45
46 // Customise program usage message
47 flag.Usage = displayProgramUsage(components)
48
49 flag.Parse()
50
51 if *flagHelpConfig != "" {
52 return RenderHelpMarkdown(os.Stderr, *flagHelpConfig, components)
53 }
54
55 if *flagPProf != "" {
56 addr, err := checkHostPort(*flagPProf)
57 if err != nil {
58 return err
59 }
60 go func() {
61 log.Warnf("running pprof server on %s", addr)
62 http.ListenAndServe(addr, nil)
63 }()
64 }
65
66 if len(flag.Args()) < 1 {
67 flag.Usage()
68 os.Exit(1)
69 }
70
71 if *flagVerbose && *flagQuiet {
72 return fmt.Errorf("logging can't both be verbose and quiet")
73 }
74
75 if *flagVerbose {
76 log.SetLevel(log.DebugLevel)
77 }
78 if *flagQuiet {
79 log.SetLevel(log.WarnLevel)
80 }
81 if *flagPretty {
82 log.SetFormatter(&log.TextFormatter{})
83 }
84
85 f, err := os.Open(flag.Arg(0))
86 if err != nil {

Callers 3

mainFunction · 0.92
mainFunction · 0.92
mainFunction · 0.92

Calls 8

RenderHelpMarkdownFunction · 0.85
checkHostPortFunction · 0.85
NewConfigFromTomlFunction · 0.85
MainFunction · 0.85
displayProgramUsageFunction · 0.70
ParseMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected