github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/from/json_map.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 "encoding/json" 23 "fmt" 24 "io" 25 26 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 27 "github.com/zntrio/harp/v2/pkg/bundle" 28 "github.com/zntrio/harp/v2/pkg/tasks" 29 ) 30 31 // JSONMapTask implements secret-container creation from JSON Map. 32 type JSONMapTask struct { 33 JSONReader tasks.ReaderProvider 34 OutputWriter tasks.WriterProvider 35 } 36 37 // Run the task. 38 func (t *JSONMapTask) 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.JSONReader(ctx) 48 if err != nil { 49 return fmt.Errorf("unable to read input reader: %w", err) 50 } 51 52 // Convert input as a map 53 var input map[string]bundle.KV 54 if err = json.NewDecoder(reader).Decode(&input); err != nil { 55 return fmt.Errorf("unable to decode input JSON: %w", err) 56 } 57 58 // Build the container from json 59 b, err = bundle.FromMap(input) 60 if err != nil { 61 return fmt.Errorf("unable to create container from map: %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 }