Skip to content

Commit 81647f8

Browse files
author
💥Hedi Ghediri
committed
✨registry - Add command line flags to select the desired store
We can now select between the in-memory story and the filesystem one by specifying CLI arguments. ``` ./cdt-registry --help --help Display this message --store string Type of store to use: 'memory' or 'file' (default "memory") --store-path string Path for file store (required when using file store) ```
1 parent ffd0628 commit 81647f8

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

remote-registry/main.go

+84-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,100 @@
11
package main
22

33
import (
4+
"fmt"
45
"log"
56
"net/http"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/spf13/pflag"
11+
"github.com/spf13/viper"
612

713
remote "github.com/criteo/command-launcher/internal/remote"
814
handlers "github.com/criteo/command-launcher/remote-registry/handlers"
915
model "github.com/criteo/command-launcher/remote-registry/model"
1016
. "github.com/criteo/command-launcher/remote-registry/store"
1117
)
1218

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+
1381
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+
1698
initStore(store)
1799

18100
// create a controller instance by specifying the store

0 commit comments

Comments
 (0)