|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/d-Rickyy-b/certstream-server-go/internal/certstream" |
| 11 | + "github.com/d-Rickyy-b/certstream-server-go/internal/config" |
| 12 | +) |
| 13 | + |
| 14 | +// rootCmd represents the base command when called without any subcommands |
| 15 | +var rootCmd = &cobra.Command{ |
| 16 | + Use: "certstream-server-go", |
| 17 | + Short: "A drop-in replacement for the certstream server by Calidog", |
| 18 | + Long: `This tool aggregates, parses, and streams certificate data from multiple |
| 19 | +certificate transparency logs via websocket connections to connected clients.`, |
| 20 | + |
| 21 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 22 | + // Handle --version flag |
| 23 | + versionBool, err := cmd.Flags().GetBool("version") |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + if versionBool { |
| 28 | + fmt.Printf("certstream-server-go v%s\n", config.Version) |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + // Handle --config flag |
| 33 | + configPath, err := cmd.Flags().GetString("config") |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + // Check if path exists and is a file |
| 38 | + _, statErr := os.Stat(configPath) |
| 39 | + if os.IsNotExist(statErr) { |
| 40 | + return fmt.Errorf("config file '%s' does not exist", configPath) |
| 41 | + } |
| 42 | + |
| 43 | + cs, err := certstream.NewCertstreamFromConfigFile(configPath) |
| 44 | + if err != nil { |
| 45 | + log.Fatalf("Error while creating certstream server: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + cs.Start() |
| 49 | + |
| 50 | + return nil |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 55 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 56 | +func Execute() { |
| 57 | + err := rootCmd.Execute() |
| 58 | + if err != nil { |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func init() { |
| 64 | + rootCmd.PersistentFlags().StringP("config", "c", "config.yml", "Path to the config file") |
| 65 | + rootCmd.Flags().BoolP("version", "v", false, "Print the version and exit") |
| 66 | +} |
0 commit comments