github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/system/list.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package system
     5  
     6  import (
     7  	"fmt"
     8  	"sort"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/errors"
    12  
    13  	"github.com/juju/juju/environs/configstore"
    14  )
    15  
    16  // ListCommand returns the list of all systems the user is
    17  // currently logged in to on the current machine.
    18  type ListCommand struct {
    19  	cmd.CommandBase
    20  	cfgStore configstore.Storage
    21  }
    22  
    23  var listDoc = `
    24  List all the Juju systems logged in to on the current machine.
    25  
    26  A system refers to a Juju Environment System (JES) that runs and manages the
    27  Juju API server and the underlying database used by Juju. A system may manage
    28  multiple environments.
    29  
    30  See Also:
    31      juju help juju-systems
    32      juju help system environments
    33      juju help system create-environment
    34      juju help system use-environment
    35  `
    36  
    37  // Info implements Command.Info
    38  func (c *ListCommand) Info() *cmd.Info {
    39  	return &cmd.Info{
    40  		Name:    "list",
    41  		Purpose: "list all systems logged in to on the current machine",
    42  		Doc:     listDoc,
    43  	}
    44  }
    45  
    46  func (c *ListCommand) getConfigstore() (configstore.Storage, error) {
    47  	if c.cfgStore != nil {
    48  		return c.cfgStore, nil
    49  	}
    50  	return configstore.Default()
    51  }
    52  
    53  // Run implements Command.Run
    54  func (c *ListCommand) Run(ctx *cmd.Context) error {
    55  	store, err := c.getConfigstore()
    56  
    57  	if err != nil {
    58  		return errors.Annotate(err, "failed to get config store")
    59  	}
    60  
    61  	list, err := store.ListSystems()
    62  	if err != nil {
    63  		return errors.Annotate(err, "failed to list systems in config store")
    64  	}
    65  
    66  	sort.Strings(list)
    67  	for _, name := range list {
    68  		fmt.Fprintf(ctx.Stdout, "%s\n", name)
    69  	}
    70  
    71  	return nil
    72  }