Skip to content

Commit

Permalink
cmd: limit global flags displayed on cli help output (#5077)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland authored Oct 11, 2023
1 parent e50ba18 commit 3ce1cea
Show file tree
Hide file tree
Showing 9 changed files with 515 additions and 287 deletions.
1 change: 1 addition & 0 deletions services/horizon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ file. This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
- The same slippage calculation from the [`v2.26.1`](#2261) hotfix now properly excludes spikes for smoother trade aggregation plots ([4999](https://github.com/stellar/go/pull/4999)).
- Limit the display of global flags on command line help `-h` output ([5077](https://github.com/stellar/go/pull/5077)).
- Add a deprecation warning for using command-line flags when running Horizon ([5051](https://github.com/stellar/go/pull/5051))


Expand Down
26 changes: 13 additions & 13 deletions services/horizon/cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func requireAndSetFlags(names ...string) error {
for _, name := range names {
set[name] = true
}
for _, flag := range flags {
for _, flag := range globalFlags {
if set[flag.Name] {
flag.Require()
if err := flag.SetValue(); err != nil {
Expand Down Expand Up @@ -66,7 +66,7 @@ var dbInitCmd = &cobra.Command{
return err
}

db, err := sql.Open("postgres", config.DatabaseURL)
db, err := sql.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return err
}
Expand All @@ -86,12 +86,12 @@ var dbInitCmd = &cobra.Command{
}

func migrate(dir schema.MigrateDir, count int) error {
if !config.Ingest {
if !globalConfig.Ingest {
log.Println("Skipping migrations because ingest flag is not enabled")
return nil
}

dbConn, err := db.Open("postgres", config.DatabaseURL)
dbConn, err := db.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return err
}
Expand Down Expand Up @@ -172,7 +172,7 @@ var dbMigrateStatusCmd = &cobra.Command{
return ErrUsage{cmd}
}

dbConn, err := db.Open("postgres", config.DatabaseURL)
dbConn, err := db.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return err
}
Expand Down Expand Up @@ -220,7 +220,7 @@ var dbReapCmd = &cobra.Command{
Short: "reaps (i.e. removes) any reapable history data",
Long: "reap removes any historical data that is earlier than the configured retention cutoff",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := horizon.NewAppFromFlags(config, flags)
app, err := horizon.NewAppFromFlags(globalConfig, globalFlags)
if err != nil {
return err
}
Expand Down Expand Up @@ -321,15 +321,15 @@ var dbReingestRangeCmd = &cobra.Command{
}
}

err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true})
err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true})
if err != nil {
return err
}
return runDBReingestRange(
[]history.LedgerRange{{StartSequence: argsUInt32[0], EndSequence: argsUInt32[1]}},
reingestForce,
parallelWorkers,
*config,
*globalConfig,
)
},
}
Expand Down Expand Up @@ -369,26 +369,26 @@ var dbFillGapsCmd = &cobra.Command{
withRange = true
}

err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true})
err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true})
if err != nil {
return err
}
var gaps []history.LedgerRange
if withRange {
gaps, err = runDBDetectGapsInRange(*config, uint32(start), uint32(end))
gaps, err = runDBDetectGapsInRange(*globalConfig, uint32(start), uint32(end))
if err != nil {
return err
}
hlog.Infof("found gaps %v within range [%v, %v]", gaps, start, end)
} else {
gaps, err = runDBDetectGaps(*config)
gaps, err = runDBDetectGaps(*globalConfig)
if err != nil {
return err
}
hlog.Infof("found gaps %v", gaps)
}

return runDBReingestRange(gaps, reingestForce, parallelWorkers, *config)
return runDBReingestRange(gaps, reingestForce, parallelWorkers, *globalConfig)
},
}

Expand Down Expand Up @@ -479,7 +479,7 @@ var dbDetectGapsCmd = &cobra.Command{
if len(args) != 0 {
return ErrUsage{cmd}
}
gaps, err := runDBDetectGaps(*config)
gaps, err := runDBDetectGaps(*globalConfig)
if err != nil {
return err
}
Expand Down
118 changes: 59 additions & 59 deletions services/horizon/cmd/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var ingestVerifyRangeCmd = &cobra.Command{
co.SetValue()
}

if err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
if err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
return err
}

Expand All @@ -111,11 +111,11 @@ var ingestVerifyRangeCmd = &cobra.Command{
}()
}

horizonSession, err := db.Open("postgres", config.DatabaseURL)
horizonSession, err := db.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return fmt.Errorf("cannot open Horizon DB: %v", err)
}
mngr := historyarchive.NewCheckpointManager(config.CheckpointFrequency)
mngr := historyarchive.NewCheckpointManager(globalConfig.CheckpointFrequency)
if !mngr.IsCheckpoint(ingestVerifyFrom) && ingestVerifyFrom != 1 {
return fmt.Errorf("`--from` must be a checkpoint ledger")
}
Expand All @@ -125,26 +125,26 @@ var ingestVerifyRangeCmd = &cobra.Command{
}

ingestConfig := ingest.Config{
NetworkPassphrase: config.NetworkPassphrase,
NetworkPassphrase: globalConfig.NetworkPassphrase,
HistorySession: horizonSession,
HistoryArchiveURLs: config.HistoryArchiveURLs,
EnableCaptiveCore: config.EnableCaptiveCoreIngestion,
CaptiveCoreBinaryPath: config.CaptiveCoreBinaryPath,
CaptiveCoreConfigUseDB: config.CaptiveCoreConfigUseDB,
RemoteCaptiveCoreURL: config.RemoteCaptiveCoreURL,
CheckpointFrequency: config.CheckpointFrequency,
CaptiveCoreToml: config.CaptiveCoreToml,
CaptiveCoreStoragePath: config.CaptiveCoreStoragePath,
RoundingSlippageFilter: config.RoundingSlippageFilter,
EnableIngestionFiltering: config.EnableIngestionFiltering,
HistoryArchiveURLs: globalConfig.HistoryArchiveURLs,
EnableCaptiveCore: globalConfig.EnableCaptiveCoreIngestion,
CaptiveCoreBinaryPath: globalConfig.CaptiveCoreBinaryPath,
CaptiveCoreConfigUseDB: globalConfig.CaptiveCoreConfigUseDB,
RemoteCaptiveCoreURL: globalConfig.RemoteCaptiveCoreURL,
CheckpointFrequency: globalConfig.CheckpointFrequency,
CaptiveCoreToml: globalConfig.CaptiveCoreToml,
CaptiveCoreStoragePath: globalConfig.CaptiveCoreStoragePath,
RoundingSlippageFilter: globalConfig.RoundingSlippageFilter,
EnableIngestionFiltering: globalConfig.EnableIngestionFiltering,
}

if !ingestConfig.EnableCaptiveCore {
if config.StellarCoreDatabaseURL == "" {
if globalConfig.StellarCoreDatabaseURL == "" {
return fmt.Errorf("flag --%s cannot be empty", horizon.StellarCoreDBURLFlagName)
}

coreSession, dbErr := db.Open("postgres", config.StellarCoreDatabaseURL)
coreSession, dbErr := db.Open("postgres", globalConfig.StellarCoreDatabaseURL)
if dbErr != nil {
return fmt.Errorf("cannot open Core DB: %v", dbErr)
}
Expand Down Expand Up @@ -203,11 +203,11 @@ var ingestStressTestCmd = &cobra.Command{
co.SetValue()
}

if err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
if err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
return err
}

horizonSession, err := db.Open("postgres", config.DatabaseURL)
horizonSession, err := db.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return fmt.Errorf("cannot open Horizon DB: %v", err)
}
Expand All @@ -221,23 +221,23 @@ var ingestStressTestCmd = &cobra.Command{
}

ingestConfig := ingest.Config{
NetworkPassphrase: config.NetworkPassphrase,
NetworkPassphrase: globalConfig.NetworkPassphrase,
HistorySession: horizonSession,
HistoryArchiveURLs: config.HistoryArchiveURLs,
EnableCaptiveCore: config.EnableCaptiveCoreIngestion,
RoundingSlippageFilter: config.RoundingSlippageFilter,
HistoryArchiveURLs: globalConfig.HistoryArchiveURLs,
EnableCaptiveCore: globalConfig.EnableCaptiveCoreIngestion,
RoundingSlippageFilter: globalConfig.RoundingSlippageFilter,
}

if config.EnableCaptiveCoreIngestion {
ingestConfig.CaptiveCoreBinaryPath = config.CaptiveCoreBinaryPath
ingestConfig.RemoteCaptiveCoreURL = config.RemoteCaptiveCoreURL
ingestConfig.CaptiveCoreConfigUseDB = config.CaptiveCoreConfigUseDB
if globalConfig.EnableCaptiveCoreIngestion {
ingestConfig.CaptiveCoreBinaryPath = globalConfig.CaptiveCoreBinaryPath
ingestConfig.RemoteCaptiveCoreURL = globalConfig.RemoteCaptiveCoreURL
ingestConfig.CaptiveCoreConfigUseDB = globalConfig.CaptiveCoreConfigUseDB
} else {
if config.StellarCoreDatabaseURL == "" {
if globalConfig.StellarCoreDatabaseURL == "" {
return fmt.Errorf("flag --%s cannot be empty", horizon.StellarCoreDBURLFlagName)
}

coreSession, dbErr := db.Open("postgres", config.StellarCoreDatabaseURL)
coreSession, dbErr := db.Open("postgres", globalConfig.StellarCoreDatabaseURL)
if dbErr != nil {
return fmt.Errorf("cannot open Core DB: %v", dbErr)
}
Expand Down Expand Up @@ -267,11 +267,11 @@ var ingestTriggerStateRebuildCmd = &cobra.Command{
Short: "updates a database to trigger state rebuild, state will be rebuilt by a running Horizon instance, DO NOT RUN production DB, some endpoints will be unavailable until state is rebuilt",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
if err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
return err
}

horizonSession, err := db.Open("postgres", config.DatabaseURL)
horizonSession, err := db.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return fmt.Errorf("cannot open Horizon DB: %v", err)
}
Expand All @@ -291,11 +291,11 @@ var ingestInitGenesisStateCmd = &cobra.Command{
Short: "ingests genesis state (ledger 1)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
if err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
return err
}

horizonSession, err := db.Open("postgres", config.DatabaseURL)
horizonSession, err := db.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return fmt.Errorf("cannot open Horizon DB: %v", err)
}
Expand All @@ -312,24 +312,24 @@ var ingestInitGenesisStateCmd = &cobra.Command{
}

ingestConfig := ingest.Config{
NetworkPassphrase: config.NetworkPassphrase,
NetworkPassphrase: globalConfig.NetworkPassphrase,
HistorySession: horizonSession,
HistoryArchiveURLs: config.HistoryArchiveURLs,
EnableCaptiveCore: config.EnableCaptiveCoreIngestion,
CheckpointFrequency: config.CheckpointFrequency,
RoundingSlippageFilter: config.RoundingSlippageFilter,
EnableIngestionFiltering: config.EnableIngestionFiltering,
HistoryArchiveURLs: globalConfig.HistoryArchiveURLs,
EnableCaptiveCore: globalConfig.EnableCaptiveCoreIngestion,
CheckpointFrequency: globalConfig.CheckpointFrequency,
RoundingSlippageFilter: globalConfig.RoundingSlippageFilter,
EnableIngestionFiltering: globalConfig.EnableIngestionFiltering,
}

if config.EnableCaptiveCoreIngestion {
ingestConfig.CaptiveCoreBinaryPath = config.CaptiveCoreBinaryPath
ingestConfig.CaptiveCoreConfigUseDB = config.CaptiveCoreConfigUseDB
if globalConfig.EnableCaptiveCoreIngestion {
ingestConfig.CaptiveCoreBinaryPath = globalConfig.CaptiveCoreBinaryPath
ingestConfig.CaptiveCoreConfigUseDB = globalConfig.CaptiveCoreConfigUseDB
} else {
if config.StellarCoreDatabaseURL == "" {
if globalConfig.StellarCoreDatabaseURL == "" {
return fmt.Errorf("flag --%s cannot be empty", horizon.StellarCoreDBURLFlagName)
}

coreSession, dbErr := db.Open("postgres", config.StellarCoreDatabaseURL)
coreSession, dbErr := db.Open("postgres", globalConfig.StellarCoreDatabaseURL)
if dbErr != nil {
return fmt.Errorf("cannot open Core DB: %v", dbErr)
}
Expand Down Expand Up @@ -363,11 +363,11 @@ var ingestBuildStateCmd = &cobra.Command{
co.SetValue()
}

if err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
if err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{RequireCaptiveCoreFullConfig: false, AlwaysIngest: true}); err != nil {
return err
}

horizonSession, err := db.Open("postgres", config.DatabaseURL)
horizonSession, err := db.Open("postgres", globalConfig.DatabaseURL)
if err != nil {
return fmt.Errorf("cannot open Horizon DB: %v", err)
}
Expand All @@ -383,33 +383,33 @@ var ingestBuildStateCmd = &cobra.Command{
return fmt.Errorf("cannot run on non-empty DB")
}

mngr := historyarchive.NewCheckpointManager(config.CheckpointFrequency)
mngr := historyarchive.NewCheckpointManager(globalConfig.CheckpointFrequency)
if !mngr.IsCheckpoint(ingestBuildStateSequence) && ingestBuildStateSequence != 1 {
return fmt.Errorf("`--sequence` must be a checkpoint ledger")
}

ingestConfig := ingest.Config{
NetworkPassphrase: config.NetworkPassphrase,
NetworkPassphrase: globalConfig.NetworkPassphrase,
HistorySession: horizonSession,
HistoryArchiveURLs: config.HistoryArchiveURLs,
EnableCaptiveCore: config.EnableCaptiveCoreIngestion,
CaptiveCoreBinaryPath: config.CaptiveCoreBinaryPath,
CaptiveCoreConfigUseDB: config.CaptiveCoreConfigUseDB,
RemoteCaptiveCoreURL: config.RemoteCaptiveCoreURL,
CheckpointFrequency: config.CheckpointFrequency,
CaptiveCoreToml: config.CaptiveCoreToml,
CaptiveCoreStoragePath: config.CaptiveCoreStoragePath,
RoundingSlippageFilter: config.RoundingSlippageFilter,
EnableIngestionFiltering: config.EnableIngestionFiltering,
HistoryArchiveURLs: globalConfig.HistoryArchiveURLs,
EnableCaptiveCore: globalConfig.EnableCaptiveCoreIngestion,
CaptiveCoreBinaryPath: globalConfig.CaptiveCoreBinaryPath,
CaptiveCoreConfigUseDB: globalConfig.CaptiveCoreConfigUseDB,
RemoteCaptiveCoreURL: globalConfig.RemoteCaptiveCoreURL,
CheckpointFrequency: globalConfig.CheckpointFrequency,
CaptiveCoreToml: globalConfig.CaptiveCoreToml,
CaptiveCoreStoragePath: globalConfig.CaptiveCoreStoragePath,
RoundingSlippageFilter: globalConfig.RoundingSlippageFilter,
EnableIngestionFiltering: globalConfig.EnableIngestionFiltering,
}

if !ingestBuildStateSkipChecks {
if !ingestConfig.EnableCaptiveCore {
if config.StellarCoreDatabaseURL == "" {
if globalConfig.StellarCoreDatabaseURL == "" {
return fmt.Errorf("flag --%s cannot be empty", horizon.StellarCoreDBURLFlagName)
}

coreSession, dbErr := db.Open("postgres", config.StellarCoreDatabaseURL)
coreSession, dbErr := db.Open("postgres", globalConfig.StellarCoreDatabaseURL)
if dbErr != nil {
return fmt.Errorf("cannot open Core DB: %v", dbErr)
}
Expand Down
4 changes: 2 additions & 2 deletions services/horizon/cmd/record_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var recordMetricsCmd = &cobra.Command{
Short: "records `/metrics` on admin port for debuging purposes",
Long: "",
RunE: func(cmd *cobra.Command, args []string) error {
if err := horizon.ApplyFlags(config, flags, horizon.ApplyOptions{}); err != nil {
if err := horizon.ApplyFlags(globalConfig, globalFlags, horizon.ApplyOptions{}); err != nil {
return err
}

Expand Down Expand Up @@ -50,7 +50,7 @@ var recordMetricsCmd = &cobra.Command{
time.Duration(time.Duration(scrapeIntervalSeconds*(scrapesCount-i))*time.Second),
)

metricsResponse, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/metrics", config.AdminPort))
metricsResponse, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/metrics", globalConfig.AdminPort))
if err != nil {
return errors.Wrap(err, "Error fetching metrics. Is admin server running?")
}
Expand Down
Loading

0 comments on commit 3ce1cea

Please sign in to comment.