github.com/opentofu/opentofu@v1.7.1/internal/plugin/ui_input.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package plugin
     7  
     8  import (
     9  	"context"
    10  	"net/rpc"
    11  
    12  	"github.com/hashicorp/go-plugin"
    13  	"github.com/opentofu/opentofu/internal/tofu"
    14  )
    15  
    16  // UIInput is an implementation of tofu.UIInput that communicates
    17  // over RPC.
    18  type UIInput struct {
    19  	Client *rpc.Client
    20  }
    21  
    22  func (i *UIInput) Input(ctx context.Context, opts *tofu.InputOpts) (string, error) {
    23  	var resp UIInputInputResponse
    24  	err := i.Client.Call("Plugin.Input", opts, &resp)
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  	if resp.Error != nil {
    29  		err = resp.Error
    30  		return "", err
    31  	}
    32  
    33  	return resp.Value, nil
    34  }
    35  
    36  type UIInputInputResponse struct {
    37  	Value string
    38  	Error *plugin.BasicError
    39  }
    40  
    41  // UIInputServer is a net/rpc compatible structure for serving
    42  // a UIInputServer. This should not be used directly.
    43  type UIInputServer struct {
    44  	UIInput tofu.UIInput
    45  }
    46  
    47  func (s *UIInputServer) Input(
    48  	opts *tofu.InputOpts,
    49  	reply *UIInputInputResponse) error {
    50  	value, err := s.UIInput.Input(context.Background(), opts)
    51  	*reply = UIInputInputResponse{
    52  		Value: value,
    53  		Error: plugin.NewBasicError(err),
    54  	}
    55  
    56  	return nil
    57  }