-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server/v2): add config & start helper #20505
Changes from 143 commits
d530e80
0a15db8
8455318
62ded17
5c4a43a
decf4a2
dbf509e
d845064
ed638cd
cb2cc01
da38aff
2691838
faba9d1
35fd591
e394e18
cce35fc
d2241fa
15619d9
ca160d8
65db2cf
7308c68
2a12963
a7503e4
1eac3b2
2c3ce95
aa91391
d3b3a24
05942a0
3fd6eb3
a9ef4e4
290263f
00f6999
a63762e
4b86a66
04b8f98
73e00c0
e9e8ab8
b16d2bc
330b8ed
fc684c5
108978d
be3b27b
24e019b
2ca0c70
b427037
02947fa
5bf88be
e55810e
b50f50c
b029625
cbab735
2738109
92539ec
c95c217
bb09616
bb1c1cc
f1b9a29
c529edd
3e8ce5a
27a75ba
4b125e1
8b2025c
6eda0c7
bf16d79
f235226
87d1c5b
aa21e93
52b883e
acfb730
aa0cebd
fd34264
9557e39
cadd5e1
2a2b3f1
98ff414
d6e93ec
0379c26
bf3cc8a
2bb2b90
ce36a1c
b9be347
647d92b
44c465b
faf1450
a2312d0
591bb8e
bd7efda
f9c110d
83f873a
6d2c424
88c260a
4588c33
6d6cf45
128794b
1c074d3
6f36227
2ad7cbf
5a9e899
6db27f3
417029d
7d72206
0a5b6d0
4f54b95
2759dab
fb32eaf
c6f6902
64a51f9
f59e682
99bc320
8ebdc82
eb6d1f6
921b627
d10b7dd
83440d8
08cf9af
68c61d5
959542e
88a2a0b
20ef626
e7178c6
14419bd
6ed4b0f
343258c
8c1e2d2
d914de2
6577d23
2dc02c0
03e791b
fd39547
b02723f
c73e11e
0243a83
40f9393
77e1603
921be8a
b461b1e
5faae0e
f1b548e
a53bec5
b55308c
1984513
5efd125
badaa02
4b516cb
58c7acd
f4c1db7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,27 +11,38 @@ import ( | |
|
||
"github.com/spf13/cobra" | ||
|
||
"cosmossdk.io/core/transaction" | ||
"cosmossdk.io/log" | ||
) | ||
|
||
func Commands(logger log.Logger, homePath string, modules ...ServerModule) (CLIConfig, error) { | ||
if len(modules) == 0 { | ||
// TODO figure if we should define default modules | ||
// and if so it should be done here to avoid unnecessary dependencies | ||
return CLIConfig{}, errors.New("no modules provided") | ||
func Commands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logger log.Logger, components ...ServerComponent[transaction.Tx]) (CLIConfig, error) { | ||
if len(components) == 0 { | ||
return CLIConfig{}, errors.New("no components provided") | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle the case where no components are provided more gracefully. Consider providing a default component or a more informative error message to guide the user. if len(components) == 0 {
return CLIConfig{}, fmt.Errorf("no server components provided. Please ensure at least one component is configured.")
} |
||
} | ||
|
||
v, err := ReadConfig(filepath.Join(homePath, "config")) | ||
if err != nil { | ||
return CLIConfig{}, fmt.Errorf("failed to read config: %w", err) | ||
} | ||
server := NewServer(logger, components...) | ||
flags := server.StartFlags() | ||
|
||
server := NewServer(logger, modules...) | ||
startCmd := &cobra.Command{ | ||
Use: "start", | ||
Short: "Run the application", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := v.BindPFlags(cmd.Flags()); err != nil { // the server modules are already instantiated here, so binding the flags is useless. | ||
v := GetViperFromCmd(cmd) | ||
l := GetLoggerFromCmd(cmd) | ||
|
||
for _, startFlags := range flags { | ||
if err := v.BindPFlags(startFlags); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := v.BindPFlags(cmd.Flags()); err != nil { | ||
return err | ||
} | ||
|
||
app := newApp(l, v) | ||
|
||
if _, err := server.Init(app, v, l); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure error handling is implemented for server initialization. The error handling for server initialization is crucial and should be clearly checked. if _, err := server.Init(app, v, l); err != nil {
return fmt.Errorf("server initialization failed: %w", err)
} |
||
return err | ||
} | ||
|
||
|
@@ -65,12 +76,58 @@ func Commands(logger log.Logger, homePath string, modules ...ServerModule) (CLIC | |
return cmds, nil | ||
} | ||
|
||
func AddCommands(rootCmd *cobra.Command, logger log.Logger, homePath string, modules ...ServerModule) error { | ||
cmds, err := Commands(logger, homePath, modules...) | ||
func AddCommands(rootCmd *cobra.Command, newApp AppCreator[transaction.Tx], logger log.Logger, components ...ServerComponent[transaction.Tx]) error { | ||
cmds, err := Commands(rootCmd, newApp, logger, components...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
server := NewServer(logger, components...) | ||
originalPersistentPreRunE := rootCmd.PersistentPreRunE | ||
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
home, err := cmd.Flags().GetString(FlagHome) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = configHandle(server, home, cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if rootCmd.PersistentPreRun != nil { | ||
rootCmd.PersistentPreRun(cmd, args) | ||
return nil | ||
} | ||
|
||
return originalPersistentPreRunE(cmd, args) | ||
} | ||
|
||
rootCmd.AddCommand(cmds.Commands...) | ||
return nil | ||
} | ||
|
||
// configHandle writes the default config to the home directory if it does not exist and sets the server context | ||
func configHandle(s *Server, home string, cmd *cobra.Command) error { | ||
if _, err := os.Stat(filepath.Join(home, "config")); os.IsNotExist(err) { | ||
if err = s.WriteConfig(filepath.Join(home, "config")); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
viper, err := ReadConfig(filepath.Join(home, "config")) | ||
if err != nil { | ||
return err | ||
} | ||
viper.Set(FlagHome, home) | ||
if err := viper.BindPFlags(cmd.Flags()); err != nil { | ||
return err | ||
} | ||
|
||
log, err := NewLogger(viper, cmd.OutOrStdout()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return SetCmdServerContext(cmd, viper, log) | ||
} | ||
Comment on lines
+110
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize the configuration handling function to improve clarity and error management. This function can be optimized by consolidating error checks and improving the error messages. func configHandle(s *Server, home string, cmd *cobra.Command) error {
configPath := filepath.Join(home, "config")
if _, err := os.Stat(configPath); os.IsNotExist(err) {
if err = s.WriteConfig(configPath); err != nil {
return fmt.Errorf("failed to write config to %s: %w", configPath, err)
}
}
viper, err := ReadConfig(configPath)
if err != nil {
return fmt.Errorf("failed to read config from %s: %w", configPath, err)
}
viper.Set(FlagHome, home)
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return fmt.Errorf("failed to bind command flags: %w", err)
}
log, err := NewLogger(viper, cmd.OutOrStdout())
if err != nil {
return fmt.Errorf("failed to initialize logger: %w", err)
}
return SetCmdServerContext(cmd, viper, log)
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quote the command substitution to prevent word splitting.
Tools
Shellcheck