github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/template/values.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 template 19 20 import ( 21 "context" 22 "encoding/json" 23 "fmt" 24 25 "github.com/zntrio/harp/v2/pkg/tasks" 26 tplcmdutil "github.com/zntrio/harp/v2/pkg/template/cmdutil" 27 ) 28 29 // ValueTask implements value object generation task. 30 type ValueTask struct { 31 OutputWriter tasks.WriterProvider 32 ValueFiles []string 33 Values []string 34 StringValues []string 35 FileValues []string 36 } 37 38 // Run the task. 39 func (t *ValueTask) Run(ctx context.Context) error { 40 // Load values 41 valueOpts := tplcmdutil.ValueOptions{ 42 ValueFiles: t.ValueFiles, 43 Values: t.Values, 44 StringValues: t.StringValues, 45 FileValues: t.FileValues, 46 } 47 values, err := valueOpts.MergeValues() 48 if err != nil { 49 return fmt.Errorf("unable to process values: %w", err) 50 } 51 52 // Create output writer 53 writer, err := t.OutputWriter(ctx) 54 if err != nil { 55 return fmt.Errorf("unable to create output writer: %w", err) 56 } 57 58 // Write rendered content 59 if err := json.NewEncoder(writer).Encode(values); err != nil { 60 return fmt.Errorf("unable to dump values as JSON: %w", err) 61 } 62 63 return nil 64 }