github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/commands/graph.go (about) 1 package commands 2 3 import ( 4 "os" 5 6 "github.com/observiq/carbon/agent" 7 "github.com/observiq/carbon/operator" 8 pg "github.com/observiq/carbon/operator" 9 "github.com/spf13/cobra" 10 "go.uber.org/zap" 11 "go.uber.org/zap/zapcore" 12 ) 13 14 // GraphFlags are the flags that can be supplied when running the graph command 15 type GraphFlags struct { 16 *RootFlags 17 } 18 19 // NewGraphCommand creates a command for printing the pipeline as a graph 20 func NewGraphCommand(rootFlags *RootFlags) *cobra.Command { 21 return &cobra.Command{ 22 Use: "graph", 23 Args: cobra.NoArgs, 24 Short: "Export a dot-formatted representation of the operator graph", 25 Run: func(command *cobra.Command, args []string) { runGraph(command, args, rootFlags) }, 26 } 27 } 28 29 func runGraph(_ *cobra.Command, _ []string, flags *RootFlags) { 30 var logger *zap.SugaredLogger 31 if flags.Debug { 32 logger = newDefaultLoggerAt(zapcore.DebugLevel, "") 33 } else { 34 logger = newDefaultLoggerAt(zapcore.InfoLevel, "") 35 } 36 defer func() { 37 _ = logger.Sync() 38 }() 39 40 cfg, err := agent.NewConfigFromGlobs(flags.ConfigFiles) 41 if err != nil { 42 logger.Errorw("Failed to read configs from glob", zap.Any("error", err)) 43 os.Exit(1) 44 } 45 46 pluginRegistry, err := operator.NewPluginRegistry(flags.PluginDir) 47 if err != nil { 48 logger.Errorw("Failed to load plugin registry", zap.Any("error", err)) 49 } 50 51 buildContext := pg.BuildContext{ 52 PluginRegistry: pluginRegistry, 53 Logger: logger, 54 } 55 56 pipeline, err := cfg.Pipeline.BuildPipeline(buildContext) 57 if err != nil { 58 logger.Errorw("Failed to build operator pipeline", zap.Any("error", err)) 59 os.Exit(1) 60 } 61 62 dotGraph, err := pipeline.MarshalDot() 63 if err != nil { 64 logger.Errorw("Failed to marshal dot graph", zap.Any("error", err)) 65 os.Exit(1) 66 } 67 68 dotGraph = append(dotGraph, '\n') 69 _, err = stdout.Write(dotGraph) 70 if err != nil { 71 logger.Errorw("Failed to write dot graph to stdout", zap.Any("error", err)) 72 os.Exit(1) 73 } 74 }