github.com/opentofu/opentofu@v1.7.1/internal/command/meta_new.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 command
     7  
     8  import (
     9  	"os"
    10  	"strconv"
    11  
    12  	"github.com/opentofu/opentofu/internal/encryption"
    13  	"github.com/opentofu/opentofu/internal/plans/planfile"
    14  )
    15  
    16  // NOTE: Temporary file until this branch is cleaned up.
    17  
    18  // Input returns whether or not input asking is enabled.
    19  func (m *Meta) Input() bool {
    20  	if test || !m.input {
    21  		return false
    22  	}
    23  
    24  	if envVar := os.Getenv(InputModeEnvVar); envVar != "" {
    25  		if v, err := strconv.ParseBool(envVar); err == nil && !v {
    26  			return false
    27  		}
    28  	}
    29  
    30  	return true
    31  }
    32  
    33  // PlanFile loads the plan file at the given path, which might be either a local
    34  // or cloud plan.
    35  //
    36  // If the return value and error are both nil, the given path exists but seems
    37  // to be a configuration directory instead.
    38  //
    39  // Error will be non-nil if path refers to something which looks like a plan
    40  // file and loading the file fails.
    41  func (m *Meta) PlanFile(path string, enc encryption.PlanEncryption) (*planfile.WrappedPlanFile, error) {
    42  	fi, err := os.Stat(path)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	if fi.IsDir() {
    48  		// Looks like a configuration directory.
    49  		return nil, nil
    50  	}
    51  
    52  	return planfile.OpenWrapped(path, enc)
    53  }