github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/modinstaller/mod_installer_args.go (about)

     1  package modinstaller
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/turbot/steampipe/pkg/error_helpers"
     7  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
     8  	"github.com/turbot/steampipe/pkg/steampipeconfig/versionmap"
     9  )
    10  
    11  func (i *ModInstaller) GetRequiredModVersionsFromArgs(modsArgs []string) (versionmap.VersionConstraintMap, error) {
    12  	var errors []error
    13  	mods := make(versionmap.VersionConstraintMap, len(modsArgs))
    14  	for _, modArg := range modsArgs {
    15  		// create mod version from arg
    16  		modVersion, err := modconfig.NewModVersionConstraint(modArg)
    17  		if err != nil {
    18  			errors = append(errors, err)
    19  			continue
    20  		}
    21  		// if we are updating there are a few checks we need to make
    22  		if i.updating() {
    23  			modVersion, err = i.getUpdateVersion(modArg, modVersion)
    24  			if err != nil {
    25  				errors = append(errors, err)
    26  				continue
    27  			}
    28  		}
    29  		if i.uninstalling() {
    30  			// it is not valid to specify a mod version for uninstall
    31  			if modVersion.HasVersion() {
    32  				errors = append(errors, fmt.Errorf("invalid arg '%s' - cannot specify a version when uninstalling", modArg))
    33  				continue
    34  			}
    35  		}
    36  
    37  		mods[modVersion.Name] = modVersion
    38  	}
    39  	if len(errors) > 0 {
    40  		return nil, error_helpers.CombineErrors(errors...)
    41  	}
    42  	return mods, nil
    43  }
    44  
    45  func (i *ModInstaller) getUpdateVersion(modArg string, modVersion *modconfig.ModVersionConstraint) (*modconfig.ModVersionConstraint, error) {
    46  	// verify the mod is already installed
    47  	if i.installData.Lock.GetMod(modVersion.Name, i.workspaceMod) == nil {
    48  		return nil, fmt.Errorf("cannot update '%s' as it is not installed", modArg)
    49  	}
    50  
    51  	// find the current dependency with this mod name
    52  	// - this is what we will be using, to ensure we keep the same version constraint
    53  	currentDependency := i.workspaceMod.GetModDependency(modVersion.Name)
    54  	if currentDependency == nil {
    55  		return nil, fmt.Errorf("cannot update '%s' as it is not a dependency of this workspace", modArg)
    56  	}
    57  
    58  	// it is not valid to specify a mod version - we will set the constraint from the modfile
    59  	if modVersion.HasVersion() {
    60  		return nil, fmt.Errorf("invalid arg '%s' - cannot specify a version when updating", modArg)
    61  	}
    62  	return currentDependency, nil
    63  }