|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "google.golang.org/grpc" |
| 8 | + |
| 9 | + "capact.io/capact/internal/logger" |
| 10 | + tivaluefetcher "capact.io/capact/internal/ti-value-fetcher" |
| 11 | + "github.com/vrischmann/envconfig" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/manager/signals" |
| 13 | +) |
| 14 | + |
| 15 | +// Config holds TypeInstance Value resolver configuration. |
| 16 | +type Config struct { |
| 17 | + Input struct { |
| 18 | + TIFilePath string `envconfig:"default=/tmp/input-ti.yaml"` |
| 19 | + BackendTIFilePath string `envconfig:"default=/tmp/storage-backend.yaml"` |
| 20 | + } |
| 21 | + OutputFilePath string `envconfig:"default=/tmp/output.yaml"` |
| 22 | + |
| 23 | + Logger logger.Config |
| 24 | +} |
| 25 | + |
| 26 | +const appName = "ti-value-fetcher" |
| 27 | + |
| 28 | +func main() { |
| 29 | + var cfg Config |
| 30 | + err := envconfig.InitWithPrefix(&cfg, "APP") |
| 31 | + exitOnError(err, "while loading configuration") |
| 32 | + |
| 33 | + ctx := signals.SetupSignalHandler() |
| 34 | + |
| 35 | + // setup logger |
| 36 | + unnamedLogger, err := logger.New(cfg.Logger) |
| 37 | + exitOnError(err, "while creating zap logger") |
| 38 | + |
| 39 | + logger := unnamedLogger.Named(appName) |
| 40 | + |
| 41 | + tiValueFetcher := tivaluefetcher.New(logger) |
| 42 | + |
| 43 | + tiArtifact, storageBackendValue, err := tiValueFetcher.LoadFromFile(cfg.Input.TIFilePath, cfg.Input.BackendTIFilePath) |
| 44 | + exitOnError(err, "while loading input files") |
| 45 | + |
| 46 | + res, err := tiValueFetcher.Do(ctx, tiArtifact, storageBackendValue, grpc.WithInsecure()) |
| 47 | + exitOnError(err, "while resolving TI value") |
| 48 | + |
| 49 | + err = tiValueFetcher.SaveToFile(cfg.OutputFilePath, res) |
| 50 | + exitOnError(err, fmt.Sprintf("while saving output to file %q", cfg.OutputFilePath)) |
| 51 | +} |
| 52 | + |
| 53 | +func exitOnError(err error, context string) { |
| 54 | + if err != nil { |
| 55 | + log.Fatalf("%s: %v", context, err) |
| 56 | + } |
| 57 | +} |
0 commit comments