@@ -3,7 +3,6 @@ package cmdopts
33import (
44 "context"
55 "errors"
6- "fmt"
76
87 "github.com/cybertec-postgresql/pgwatch/v3/internal/metrics"
98 "github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
@@ -27,91 +26,74 @@ type ConfigInitCommand struct {
2726 owner * Options
2827}
2928
30- func (cmd * ConfigInitCommand ) Execute ([]string ) error {
31- if len (cmd .owner .Sources .Sources )+ len (cmd .owner .Metrics .Metrics ) == 0 {
32- return errors .New ("both --sources and --metrics are empty" )
29+ // Execute initializes the configuration.
30+ func (cmd * ConfigInitCommand ) Execute ([]string ) (err error ) {
31+ if err = cmd .owner .ValidateConfig (); err != nil {
32+ return
3333 }
3434 if cmd .owner .Metrics .Metrics > "" {
35- cmd .InitMetrics ()
35+ err = cmd .InitMetrics ()
3636 }
3737 if cmd .owner .Sources .Sources > "" && cmd .owner .Metrics .Metrics != cmd .owner .Sources .Sources {
38- cmd .InitSources ()
38+ err = errors . Join ( err , cmd .InitSources () )
3939 }
40- cmd .owner .CommandCompleted = true
41- return nil
40+ cmd .owner .CompleteCommand (map [bool ]int32 {
41+ true : ExitCodeOK ,
42+ false : ExitCodeConfigError ,
43+ }[err == nil ])
44+ return
4245}
4346
44- func (cmd * ConfigInitCommand ) InitSources () {
47+ // InitSources initializes the sources configuration.
48+ func (cmd * ConfigInitCommand ) InitSources () (err error ) {
4549 ctx := context .Background ()
46- kind , err := cmd .owner .GetConfigKind (cmd .owner .Sources .Sources )
47- if err != nil {
48- cmd .owner .CompleteCommand (ExitCodeConfigError )
49- return
50- }
51- switch kind {
52- case ConfigPgURL :
53- if err = cmd .owner .InitSourceReader (ctx ); err != nil {
54- cmd .owner .CompleteCommand (ExitCodeConfigError )
55- return
56- }
57- case ConfigFile :
58- writer , _ := sources .NewYAMLSourcesReaderWriter (ctx , cmd .owner .Sources .Sources )
59- if err = writer .WriteSources (sources.Sources {sources.Source {}}); err != nil {
60- fmt .Printf ("cannot init sources: %s" , err )
61- cmd .owner .CompleteCommand (ExitCodeConfigError )
62- return
63- }
50+ opts := cmd .owner
51+ if opts .IsPgConnStr (opts .Sources .Sources ) {
52+ return opts .InitSourceReader (ctx )
6453 }
65- fmt .Println ("Sources initialized" )
54+ rw , _ := sources .NewYAMLSourcesReaderWriter (ctx , opts .Sources .Sources )
55+ return rw .WriteSources (sources.Sources {sources.Source {}})
6656}
6757
68- func (cmd * ConfigInitCommand ) InitMetrics () {
58+ // InitMetrics initializes the metrics configuration.
59+ func (cmd * ConfigInitCommand ) InitMetrics () (err error ) {
6960 ctx := context .Background ()
70- if cmd .owner .IsPgConnStr (cmd .owner .Metrics .Metrics ) {
71- if err := cmd .owner .InitMetricReader (ctx ); err != nil {
72- cmd .owner .CompleteCommand (ExitCodeConfigError )
73- return
74- }
75- } else {
76- reader , _ := metrics .NewYAMLMetricReaderWriter (ctx , "" )
77- writer , _ := metrics .NewYAMLMetricReaderWriter (ctx , cmd .owner .Metrics .Metrics )
78- defMetrics , _ := reader .GetMetrics ()
79- if err := writer .WriteMetrics (defMetrics ); err != nil {
80- fmt .Printf ("cannot init metrics: %s" , err )
81- cmd .owner .CompleteCommand (ExitCodeConfigError )
82- return
83- }
61+ opts := cmd .owner
62+ err = opts .InitMetricReader (ctx )
63+ if err != nil || opts .IsPgConnStr (opts .Metrics .Metrics ) {
64+ return // nothing to do, database initialized automatically
8465 }
85- fmt .Println ("Metrics initialized" )
66+ reader , _ := metrics .NewYAMLMetricReaderWriter (ctx , "" )
67+ defMetrics , _ := reader .GetMetrics ()
68+ return opts .MetricsReaderWriter .WriteMetrics (defMetrics )
8669}
8770
8871type ConfigUpgradeCommand struct {
8972 owner * Options
9073}
9174
75+ // Execute upgrades the configuration schema.
9276func (cmd * ConfigUpgradeCommand ) Execute ([]string ) (err error ) {
93- err = cmd .owner .InitConfigReaders (context .Background ())
94- if err != nil {
95- cmd .owner .CompleteCommand (ExitCodeConfigError )
96- fmt .Println (err )
77+ opts := cmd .owner
78+ if err = opts .ValidateConfig (); err != nil {
9779 return
9880 }
99- if m , ok := cmd .owner .MetricsReaderWriter .(metrics.Migrator ); ok {
100- if err = m .Migrate (); err != nil {
101- cmd .owner .CompleteCommand (ExitCodeUpgradeError )
102- fmt .Printf ("cannot upgrade metrics: %s" , err )
81+ // for now only postgres configuration is upgradable
82+ if opts .IsPgConnStr (opts .Metrics .Metrics ) && opts .IsPgConnStr (opts .Sources .Sources ) {
83+ err = opts .InitMetricReader (context .Background ())
84+ if err != nil {
85+ opts .CompleteCommand (ExitCodeConfigError )
86+ return
87+ }
88+ if m , ok := opts .MetricsReaderWriter .(metrics.Migrator ); ok {
89+ err = m .Migrate ()
90+ opts .CompleteCommand (map [bool ]int32 {
91+ true : ExitCodeOK ,
92+ false : ExitCodeConfigError ,
93+ }[err == nil ])
10394 return
10495 }
105- cmd .owner .CompleteCommand (ExitCodeOK )
106- fmt .Println ("Configuration schema upgraded" )
107- return
10896 }
109- // if s, ok := cmd.owner.SourcesReaderWriter.(sources.Migrator); ok {
110- // if err = cmd.UpgradeConfiguration(s); err != nil {
111- // cmd.owner.CompleteCommand(ExitCodeUpgradeError)
112- // fmt.Printf("cannot upgrade sources: %s", err)
113- // return
114- // }
115- // }
116- return errors .New ("Configuration storage does not support upgrade" )
97+ opts .CompleteCommand (ExitCodeConfigError )
98+ return errors .New ("configuration storage does not support upgrade" )
11799}
0 commit comments