|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - _ "embed" |
6 | 4 | "fmt" |
7 | 5 | "os" |
8 | | - "strings" |
9 | 6 |
|
10 | | - log "github.com/sirupsen/logrus" |
11 | | - "github.com/spf13/cobra" |
12 | 7 | "github.com/spf13/cobra/doc" |
13 | 8 |
|
14 | | - "github.com/algorand/conduit/cmd/conduit/internal/initialize" |
15 | | - "github.com/algorand/conduit/cmd/conduit/internal/list" |
16 | | - "github.com/algorand/conduit/conduit/data" |
17 | | - "github.com/algorand/conduit/conduit/loggers" |
18 | | - "github.com/algorand/conduit/conduit/pipeline" |
| 9 | + "github.com/algorand/conduit/pkg/cli" |
| 10 | + |
19 | 11 | _ "github.com/algorand/conduit/conduit/plugins/exporters/all" |
20 | 12 | _ "github.com/algorand/conduit/conduit/plugins/importers/all" |
21 | 13 | _ "github.com/algorand/conduit/conduit/plugins/processors/all" |
22 | | - "github.com/algorand/conduit/version" |
23 | | -) |
24 | | - |
25 | | -var ( |
26 | | - logger *log.Logger |
27 | | - conduitCmd = makeConduitCmd() |
28 | | - //go:embed banner.txt |
29 | | - banner string |
30 | | -) |
31 | | - |
32 | | -const ( |
33 | | - conduitEnvVar = "CONDUIT_DATA_DIR" |
34 | 14 | ) |
35 | 15 |
|
36 | | -// init() function for main package |
37 | | -func init() { |
38 | | - conduitCmd.AddCommand(initialize.InitCommand) |
39 | | - conduitCmd.AddCommand(list.Command) |
40 | | -} |
41 | | - |
42 | | -// runConduitCmdWithConfig run the main logic with a supplied conduit config |
43 | | -func runConduitCmdWithConfig(args *data.Args) error { |
44 | | - defer pipeline.HandlePanic(logger) |
45 | | - |
46 | | - if args.ConduitDataDir == "" { |
47 | | - args.ConduitDataDir = os.Getenv(conduitEnvVar) |
48 | | - } |
49 | | - |
50 | | - if args.ConduitDataDir == "" { |
51 | | - return fmt.Errorf("the data directory is required and must be provided with a command line option or the '%s' environment variable", conduitEnvVar) |
52 | | - } |
53 | | - |
54 | | - pCfg, err := data.MakePipelineConfig(args) |
55 | | - if err != nil { |
56 | | - return err |
57 | | - } |
58 | | - |
59 | | - // Initialize logger |
60 | | - level, err := log.ParseLevel(pCfg.LogLevel) |
61 | | - if err != nil { |
62 | | - var levels []string |
63 | | - for _, l := range log.AllLevels { |
64 | | - levels = append(levels, l.String()) |
65 | | - } |
66 | | - return fmt.Errorf("invalid configuration: '%s' is not a valid log level, valid levels: %s", pCfg.LogLevel, strings.Join(levels, ", ")) |
67 | | - } |
68 | | - |
69 | | - logger, err = loggers.MakeThreadSafeLogger(level, pCfg.LogFile) |
70 | | - if err != nil { |
71 | | - return fmt.Errorf("failed to create logger: %w", err) |
72 | | - } |
73 | | - |
74 | | - logger.Infof("Starting Conduit %s", version.LongVersion()) |
75 | | - logger.Infof("Using data directory: %s", args.ConduitDataDir) |
76 | | - logger.Info("Conduit configuration is valid") |
77 | | - |
78 | | - if !pCfg.HideBanner { |
79 | | - fmt.Print(banner) |
80 | | - } |
81 | | - |
82 | | - if pCfg.LogFile != "" { |
83 | | - fmt.Printf("Writing logs to file: %s\n", pCfg.LogFile) |
84 | | - } else { |
85 | | - fmt.Println("Writing logs to console.") |
86 | | - } |
87 | | - |
88 | | - ctx := context.Background() |
89 | | - pipeline, err := pipeline.MakePipeline(ctx, pCfg, logger) |
90 | | - if err != nil { |
91 | | - err = fmt.Errorf("pipeline creation error: %w", err) |
92 | | - |
93 | | - // Suppress log, it is about to be printed to stderr. |
94 | | - if pCfg.LogFile != "" { |
95 | | - logger.Error(err) |
96 | | - } |
97 | | - return err |
98 | | - } |
99 | | - |
100 | | - err = pipeline.Init() |
101 | | - if err != nil { |
102 | | - // Suppress log, it is about to be printed to stderr. |
103 | | - if pCfg.LogFile != "" { |
104 | | - logger.Error(err) |
105 | | - } |
106 | | - return fmt.Errorf("pipeline init error: %w", err) |
107 | | - } |
108 | | - pipeline.Start() |
109 | | - defer pipeline.Stop() |
110 | | - pipeline.Wait() |
111 | | - return pipeline.Error() |
112 | | -} |
113 | | - |
114 | | -// makeConduitCmd creates the main cobra command, initializes flags |
115 | | -func makeConduitCmd() *cobra.Command { |
116 | | - cfg := &data.Args{} |
117 | | - var vFlag bool |
118 | | - cmd := &cobra.Command{ |
119 | | - Use: "conduit", |
120 | | - Short: "Run the Conduit framework.", |
121 | | - Long: `Conduit is a framework for ingesting blocks from the Algorand blockchain |
122 | | -into external applications. It is designed as a modular plugin system that |
123 | | -allows users to configure their own data pipelines. |
124 | | -
|
125 | | -You must provide a data directory containing a file named conduit.yml. The |
126 | | -file configures pipeline and all enabled plugins. |
127 | | -
|
128 | | -See other subcommands for further built in utilities and information. |
129 | | -
|
130 | | -Detailed documentation is online: https://github.com/algorand/conduit`, |
131 | | - Args: cobra.NoArgs, |
132 | | - Run: func(cmd *cobra.Command, args []string) { |
133 | | - err := runConduitCmdWithConfig(cfg) |
134 | | - if err != nil { |
135 | | - fmt.Fprintf(os.Stderr, "\nExiting with error:\n%s.\n", err) |
136 | | - os.Exit(1) |
137 | | - } |
138 | | - }, |
139 | | - PersistentPreRun: func(cmd *cobra.Command, args []string) { |
140 | | - if vFlag { |
141 | | - fmt.Printf("%s\n", version.LongVersion()) |
142 | | - os.Exit(0) |
143 | | - } |
144 | | - }, |
145 | | - } |
146 | | - cmd.Flags().StringVarP(&cfg.ConduitDataDir, "data-dir", "d", "", "Set the Conduit data directory. If not set the CONDUIT_DATA_DIR environment variable is used.") |
147 | | - cmd.Flags().Uint64VarP(&cfg.NextRoundOverride, "next-round-override", "r", 0, "Set the starting round. Overrides next-round in metadata.json. Some exporters do not support overriding the starting round.") |
148 | | - cmd.Flags().BoolVarP(&vFlag, "version", "v", false, "Print the Conduit version.") |
149 | | - // No need for shell completions. |
150 | | - cmd.CompletionOptions.DisableDefaultCmd = true |
151 | | - |
152 | | - return cmd |
153 | | -} |
154 | | - |
155 | 16 | func main() { |
| 17 | + conduitCmd := cli.MakeConduitCmdWithUtilities() |
| 18 | + |
156 | 19 | // Hidden command to generate docs in a given directory |
157 | 20 | // conduit generate-docs [path] |
158 | 21 | if len(os.Args) == 3 && os.Args[1] == "generate-docs" { |
|
0 commit comments