|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "log"
|
5 | 6 | "net/http"
|
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/spf13/pflag" |
| 11 | + "github.com/spf13/viper" |
6 | 12 |
|
7 | 13 | remote "github.com/criteo/command-launcher/internal/remote"
|
8 | 14 | handlers "github.com/criteo/command-launcher/remote-registry/handlers"
|
9 | 15 | model "github.com/criteo/command-launcher/remote-registry/model"
|
10 | 16 | . "github.com/criteo/command-launcher/remote-registry/store"
|
11 | 17 | )
|
12 | 18 |
|
| 19 | +type CommandLineArgs struct { |
| 20 | + StoreType string |
| 21 | + StorePath string |
| 22 | + ShowHelp bool |
| 23 | +} |
| 24 | + |
| 25 | +func setupCommandLineArgs() (*CommandLineArgs, error) { |
| 26 | + pflag.String("store", "memory", "Type of store to use: 'memory' or 'filesystem'") |
| 27 | + pflag.String("store-path", "", "Path for file store (required when using filesystem store)") |
| 28 | + pflag.Bool("help", false, "Display this message") |
| 29 | + pflag.Parse() |
| 30 | + |
| 31 | + if err := viper.BindPFlags(pflag.CommandLine); err != nil { |
| 32 | + return nil, fmt.Errorf("failed to bind flags: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + viper.SetDefault("store", "memory") |
| 36 | + |
| 37 | + viper.SetEnvPrefix("REGISTRY") |
| 38 | + viper.AutomaticEnv() |
| 39 | + |
| 40 | + return &CommandLineArgs{ |
| 41 | + StoreType: viper.GetString("store"), |
| 42 | + StorePath: viper.GetString("store-path"), |
| 43 | + ShowHelp: viper.GetBool("help"), |
| 44 | + }, nil |
| 45 | +} |
| 46 | + |
| 47 | +func createStore(config *CommandLineArgs) (Store, error) { |
| 48 | + var store Store |
| 49 | + var err error |
| 50 | + |
| 51 | + switch config.StoreType { |
| 52 | + case "memory": |
| 53 | + log.Println("Using in-memory store") |
| 54 | + store = NewInMemoryStore() |
| 55 | + case "filesystem": |
| 56 | + storePath := config.StorePath |
| 57 | + if storePath == "" { |
| 58 | + log.Printf("No path specified, using default working directory") |
| 59 | + wd, err := os.Getwd() |
| 60 | + if err == nil { |
| 61 | + storePath = filepath.Join(wd, "registry-store") |
| 62 | + } else { |
| 63 | + return nil, fmt.Errorf("failed to get working directory: %w", err) |
| 64 | + } |
| 65 | + log.Printf("Using path: %s", storePath) |
| 66 | + } |
| 67 | + |
| 68 | + log.Printf("Using filesystem store at: %s", storePath) |
| 69 | + store, err = NewFileStore(storePath) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("failed to create filesystem store: %w", err) |
| 72 | + } |
| 73 | + log.Printf("Filesystem store created at %s", storePath) |
| 74 | + default: |
| 75 | + return nil, fmt.Errorf("unknown store type: %s", config.StoreType) |
| 76 | + } |
| 77 | + |
| 78 | + return store, nil |
| 79 | +} |
| 80 | + |
13 | 81 | func main() {
|
14 |
| - // create a new in-memory store |
15 |
| - store := NewInMemoryStore() |
| 82 | + config, err := setupCommandLineArgs() |
| 83 | + if err != nil { |
| 84 | + log.Fatalf("Failed to set up configuration: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + if config.ShowHelp { |
| 88 | + pflag.PrintDefaults() |
| 89 | + os.Exit(0) |
| 90 | + } |
| 91 | + |
| 92 | + // Create the appropriate store |
| 93 | + store, err := createStore(config) |
| 94 | + if err != nil { |
| 95 | + log.Fatalf("Failed to create store: %v", err) |
| 96 | + } |
| 97 | + |
16 | 98 | initStore(store)
|
17 | 99 |
|
18 | 100 | // create a controller instance by specifying the store
|
|
0 commit comments