...

Source file src/edge-infra.dev/pkg/edge/datasync/magpie/config.go

Documentation: edge-infra.dev/pkg/edge/datasync/magpie

     1  package magpie
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/peterbourgon/ff/v3"
     9  
    10  	"edge-infra.dev/pkg/edge/edgeencrypt"
    11  )
    12  
    13  type Config struct {
    14  	Port       string
    15  	MetricPort string
    16  	JWTSecret  string
    17  	Namespace  string
    18  	edgeencrypt.KmsKey
    19  }
    20  
    21  func NewConfig() (*Config, error) {
    22  	cfg := &Config{}
    23  
    24  	fs := flag.NewFlagSet("magpie", flag.ExitOnError)
    25  
    26  	cfg.BindFlags(fs)
    27  
    28  	if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix(), ff.WithIgnoreUndefined(true)); err != nil {
    29  		return cfg, err
    30  	}
    31  
    32  	if err := cfg.Validate(); err != nil {
    33  		return cfg, err
    34  	}
    35  
    36  	return cfg, nil
    37  }
    38  
    39  func (c *Config) BindFlags(fs *flag.FlagSet) {
    40  	fs.StringVar(&c.Port, "port", "8080", "Port to listen on")
    41  	fs.StringVar(&c.MetricPort, "metric-port", "8082", "Metric Port to listen on")
    42  	fs.StringVar(&c.JWTSecret, "jwt-secret", edgeencrypt.DecryptionJWTSecret, "JWT secret for bearer authentication")
    43  	fs.StringVar(&c.Namespace, "namespace", edgeencrypt.DecryptionName, "decryption namespace")
    44  	fs.StringVar(&c.Location, "location", "", "GCP foreman location")
    45  	fs.StringVar(&c.ProjectID, "project-id", "", "foreman project id")
    46  }
    47  
    48  func (c *Config) Validate() error {
    49  	if c.Port == "" {
    50  		return fmt.Errorf("missing PORT environment variable")
    51  	}
    52  	if c.MetricPort == "" {
    53  		return fmt.Errorf("missing METRIC_PORT environment variable")
    54  	}
    55  	if c.JWTSecret == "" {
    56  		return fmt.Errorf("missing JWT_SECRET environment variable")
    57  	}
    58  	if c.Namespace == "" {
    59  		return fmt.Errorf("missing NAMESPACE environment variable")
    60  	}
    61  	if c.Location == "" {
    62  		return fmt.Errorf("missing LOCATION environment variable")
    63  	}
    64  	if c.ProjectID == "" {
    65  		return fmt.Errorf("missing PROJECT_ID environment variable")
    66  	}
    67  	return nil
    68  }
    69  

View as plain text