github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/version/command_factory/command_factory.go (about)

     1  package command_factory
     2  
     3  import (
     4  	config_package "github.com/cloudfoundry-incubator/ltc/config"
     5  	"github.com/cloudfoundry-incubator/ltc/exit_handler"
     6  	"github.com/cloudfoundry-incubator/ltc/exit_handler/exit_codes"
     7  	"github.com/cloudfoundry-incubator/ltc/terminal"
     8  	"github.com/cloudfoundry-incubator/ltc/version"
     9  	"github.com/codegangsta/cli"
    10  )
    11  
    12  type VersionCommandFactory struct {
    13  	config      *config_package.Config
    14  	ui          terminal.UI
    15  	exitHandler exit_handler.ExitHandler
    16  
    17  	arch    string
    18  	ltcPath string
    19  
    20  	versionManager VersionManager
    21  }
    22  
    23  type VersionManager interface {
    24  	SyncLTC(ltcPath string, arch string, config *config_package.Config) error
    25  	ServerVersions(receptorTarget string) (version.ServerVersions, error)
    26  	LatticeVersion() string
    27  }
    28  
    29  func NewVersionCommandFactory(config *config_package.Config, ui terminal.UI, exitHandler exit_handler.ExitHandler, arch string, ltcPath string, versionManager VersionManager) *VersionCommandFactory {
    30  	return &VersionCommandFactory{config, ui, exitHandler, arch, ltcPath, versionManager}
    31  }
    32  
    33  func (f *VersionCommandFactory) MakeSyncCommand() cli.Command {
    34  	return cli.Command{
    35  		Name:        "sync",
    36  		Usage:       "Updates ltc to the latest version available in the targeted Lattice cluster",
    37  		Description: "ltc sync",
    38  		Action:      f.syncLTC,
    39  	}
    40  }
    41  
    42  func (f *VersionCommandFactory) MakeVersionCommand() cli.Command {
    43  	return cli.Command{
    44  		Name:        "version",
    45  		Aliases:     []string{"vr"},
    46  		Usage:       "Returns CLI and server versions",
    47  		Description: "ltc version",
    48  		Action:      f.version,
    49  	}
    50  }
    51  
    52  func (f *VersionCommandFactory) syncLTC(context *cli.Context) {
    53  	var architecture string
    54  	switch f.arch {
    55  	case "darwin":
    56  		architecture = "osx"
    57  	case "linux":
    58  		architecture = "linux"
    59  	default:
    60  		f.ui.SayLine("Error: Unknown architecture %s. Sync not supported.", f.arch)
    61  		f.exitHandler.Exit(exit_codes.CommandFailed)
    62  		return
    63  	}
    64  
    65  	if f.ltcPath == "" {
    66  		f.ui.SayLine("Error: Unable to locate the ltc binary. Sync not supported.")
    67  		f.exitHandler.Exit(exit_codes.CommandFailed)
    68  		return
    69  	}
    70  
    71  	if f.config.Target() == "" {
    72  		f.ui.SayLine("Error: Must be targeted to sync.")
    73  		f.exitHandler.Exit(exit_codes.CommandFailed)
    74  		return
    75  	}
    76  
    77  	err := f.versionManager.SyncLTC(f.ltcPath, architecture, f.config)
    78  	if err != nil {
    79  		f.ui.SayLine("Error: " + err.Error())
    80  		f.exitHandler.Exit(exit_codes.CommandFailed)
    81  		return
    82  	}
    83  
    84  	f.ui.SayLine("Updated ltc to the latest version.")
    85  }
    86  
    87  func (f *VersionCommandFactory) version(context *cli.Context) {
    88  	f.ui.SayLine("Client: " + f.versionManager.LatticeVersion())
    89  
    90  	serverVersions, err := f.versionManager.ServerVersions(f.config.Receptor())
    91  	if err != nil {
    92  		f.ui.SayLine("Error: " + err.Error())
    93  		f.exitHandler.Exit(exit_codes.CommandFailed)
    94  		return
    95  	}
    96  
    97  	f.ui.SayLine("Server: " + serverVersions.LatticeRelease)
    98  	f.ui.SayLine("\tImage: " + serverVersions.LatticeReleaseImage)
    99  	f.ui.SayLine("\tCF: " + serverVersions.CFRelease)
   100  	f.ui.SayLine("\tDiego: " + serverVersions.DiegoRelease)
   101  	f.ui.SayLine("\tGarden-Linux: " + serverVersions.GardenLinuxRelease)
   102  	f.ui.SayLine("\tRouting: " + serverVersions.CFRoutingRelease)
   103  }