...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/cmdopts/cmdconfig.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/cmdopts

     1  package cmdopts
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/cybertec-postgresql/pgwatch/v3/internal/metrics"
     8  	"github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
     9  )
    10  
    11  type ConfigCommand struct {
    12  	owner   *Options
    13  	Init    ConfigInitCommand    `command:"init" description:"Initialize configuration"`
    14  	Upgrade ConfigUpgradeCommand `command:"upgrade" description:"Upgrade configuration schema"`
    15  }
    16  
    17  func NewConfigCommand(owner *Options) *ConfigCommand {
    18  	return &ConfigCommand{
    19  		owner:   owner,
    20  		Init:    ConfigInitCommand{owner: owner},
    21  		Upgrade: ConfigUpgradeCommand{owner: owner},
    22  	}
    23  }
    24  
    25  type ConfigInitCommand struct {
    26  	owner *Options
    27  }
    28  
    29  // Execute initializes the configuration.
    30  func (cmd *ConfigInitCommand) Execute([]string) (err error) {
    31  	if err = cmd.owner.ValidateConfig(); err != nil {
    32  		return
    33  	}
    34  	if cmd.owner.Metrics.Metrics > "" {
    35  		err = cmd.InitMetrics()
    36  	}
    37  	if cmd.owner.Sources.Sources > "" && cmd.owner.Metrics.Metrics != cmd.owner.Sources.Sources {
    38  		err = errors.Join(err, cmd.InitSources())
    39  	}
    40  	cmd.owner.CompleteCommand(map[bool]int32{
    41  		true:  ExitCodeOK,
    42  		false: ExitCodeConfigError,
    43  	}[err == nil])
    44  	return
    45  }
    46  
    47  // InitSources initializes the sources configuration.
    48  func (cmd *ConfigInitCommand) InitSources() (err error) {
    49  	ctx := context.Background()
    50  	opts := cmd.owner
    51  	if opts.IsPgConnStr(opts.Sources.Sources) {
    52  		return opts.InitSourceReader(ctx)
    53  	}
    54  	rw, _ := sources.NewYAMLSourcesReaderWriter(ctx, opts.Sources.Sources)
    55  	return rw.WriteSources(sources.Sources{sources.Source{}})
    56  }
    57  
    58  // InitMetrics initializes the metrics configuration.
    59  func (cmd *ConfigInitCommand) InitMetrics() (err error) {
    60  	ctx := context.Background()
    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
    65  	}
    66  	reader, _ := metrics.NewYAMLMetricReaderWriter(ctx, "")
    67  	defMetrics, _ := reader.GetMetrics()
    68  	return opts.MetricsReaderWriter.WriteMetrics(defMetrics)
    69  }
    70  
    71  type ConfigUpgradeCommand struct {
    72  	owner *Options
    73  }
    74  
    75  // Execute upgrades the configuration schema.
    76  func (cmd *ConfigUpgradeCommand) Execute([]string) (err error) {
    77  	opts := cmd.owner
    78  	if err = opts.ValidateConfig(); err != nil {
    79  		return
    80  	}
    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])
    94  			return
    95  		}
    96  	}
    97  	opts.CompleteCommand(ExitCodeConfigError)
    98  	return errors.New("configuration storage does not support upgrade")
    99  }
   100