github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/from/object.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  	"errors"
    24  	"fmt"
    25  	"io"
    26  
    27  	"gopkg.in/yaml.v3"
    28  
    29  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    30  	"github.com/zntrio/harp/v2/pkg/bundle"
    31  	"github.com/zntrio/harp/v2/pkg/sdk/value/flatmap"
    32  	"github.com/zntrio/harp/v2/pkg/tasks"
    33  )
    34  
    35  // ObjectTask implements secret-container creation from a YAML/JSON structure.
    36  type ObjectTask struct {
    37  	ObjectReader tasks.ReaderProvider
    38  	OutputWriter tasks.WriterProvider
    39  	JSON         bool
    40  	YAML         bool
    41  }
    42  
    43  // Run the task.
    44  func (t *ObjectTask) Run(ctx context.Context) error {
    45  	var (
    46  		reader io.Reader
    47  		writer io.Writer
    48  		b      *bundlev1.Bundle
    49  		err    error
    50  	)
    51  
    52  	// Create input reader
    53  	reader, err = t.ObjectReader(ctx)
    54  	if err != nil {
    55  		return fmt.Errorf("unable to read input reader: %w", err)
    56  	}
    57  
    58  	// Decode as YAML any object
    59  	var source map[string]interface{}
    60  
    61  	switch {
    62  	case t.YAML:
    63  		if errYaml := yaml.NewDecoder(reader).Decode(&source); errYaml != nil {
    64  			return fmt.Errorf("unable to decode source as YAML: %w", err)
    65  		}
    66  	case t.JSON:
    67  		if errYaml := json.NewDecoder(reader).Decode(&source); errYaml != nil {
    68  			return fmt.Errorf("unable to decode source as JSON: %w", err)
    69  		}
    70  	default:
    71  		return errors.New("json or yaml must be selected")
    72  	}
    73  
    74  	// Flatten the struct
    75  	input := flatmap.Flatten(source)
    76  
    77  	// Build the container from json
    78  	b, err = bundle.FromMap(input)
    79  	if err != nil {
    80  		return fmt.Errorf("unable to create container from map: %w", err)
    81  	}
    82  
    83  	// Create output writer
    84  	writer, err = t.OutputWriter(ctx)
    85  	if err != nil {
    86  		return fmt.Errorf("unable to open output writer: %w", err)
    87  	}
    88  
    89  	// Dump bundle
    90  	if err = bundle.ToContainerWriter(writer, b); err != nil {
    91  		return fmt.Errorf("unable to produce exported bundle: %w", err)
    92  	}
    93  
    94  	// No error
    95  	return nil
    96  }