github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/profiles/profilesmanager/manager.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 provides support for managing jiri profiles.
     6  // In particular for installing and uninstalling them. It provides a
     7  // registration mechanism for profile implementations to call from an init
     8  // function to add themselves to the suite profiles available within this
     9  // application.
    10  package profilesmanager
    11  
    12  import (
    13  	"sort"
    14  	"sync"
    15  
    16  	"v.io/jiri/profiles"
    17  )
    18  
    19  var (
    20  	registry = struct {
    21  		sync.Mutex
    22  		managers map[string]profiles.Manager
    23  	}{
    24  		managers: make(map[string]profiles.Manager),
    25  	}
    26  )
    27  
    28  // Register is used to register a profile manager. It is an error
    29  // to call Registerr more than once with the same name, though it
    30  // is possible to register the same Manager using different names.
    31  func Register(mgr profiles.Manager) {
    32  	registry.Lock()
    33  	defer registry.Unlock()
    34  	qualifiedName := profiles.QualifiedProfileName(mgr.Installer(), mgr.Name())
    35  	if _, present := registry.managers[qualifiedName]; present {
    36  		panic("a profile manager is already registered for: " + qualifiedName)
    37  	}
    38  	registry.managers[qualifiedName] = mgr
    39  }
    40  
    41  // Names returns the names, in lexicographic order, of all of the currently
    42  // available in-process, profile managers.
    43  func Managers() []string {
    44  	registry.Lock()
    45  	defer registry.Unlock()
    46  	names := make([]string, 0, len(registry.managers))
    47  	for name := range registry.managers {
    48  		names = append(names, name)
    49  	}
    50  	sort.Strings(names)
    51  	return names
    52  }
    53  
    54  // LookupManager returns the manager for the named, in-process, profile or nil
    55  // if one is not found.
    56  func LookupManager(name string) profiles.Manager {
    57  	registry.Lock()
    58  	defer registry.Unlock()
    59  	return registry.managers[name]
    60  }