github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/from/dump.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/tasks"
    28  )
    29  
    30  // BundleDumpTask implements secret-container creation from a Bundle Dump.
    31  type BundleDumpTask struct {
    32  	JSONReader   tasks.ReaderProvider
    33  	OutputWriter tasks.WriterProvider
    34  }
    35  
    36  // Run the task.
    37  func (t *BundleDumpTask) Run(ctx context.Context) error {
    38  	var (
    39  		reader io.Reader
    40  		writer io.Writer
    41  		b      *bundlev1.Bundle
    42  		err    error
    43  	)
    44  
    45  	// Create input reader
    46  	reader, err = t.JSONReader(ctx)
    47  	if err != nil {
    48  		return fmt.Errorf("unable to read input reader: %w", err)
    49  	}
    50  
    51  	// Build the container from json
    52  	b, err = bundle.FromDump(reader)
    53  	if err != nil {
    54  		return fmt.Errorf("unable to create container from dump: %w", err)
    55  	}
    56  
    57  	// Create output writer
    58  	writer, err = t.OutputWriter(ctx)
    59  	if err != nil {
    60  		return fmt.Errorf("unable to open output writer: %w", err)
    61  	}
    62  
    63  	// Dump bundle
    64  	if err = bundle.ToContainerWriter(writer, b); err != nil {
    65  		return fmt.Errorf("unable to produce exported bundle: %w", err)
    66  	}
    67  
    68  	// No error
    69  	return nil
    70  }