github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/to/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 to
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"fmt"
    24  
    25  	"github.com/pelletier/go-toml"
    26  	"gopkg.in/yaml.v3"
    27  
    28  	"github.com/zntrio/harp/v2/pkg/bundle"
    29  	"github.com/zntrio/harp/v2/pkg/sdk/value/flatmap"
    30  	"github.com/zntrio/harp/v2/pkg/tasks"
    31  )
    32  
    33  // ObjectTask implements secret-container publication process to json/yaml content.
    34  type ObjectTask struct {
    35  	ContainerReader tasks.ReaderProvider
    36  	OutputWriter    tasks.WriterProvider
    37  	Expand          bool
    38  	TOML            bool
    39  	YAML            bool
    40  }
    41  
    42  // Run the task.
    43  func (t *ObjectTask) Run(ctx context.Context) error {
    44  	// Create the reader
    45  	reader, err := t.ContainerReader(ctx)
    46  	if err != nil {
    47  		return fmt.Errorf("unable to open input bundle reader: %w", err)
    48  	}
    49  
    50  	// Create output writer
    51  	writer, err := t.OutputWriter(ctx)
    52  	if err != nil {
    53  		return fmt.Errorf("unable to open writer: %w", err)
    54  	}
    55  
    56  	// Extract bundle from container
    57  	b, err := bundle.FromContainerReader(reader)
    58  	if err != nil {
    59  		return fmt.Errorf("unable to load bundle: %w", err)
    60  	}
    61  
    62  	// Convert as map
    63  	bundleMap, err := bundle.AsMap(b)
    64  	if err != nil {
    65  		return fmt.Errorf("unable to transform the bundle as a map: %w", err)
    66  	}
    67  
    68  	var toEncode interface{}
    69  
    70  	// Expand if required
    71  	if t.Expand {
    72  		toEncode = flatmap.Expand(bundleMap, "")
    73  	} else {
    74  		toEncode = bundleMap
    75  	}
    76  
    77  	// Select strategy
    78  	switch {
    79  	case t.YAML:
    80  		// Encode as YAML
    81  		if err := yaml.NewEncoder(writer).Encode(toEncode); err != nil {
    82  			return fmt.Errorf("unable to marshal YAML bundle content: %w", err)
    83  		}
    84  	case t.TOML:
    85  		// Encode as TOML
    86  		if err := toml.NewEncoder(writer).Encode(toEncode); err != nil {
    87  			return fmt.Errorf("unable to marshal TOML bundle content: %w", err)
    88  		}
    89  	default:
    90  		// Encode as JSON
    91  		if err := json.NewEncoder(writer).Encode(toEncode); err != nil {
    92  			return fmt.Errorf("unable to marshal JSON bundle content: %w", err)
    93  		}
    94  	}
    95  
    96  	// No error
    97  	return nil
    98  }