github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/from/hcl.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package from 19 20 import ( 21 "context" 22 "fmt" 23 "io" 24 25 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 26 "github.com/zntrio/harp/v2/pkg/bundle" 27 "github.com/zntrio/harp/v2/pkg/bundle/hcl" 28 "github.com/zntrio/harp/v2/pkg/tasks" 29 ) 30 31 // JSONMapTask implements secret-container creation from JSON Map. 32 type HCLTask struct { 33 HCLReader tasks.ReaderProvider 34 OutputWriter tasks.WriterProvider 35 } 36 37 // Run the task. 38 func (t *HCLTask) Run(ctx context.Context) error { 39 var ( 40 reader io.Reader 41 writer io.Writer 42 b *bundlev1.Bundle 43 err error 44 ) 45 46 // Create input reader 47 reader, err = t.HCLReader(ctx) 48 if err != nil { 49 return fmt.Errorf("unable to read input reader: %w", err) 50 } 51 52 // Parse input as HCL configuration object. 53 cfg, err := hcl.Parse(reader, "input", "hcl") 54 if err != nil { 55 return fmt.Errorf("unable to parse input HCL: %w", err) 56 } 57 58 // Build the container from hcl dsl 59 b, err = bundle.FromHCL(cfg) 60 if err != nil { 61 return fmt.Errorf("unable to create container from hcl: %w", err) 62 } 63 64 // Create output writer 65 writer, err = t.OutputWriter(ctx) 66 if err != nil { 67 return fmt.Errorf("unable to open output writer: %w", err) 68 } 69 70 // Dump bundle 71 if err = bundle.ToContainerWriter(writer, b); err != nil { 72 return fmt.Errorf("unable to produce exported bundle: %w", err) 73 } 74 75 // No error 76 return nil 77 }