Skip to content

Commit 56a0131

Browse files
authored
feat: ability to set config key or config file from root cmd (#502)
This change introduces the ability to set the `config-key` or `config-file` as a flag when running any of the commands that require loading the config.
1 parent 08e67cf commit 56a0131

File tree

8 files changed

+82
-34
lines changed

8 files changed

+82
-34
lines changed

service/cmd/migrate.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ var (
1919
Use: "down",
2020
Short: "Run database migration down one version",
2121
Run: func(cmd *cobra.Command, args []string) {
22-
dbClient, err := migrateDBClient()
22+
configFile, _ := cmd.Flags().GetString(configFileFlag)
23+
configKey, _ := cmd.Flags().GetString(configKeyFlag)
24+
cfg, err := config.LoadConfig(configKey, configFile)
25+
if err != nil {
26+
panic(fmt.Errorf("could not load config: %w", err))
27+
}
28+
29+
dbClient, err := migrateDBClient(cfg)
2330
if err != nil {
2431
panic(fmt.Errorf("could not load config: %w", err))
2532
}
@@ -35,7 +42,13 @@ var (
3542
Use: "up",
3643
Short: "Run database migrations up to the latest version",
3744
Run: func(cmd *cobra.Command, _ []string) {
38-
dbClient, err := migrateDBClient()
45+
configFile, _ := cmd.Flags().GetString(configFileFlag)
46+
configKey, _ := cmd.Flags().GetString(configKeyFlag)
47+
cfg, err := config.LoadConfig(configKey, configFile)
48+
if err != nil {
49+
panic(fmt.Errorf("could not load config: %w", err))
50+
}
51+
dbClient, err := migrateDBClient(cfg)
3952
if err != nil {
4053
panic(fmt.Errorf("could not load config: %w", err))
4154
}
@@ -52,7 +65,13 @@ var (
5265
Use: "status",
5366
Short: "Show the status of the database migrations",
5467
Run: func(cmd *cobra.Command, args []string) {
55-
dbClient, err := migrateDBClient()
68+
configFile, _ := cmd.Flags().GetString(configFileFlag)
69+
configKey, _ := cmd.Flags().GetString(configKeyFlag)
70+
cfg, err := config.LoadConfig(configKey, configFile)
71+
if err != nil {
72+
panic(fmt.Errorf("could not load config: %w", err))
73+
}
74+
dbClient, err := migrateDBClient(cfg)
5675
if err != nil {
5776
panic(fmt.Errorf("could not load config: %w", err))
5877
}
@@ -68,13 +87,7 @@ var (
6887
}
6988
)
7089

71-
func migrateDBClient() (*db.Client, error) {
72-
// Load the config
73-
conf, err := config.LoadConfig("opentdf")
74-
if err != nil {
75-
return nil, err
76-
}
77-
90+
func migrateDBClient(conf *config.Config) (*db.Client, error) {
7891
slog.Info("creating database client")
7992
dbClient, err := db.NewClient(conf.DB)
8093
if err != nil {

service/cmd/policy.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ var (
2929
Long: policyFqnReindexCmdLong,
3030

3131
Run: func(cmd *cobra.Command, args []string) {
32-
dbClient, err := policyDBClient()
32+
configFile, _ := cmd.Flags().GetString(configFileFlag)
33+
configKey, _ := cmd.Flags().GetString(configKeyFlag)
34+
cfg, err := config.LoadConfig(configKey, configFile)
35+
if err != nil {
36+
panic(fmt.Errorf("could not load config: %w", err))
37+
}
38+
dbClient, err := policyDBClient(cfg)
3339
if err != nil {
3440
panic(fmt.Errorf("could not load config: %w", err))
3541
}
@@ -53,13 +59,7 @@ var (
5359
}
5460
)
5561

56-
func policyDBClient() (*policydb.PolicyDBClient, error) {
57-
// Load the config
58-
conf, err := config.LoadConfig("opentdf")
59-
if err != nil {
60-
return nil, err
61-
}
62-
62+
func policyDBClient(conf *config.Config) (*policydb.PolicyDBClient, error) {
6363
slog.Info("creating database client")
6464
dbClient, err := db.NewClient(conf.DB)
6565
if err != nil {

service/cmd/provisionFixtures.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ to run this command in a clean database. This command is intended for local deve
3535
** Teardown or Issues **
3636
You can clear/recycle your database with 'docker-compose down' and 'docker-compose up' to start fresh.`,
3737
Run: func(cmd *cobra.Command, args []string) {
38-
cfg, err := config.LoadConfig("opentdf")
38+
configFile, _ := cmd.Flags().GetString(configFileFlag)
39+
configKey, _ := cmd.Flags().GetString(configKeyFlag)
40+
cfg, err := config.LoadConfig(configKey, configFile)
3941
if err != nil {
4042
panic(fmt.Errorf("could not load config: %w", err))
4143
}

service/cmd/provisionKeyloak.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ var (
6767
realmName, _ := cmd.Flags().GetString(provKcRealm)
6868
kcUsername, _ := cmd.Flags().GetString(provKcUsername)
6969
kcPassword, _ := cmd.Flags().GetString(provKcPassword)
70+
configFile, _ := cmd.Flags().GetString(configFileFlag)
71+
configKey, _ := cmd.Flags().GetString(configKeyFlag)
7072

7173
kcConnectParams := keycloakConnectParams{
7274
BasePath: kcEndpoint,
@@ -76,7 +78,7 @@ var (
7678
AllowInsecureTLS: true,
7779
}
7880

79-
config, err := config.LoadConfig("")
81+
config, err := config.LoadConfig(configKey, configFile)
8082
if err != nil {
8183
return err
8284
}

service/cmd/root.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ user authorization, and performing access checks. Use this tool to start, stop,
1919
manage, configure, or upgrade one or more of the OpenTDF Platform services.`,
2020
}
2121

22+
var (
23+
configFileFlag = "config-file"
24+
configKeyFlag = "config-key"
25+
)
26+
27+
func init() {
28+
rootCmd.PersistentFlags().StringP(configFileFlag, "", "", "custom configuration file location")
29+
rootCmd.PersistentFlags().StringP(configKeyFlag, "", "opentdf", "the key is the name of the configuration file without the extension")
30+
}
31+
2232
// Execute adds all child commands to the root command and sets flags appropriately.
2333
// This is called by main.main(). It only needs to happen once to the rootCmd.
2434
func Execute() {

service/cmd/start.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ func init() {
1515
rootCmd.AddCommand(&startCmd)
1616
}
1717

18-
func start(_ *cobra.Command, _ []string) error {
19-
return server.Start(server.WithWaitForShutdownSignal())
18+
func start(cmd *cobra.Command, _ []string) error {
19+
configFile, _ := cmd.Flags().GetString(configFileFlag)
20+
configKey, _ := cmd.Flags().GetString(configKeyFlag)
21+
22+
return server.Start(
23+
server.WithWaitForShutdownSignal(),
24+
server.WithConfigFile(configFile),
25+
server.WithConfigKey(configKey),
26+
)
2027
}

service/internal/config/config.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"errors"
55
"fmt"
6-
"log/slog"
76
"os"
87
"strings"
98

@@ -35,14 +34,7 @@ const (
3534
)
3635

3736
// Load config with viper.
38-
func LoadConfig(key string) (*Config, error) {
39-
if key == "" {
40-
key = "opentdf"
41-
slog.Info("LoadConfig: key not provided, using default", "config", key)
42-
} else {
43-
slog.Info("LoadConfig", "config", key)
44-
}
45-
37+
func LoadConfig(key string, file string) (*Config, error) {
4638
config := &Config{}
4739
homedir, err := os.UserHomeDir()
4840
if err != nil {
@@ -58,6 +50,12 @@ func LoadConfig(key string) (*Config, error) {
5850
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
5951
viper.AutomaticEnv()
6052

53+
// Allow for a custom config file to be passed in
54+
// This takes precedence over the AddConfigPath/SetConfigName
55+
if file != "" {
56+
viper.SetConfigFile(file)
57+
}
58+
6159
if err := viper.ReadInConfig(); err != nil {
6260
return nil, errors.Join(err, ErrLoadingConfig)
6361
}

service/pkg/server/start.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,24 @@ import (
2020

2121
type StartOptions func(StartConfig) StartConfig
2222

23+
// Deprecated: Use WithConfigKey
2324
func WithConfigName(name string) StartOptions {
2425
return func(c StartConfig) StartConfig {
25-
c.ConfigName = name
26+
c.ConfigKey = name
27+
return c
28+
}
29+
}
30+
31+
func WithConfigFile(file string) StartOptions {
32+
return func(c StartConfig) StartConfig {
33+
c.ConfigFile = file
34+
return c
35+
}
36+
}
37+
38+
func WithConfigKey(key string) StartOptions {
39+
return func(c StartConfig) StartConfig {
40+
c.ConfigKey = key
2641
return c
2742
}
2843
}
@@ -35,7 +50,8 @@ func WithWaitForShutdownSignal() StartOptions {
3550
}
3651

3752
type StartConfig struct {
38-
ConfigName string
53+
ConfigKey string
54+
ConfigFile string
3955
WaitForShutdownSignal bool
4056
}
4157

@@ -50,7 +66,7 @@ func Start(f ...StartOptions) error {
5066
slog.Info("starting opentdf services")
5167

5268
slog.Info("loading configuration")
53-
conf, err := config.LoadConfig(startConfig.ConfigName)
69+
conf, err := config.LoadConfig(startConfig.ConfigKey, startConfig.ConfigFile)
5470
if err != nil {
5571
return fmt.Errorf("could not load config: %w", err)
5672
}

0 commit comments

Comments
 (0)