github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/profiles/profilesmanager/util.go (about)

     1  // Copyright 2015 The Vanadium Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package profilesmanager
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"v.io/jiri"
    11  	"v.io/jiri/profiles"
    12  )
    13  
    14  // ensureAction ensures that the requested profile and target
    15  // is installed/uninstalled, installing/uninstalling it if and only if necessary.
    16  func ensureAction(jirix *jiri.X, pdb *profiles.DB, action profiles.Action, installer, profile string, root jiri.RelPath, target profiles.Target) error {
    17  	verb := ""
    18  	switch action {
    19  	case profiles.Install:
    20  		verb = "install"
    21  	case profiles.Uninstall:
    22  		verb = "uninstall"
    23  	default:
    24  		return fmt.Errorf("unrecognised action %v", action)
    25  	}
    26  	if jirix.Verbose() {
    27  		fmt.Fprintf(jirix.Stdout(), "%s %v %s\n", verb, action, target)
    28  	}
    29  	if t := pdb.LookupProfileTarget(installer, profile, target); t != nil {
    30  		if jirix.Verbose() {
    31  			fmt.Fprintf(jirix.Stdout(), "%v %v is already %sed as %v\n", profile, target, verb, t)
    32  		}
    33  		return nil
    34  	}
    35  	mgr := LookupManager(profiles.QualifiedProfileName(installer, profile))
    36  	if mgr == nil {
    37  		return fmt.Errorf("profile %v is not supported", profile)
    38  	}
    39  	version, err := mgr.VersionInfo().Select(target.Version())
    40  	if err != nil {
    41  		return err
    42  	}
    43  	target.SetVersion(version)
    44  	if jirix.Verbose() {
    45  		fmt.Fprintf(jirix.Stdout(), "%s %s %s\n", verb, profile, target.DebugString())
    46  	}
    47  	if action == profiles.Install {
    48  		return mgr.Install(jirix, pdb, root, target)
    49  	}
    50  	return mgr.Uninstall(jirix, pdb, root, target)
    51  }
    52  
    53  // EnsureProfileTargetIsInstalled ensures that the requested profile and target
    54  // is installed, installing it if only if necessary.
    55  func EnsureProfileTargetIsInstalled(jirix *jiri.X, pdb *profiles.DB, installer, profile string, root jiri.RelPath, target profiles.Target) error {
    56  	return ensureAction(jirix, pdb, profiles.Install, installer, profile, root, target)
    57  }
    58  
    59  // EnsureProfileTargetIsUninstalled ensures that the requested profile and target
    60  // are no longer installed.
    61  func EnsureProfileTargetIsUninstalled(jirix *jiri.X, pdb *profiles.DB, installer, profile string, root jiri.RelPath, target profiles.Target) error {
    62  	return ensureAction(jirix, pdb, profiles.Uninstall, installer, profile, root, target)
    63  }