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