From 32686c139a57a64a6653eac042ffd4a5727a74e7 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Fri, 4 Dec 2015 20:44:27 +0000 Subject: [PATCH] Add initial implementation of scopes. This is a work in progress but sufficient for review. TODO: Rename the "config" and "activity" commands to avoid name clash with their scope specifiers. Replace "list" commands with their equivalents from "RESTish" syntax. Add tests. The scope specifiers (and their aliases) are: application (app, a) entity (ent, e) effector (eff, f) config (conf, con, c) activity (act, v) For example: br app myweb ent mycluster entity-children br a myweb e mycluster config br a myweb e myweb activity RJRwVqXR activity-children br a myweb e mycluster list entities br app myweb status --- brooklyn.go | 9 ++- command/command.go | 3 +- command_factory/factory.go | 78 +++++++++--------- command_runner/runner.go | 9 ++- commands/access.go | 5 +- commands/activities.go | 8 +- commands/activity-children.go | 7 +- commands/activity-stream.go | 7 +- commands/activity.go | 7 +- commands/add-catalog.go | 7 +- commands/add-children.go | 7 +- commands/add-policy.go | 5 +- commands/applications.go | 5 +- commands/catalog.go | 5 +- commands/config.go | 7 +- commands/create.go | 7 +- commands/delete.go | 7 +- commands/destroy-policy.go | 7 +- commands/effectors.go | 9 ++- commands/entities.go | 9 ++- commands/entity-children.go | 9 ++- commands/list.go | 11 +-- commands/locations.go | 5 +- commands/login.go | 5 +- commands/policies.go | 7 +- commands/policy.go | 7 +- commands/rename-entity.go | 7 +- commands/sensor.go | 7 +- commands/sensors.go | 11 +-- commands/{set-config.go => set.go} | 9 ++- commands/spec.go | 7 +- commands/start-policy.go | 7 +- commands/{application.go => status.go} | 9 ++- commands/stop-policy.go | 7 +- commands/tree.go | 5 +- commands/version.go | 5 +- scope/scope.go | 105 +++++++++++++++++++++++++ 37 files changed, 291 insertions(+), 140 deletions(-) rename commands/{set-config.go => set.go} (67%) rename commands/{application.go => status.go} (77%) create mode 100644 scope/scope.go diff --git a/brooklyn.go b/brooklyn.go index 7fbf448..8ac8c3d 100644 --- a/brooklyn.go +++ b/brooklyn.go @@ -8,6 +8,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/io" "os" "path/filepath" + "github.com/brooklyncentral/brooklyn-cli/scope" ) func getNetworkCredentialsFromConfig(yamlMap map[string]interface{}) (string, string, string){ @@ -32,8 +33,10 @@ func main() { //target, username, password := "http://192.168.50.101:8081", "brooklyn", "Sns4Hh9j7l" network := net.NewNetwork(target, username, password) cmdFactory := command_factory.NewFactory(network, config) - cmdRunner := command_runner.NewRunner(cmdFactory) + + args, scope := scope.ScopeArguments(os.Args) + cmdRunner := command_runner.NewRunner(scope, cmdFactory) metaDatas := cmdFactory.CommandMetadatas() - theApp := app.NewApp(filepath.Base(os.Args[0]), cmdRunner, metaDatas...) - theApp.Run(os.Args) + theApp := app.NewApp(filepath.Base(args[0]), cmdRunner, metaDatas...) + theApp.Run(args) } diff --git a/command/command.go b/command/command.go index 26a00e9..0eb2ff5 100644 --- a/command/command.go +++ b/command/command.go @@ -3,9 +3,10 @@ package command import ( "github.com/codegangsta/cli" "github.com/brooklyncentral/brooklyn-cli/command_metadata" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Command interface { Metadata() command_metadata.CommandMetadata - Run(context *cli.Context) + Run(scope scope.Scope, context *cli.Context) } diff --git a/command_factory/factory.go b/command_factory/factory.go index 9d25228..ce4e217 100644 --- a/command_factory/factory.go +++ b/command_factory/factory.go @@ -22,51 +22,58 @@ type concreteFactory struct { subCommands map[string]map[string]command.Command } + func NewFactory(network *net.Network, config *io.Config) (factory concreteFactory) { factory.cmdsByName = make(map[string]command.Command) factory.subCommands = make(map[string]map[string]command.Command) - factory.cmdsByName["access"] = commands.NewAccess(network) - //factory.cmdsByName["activities"] = commands.NewActivities(network) - factory.cmdsByName["activity"] = commands.NewActivity(network) - factory.cmdsByName["activity-children"] = commands.NewActivityChildren(network) - factory.cmdsByName["activity-stream"] = commands.NewActivityStream(network) - factory.cmdsByName["add-catalog"] = commands.NewAddCatalog(network) - factory.cmdsByName["add-children"] = commands.NewAddChildren(network) - factory.cmdsByName["application"] = commands.NewApplication(network) - factory.cmdsByName["catalog"] = commands.NewCatalog(network) - factory.cmdsByName["config"] = commands.NewConfig(network) - factory.cmdsByName["create"] = commands.NewCreate(network) - factory.cmdsByName["delete"] = commands.NewDelete(network) - factory.cmdsByName["destroy-policy"] = commands.NewDestroyPolicy(network) - factory.cmdsByName["entity-children"] = commands.NewChildren(network) + factory.command(commands.NewAccess(network)) + //factory.command(commands.NewActivities(network)) + factory.command(commands.NewActivity(network)) + factory.command(commands.NewActivityChildren(network)) + factory.command(commands.NewActivityStream(network)) + factory.command(commands.NewAddCatalog(network)) + factory.command(commands.NewAddChildren(network)) + factory.command(commands.NewApplication(network)) + factory.command(commands.NewCatalog(network)) + factory.command(commands.NewConfig(network)) + factory.command(commands.NewCreate(network)) + factory.command(commands.NewDelete(network)) + factory.command(commands.NewDestroyPolicy(network)) + factory.command(commands.NewChildren(network)) listCommand := commands.NewList(network) - factory.cmdsByName["list"] = listCommand; - factory.subCommand("list", "application", listCommand.SubCommand(commands.ListApplicationCommand)) - factory.subCommand("list", "effector", listCommand.SubCommand(commands.ListEffectorCommand)) - factory.subCommand("list", "entity", listCommand.SubCommand(commands.ListEntityCommand)) - factory.subCommand("list", "sensor", listCommand.SubCommand(commands.ListSensorCommand)) - factory.cmdsByName["locations"] = commands.NewLocations(network) - factory.cmdsByName["login"] = commands.NewLogin(network, config) - factory.cmdsByName["policies"] = commands.NewPolicies(network) - factory.cmdsByName["policy"] = commands.NewPolicy(network) - factory.cmdsByName["rename-entity"] = commands.NewRename(network) - factory.cmdsByName["sensor"] = commands.NewSensor(network) - factory.cmdsByName["set-config"] = commands.NewSetConfig(network) - factory.cmdsByName["spec"] = commands.NewSpec(network) - factory.cmdsByName["start-policy"] = commands.NewStartPolicy(network) - factory.cmdsByName["stop-policy"] = commands.NewStopPolicy(network) - factory.cmdsByName["tree"] = commands.NewTree(network) - factory.cmdsByName["version"] = commands.NewVersion(network) + factory.command(listCommand); + factory.subCommand(listCommand, commands.ListApplicationCommand) + factory.subCommand(listCommand, commands.ListEffectorCommand) + factory.subCommand(listCommand, commands.ListEntityCommand) + factory.subCommand(listCommand, commands.ListSensorCommand) + factory.command(commands.NewLocations(network)) + factory.command(commands.NewLogin(network, config)) + factory.command(commands.NewPolicies(network)) + factory.command(commands.NewPolicy(network)) + factory.command(commands.NewRename(network)) + factory.command(commands.NewSensor(network)) + factory.command(commands.NewSetConfig(network)) + factory.command(commands.NewSpec(network)) + factory.command(commands.NewStartPolicy(network)) + factory.command(commands.NewStopPolicy(network)) + factory.command(commands.NewTree(network)) + factory.command(commands.NewVersion(network)) return factory } -func (factory concreteFactory) subCommand(commandName string, subCommandName string, subCommand command.Command) { - if nil == factory.subCommands[commandName] { - factory.subCommands[commandName] = make(map[string]command.Command) + +func (factory *concreteFactory) command(command command.Command) { + factory.cmdsByName[command.Metadata().Name] = command +} + +// TODO make this more generic - instead of List use a generic Command type +func (factory concreteFactory) subCommand(listCommand *commands.List, subCommandName string) { + if nil == factory.subCommands[listCommand.Metadata().Name] { + factory.subCommands[listCommand.Metadata().Name] = make(map[string]command.Command) } - factory.subCommands[commandName][subCommandName] = subCommand + factory.subCommands[listCommand.Metadata().Name][subCommandName] = listCommand.SubCommand(subCommandName) } func (f concreteFactory) GetByCmdName(cmdName string) (cmd command.Command, err error) { @@ -84,6 +91,7 @@ func (f concreteFactory) GetByCmdName(cmdName string) (cmd command.Command, err } func (f concreteFactory) GetBySubCmdName(cmdName string, subCmdName string) (cmd command.Command, err error) { + _, hasPrimary := f.subCommands[cmdName] if hasPrimary { cmd, found := f.subCommands[cmdName][subCmdName] diff --git a/command_runner/runner.go b/command_runner/runner.go index f609221..e9f9160 100644 --- a/command_runner/runner.go +++ b/command_runner/runner.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/codegangsta/cli" "github.com/brooklyncentral/brooklyn-cli/command_factory" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Runner interface { @@ -13,10 +14,12 @@ type Runner interface { type ConcreteRunner struct { cmdFactory command_factory.Factory + scope scope.Scope } -func NewRunner(cmdFactory command_factory.Factory) (runner ConcreteRunner) { +func NewRunner(scope scope.Scope, cmdFactory command_factory.Factory) (runner ConcreteRunner) { runner.cmdFactory = cmdFactory + runner.scope = scope return } @@ -27,7 +30,7 @@ func (runner ConcreteRunner) RunCmdByName(cmdName string, c *cli.Context) error return err } - cmd.Run(c) + cmd.Run(runner.scope, c) return nil } @@ -38,6 +41,6 @@ func (runner ConcreteRunner) RunSubCmdByName(cmdName string, subCommand string, return err } - cmd.Run(c) + cmd.Run(runner.scope, c) return nil } diff --git a/commands/access.go b/commands/access.go index 4644077..48488f4 100644 --- a/commands/access.go +++ b/commands/access.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/access_control" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Access struct { @@ -22,12 +23,12 @@ func (cmd *Access) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "access", Description: "Show access control", - Usage: "BROOKLYN_NAME access", + Usage: "BROOKLYN_NAME [ SCOPE ] access", Flags: []cli.Flag{}, } } -func (cmd *Access) Run(c *cli.Context) { +func (cmd *Access) Run(scope scope.Scope, c *cli.Context) { access := access_control.Access(cmd.network) fmt.Println("Location Provisioning Allowed:", access.LocationProvisioningAllowed) } diff --git a/commands/activities.go b/commands/activities.go index 623717b..3bd21dc 100644 --- a/commands/activities.go +++ b/commands/activities.go @@ -23,16 +23,16 @@ func (cmd *Activities) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "activities", Description: "Show the activities for an entity", - Usage: "BROOKLYN_NAME activities APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] activities", Flags: []cli.Flag{}, } } //WIP -//func (cmd *Activities) Run(c *cli.Context) { -// activityList := activities.ActivityList(cmd.network, c.Args()[0], c.Args()[1]) +//func (cmd *Activities) Run(scope scope.Scope, c *cli.Context) { +// activityList := activities.ActivityList(cmd.network, c.Args().First(), c.Args()[1]) // table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"}) // for _, activity := range activityList { // table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus) // } // table.Print() -//} +//} \ No newline at end of file diff --git a/commands/activity-children.go b/commands/activity-children.go index 6171a0e..3b73433 100644 --- a/commands/activity-children.go +++ b/commands/activity-children.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" "time" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type ActivityChildren struct { @@ -23,13 +24,13 @@ func (cmd *ActivityChildren) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "activity-children", Description: "Show the child activities for an entity", - Usage: "BROOKLYN_NAME activity-children ACTIVITY", + Usage: "BROOKLYN_NAME [ SCOPE ] activity-children", Flags: []cli.Flag{}, } } -func (cmd *ActivityChildren) Run(c *cli.Context) { - activityList := activities.ActivityChildren(cmd.network, c.Args()[0]) +func (cmd *ActivityChildren) Run(scope scope.Scope, c *cli.Context) { + activityList := activities.ActivityChildren(cmd.network, scope.Activity) table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"}) for _, activity := range activityList { table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus) diff --git a/commands/activity-stream.go b/commands/activity-stream.go index 136b13f..595c5ce 100644 --- a/commands/activity-stream.go +++ b/commands/activity-stream.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/activities" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type ActivityStream struct { @@ -22,12 +23,12 @@ func (cmd *ActivityStream) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "activity-stream", Description: "Show the stream for a given activity", - Usage: "BROOKLYN_NAME activity-stream ACTIVITY STREAM_ID", + Usage: "BROOKLYN_NAME [ SCOPE ] activity-stream STREAM_ID", Flags: []cli.Flag{}, } } -func (cmd *ActivityStream) Run(c *cli.Context) { - activityStream := activities.ActivityStream(cmd.network, c.Args()[0], c.Args()[1]) +func (cmd *ActivityStream) Run(scope scope.Scope, c *cli.Context) { + activityStream := activities.ActivityStream(cmd.network, scope.Activity, c.Args().First()) fmt.Println(activityStream) } diff --git a/commands/activity.go b/commands/activity.go index c4fcb49..57fce38 100644 --- a/commands/activity.go +++ b/commands/activity.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" "time" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Activity struct { @@ -23,13 +24,13 @@ func (cmd *Activity) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "activity", Description: "Show the activity for an entity", - Usage: "BROOKLYN_NAME activity ACTIVITY", + Usage: "BROOKLYN_NAME [ SCOPE ] activity ACTIVITYID", Flags: []cli.Flag{}, } } -func (cmd *Activity) Run(c *cli.Context) { - activity := activities.Activity(cmd.network, c.Args()[0]) +func (cmd *Activity) Run(scope scope.Scope, c *cli.Context) { + activity := activities.Activity(cmd.network, c.Args().First()) table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"}) table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus) diff --git a/commands/add-catalog.go b/commands/add-catalog.go index b8d30f2..fdf9384 100644 --- a/commands/add-catalog.go +++ b/commands/add-catalog.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/catalog" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type AddCatalog struct { @@ -22,12 +23,12 @@ func (cmd *AddCatalog) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "add-catalog", Description: "Add a new catalog item from the supplied YAML", - Usage: "BROOKLYN_NAME add-catalog FILEPATH", + Usage: "BROOKLYN_NAME [ SCOPE ] add-catalog FILEPATH", Flags: []cli.Flag{}, } } -func (cmd *AddCatalog) Run(c *cli.Context) { - create := catalog.AddCatalog(cmd.network, c.Args()[0]) +func (cmd *AddCatalog) Run(scope scope.Scope, c *cli.Context) { + create := catalog.AddCatalog(cmd.network, c.Args().First()) fmt.Println(create) } diff --git a/commands/add-children.go b/commands/add-children.go index 7791c1c..339e8ea 100644 --- a/commands/add-children.go +++ b/commands/add-children.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" "time" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type AddChildren struct { @@ -23,13 +24,13 @@ func (cmd *AddChildren) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "add-children", Description: "Add a child or children to this entity from the supplied YAML", - Usage: "BROOKLYN_NAME add-children FILEPATH", + Usage: "BROOKLYN_NAME [ SCOPE ] add-children FILEPATH", Flags: []cli.Flag{}, } } -func (cmd *AddChildren) Run(c *cli.Context) { - activity := entities.AddChildren(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2]) +func (cmd *AddChildren) Run(scope scope.Scope, c *cli.Context) { + activity := entities.AddChildren(cmd.network, scope.Application, scope.Entity, c.Args().First()) table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"}) table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus) diff --git a/commands/add-policy.go b/commands/add-policy.go index 30cb592..b0c2f0e 100644 --- a/commands/add-policy.go +++ b/commands/add-policy.go @@ -5,6 +5,7 @@ import ( //"github.com/brooklyncentral/brooklyn-cli/api/entity_policies" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type AddPolicy struct { @@ -21,11 +22,11 @@ func (cmd *AddPolicy) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "add-policy", Description: "Add a new policy", - Usage: "BROOKLYN_NAME add-policy APPLICATION ENTITY POLICY_TYPE", + Usage: "BROOKLYN_NAME [ SCOPE ] add-policy APPLICATION ENTITY POLICY_TYPE", Flags: []cli.Flag{}, } } -func (cmd *AddPolicy) Run(c *cli.Context) { +func (cmd *AddPolicy) Run(scope scope.Scope, c *cli.Context) { // Todo } diff --git a/commands/applications.go b/commands/applications.go index 56a6c9a..ae453a5 100644 --- a/commands/applications.go +++ b/commands/applications.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/terminal" "strings" "github.com/brooklyncentral/brooklyn-cli/command_metadata" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type ListApplicationSubCommand struct { @@ -23,12 +24,12 @@ func (cmd *ListApplicationSubCommand) Metadata() command_metadata.CommandMetadat return command_metadata.CommandMetadata{ Name: "application", Description: "Show the status and location of running applications", - Usage: "list application", + Usage: "BROOKLYN_NAME [ SCOPE ] application", Flags: []cli.Flag{}, } } -func (cmd *ListApplicationSubCommand) Run(c *cli.Context) { +func (cmd *ListApplicationSubCommand) Run(scope scope.Scope, c *cli.Context) { applications := application.Applications(cmd.network) table := terminal.NewTable([]string{"Id", "Name", "Status", "Location"}) diff --git a/commands/catalog.go b/commands/catalog.go index bb29513..f847f66 100644 --- a/commands/catalog.go +++ b/commands/catalog.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Catalog struct { @@ -22,12 +23,12 @@ func (cmd *Catalog) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "catalog", Description: "List the available catalog applications", - Usage: "BROOKLYN_NAME catalog", + Usage: "BROOKLYN_NAME [ SCOPE ] catalog", Flags: []cli.Flag{}, } } -func (cmd *Catalog) Run(c *cli.Context) { +func (cmd *Catalog) Run(scope scope.Scope, c *cli.Context) { catalog := catalog.Catalog(cmd.network) table := terminal.NewTable([]string{"Id", "Name", "Description"}) for _, app := range catalog { diff --git a/commands/config.go b/commands/config.go index 9f48c2b..dfa9dae 100644 --- a/commands/config.go +++ b/commands/config.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Config struct { @@ -23,13 +24,13 @@ func (cmd *Config) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "config", Description: "Show the config for an application and entity", - Usage: "BROOKLYN_NAME config APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] config", Flags: []cli.Flag{}, } } -func (cmd *Config) Run(c *cli.Context) { - config := entity_config.ConfigCurrentState(cmd.network, c.Args()[0], c.Args()[1]) +func (cmd *Config) Run(scope scope.Scope, c *cli.Context) { + config := entity_config.ConfigCurrentState(cmd.network, scope.Application, scope.Entity) table := terminal.NewTable([]string{"Key", "Value"}) for key, value := range config { table.Add(key, fmt.Sprintf("%v", value)) diff --git a/commands/create.go b/commands/create.go index ee07ee2..2562cf4 100644 --- a/commands/create.go +++ b/commands/create.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/application" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Create struct { @@ -22,12 +23,12 @@ func (cmd *Create) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "create", Description: "Create a new brooklyn application from the supplied YAML", - Usage: "BROOKLYN_NAME create FILEPATH", + Usage: "BROOKLYN_NAME [ SCOPE ] create FILEPATH", Flags: []cli.Flag{}, } } -func (cmd *Create) Run(c *cli.Context) { - create := application.Create(cmd.network, c.Args()[0]) +func (cmd *Create) Run(scope scope.Scope, c *cli.Context) { + create := application.Create(cmd.network, c.Args().First()) fmt.Println(create) } diff --git a/commands/delete.go b/commands/delete.go index 3aa71d5..7455bd5 100644 --- a/commands/delete.go +++ b/commands/delete.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/application" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Delete struct { @@ -22,12 +23,12 @@ func (cmd *Delete) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "delete", Description: "Delete a brooklyn application", - Usage: "BROOKLYN_NAME create FILEPATH", + Usage: "BROOKLYN_NAME [ SCOPE ] delete", Flags: []cli.Flag{}, } } -func (cmd *Delete) Run(c *cli.Context) { - del := application.Delete(cmd.network, c.Args()[0]) +func (cmd *Delete) Run(scope scope.Scope, c *cli.Context) { + del := application.Delete(cmd.network, scope.Application) fmt.Println(del) } diff --git a/commands/destroy-policy.go b/commands/destroy-policy.go index e243ccc..4d30346 100644 --- a/commands/destroy-policy.go +++ b/commands/destroy-policy.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entity_policies" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type DestroyPolicy struct { @@ -22,12 +23,12 @@ func (cmd *DestroyPolicy) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "destroy-policy", Description: "Destroy a policy", - Usage: "BROOKLYN_NAME destroy-policy APPLICATION ENTITY POLICY", + Usage: "BROOKLYN_NAME [ SCOPE ] destroy-policy POLICY", Flags: []cli.Flag{}, } } -func (cmd *DestroyPolicy) Run(c *cli.Context) { - spec := entity_policies.DestroyPolicy(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2]) +func (cmd *DestroyPolicy) Run(scope scope.Scope, c *cli.Context) { + spec := entity_policies.DestroyPolicy(cmd.network, scope.Application, scope.Entity, c.Args().First()) fmt.Println(spec) } diff --git a/commands/effectors.go b/commands/effectors.go index 901bce3..dc51522 100644 --- a/commands/effectors.go +++ b/commands/effectors.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" "strings" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type ListEffectorSubCommand struct { @@ -21,15 +22,15 @@ func NewListEffector(network *net.Network) (cmd *ListEffectorSubCommand) { func (cmd *ListEffectorSubCommand) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ - Name: "effector", + Name: "effectors", Description: "Show the list of effectors for an application and entity", - Usage: "list effector APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] effectors", Flags: []cli.Flag{}, } } -func (cmd *ListEffectorSubCommand) Run(c *cli.Context) { - effectors := entity_effectors.EffectorList(cmd.network, c.Args()[0], c.Args()[1]) +func (cmd *ListEffectorSubCommand) Run(scope scope.Scope, c *cli.Context) { + effectors := entity_effectors.EffectorList(cmd.network, scope.Application, scope.Entity) table := terminal.NewTable([]string{"Name", "Description", "Parameters"}) for _, effector := range effectors { var parameters []string diff --git a/commands/entities.go b/commands/entities.go index 55a453c..1a73528 100644 --- a/commands/entities.go +++ b/commands/entities.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" "github.com/brooklyncentral/brooklyn-cli/command_metadata" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type ListEntitySubCommand struct { @@ -20,15 +21,15 @@ func NewListEntity(network *net.Network) (cmd *ListEntitySubCommand) { func (cmd *ListEntitySubCommand) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ - Name: "entity", + Name: "entities", Description: "Show the entities for an application", - Usage: "list entity APPLICATION", + Usage: "BROOKLYN_NAME [ SCOPE ] entities", Flags: []cli.Flag{}, } } -func (cmd *ListEntitySubCommand) Run(c *cli.Context) { - entityList := entities.EntityList(cmd.network, c.Args()[0]) +func (cmd *ListEntitySubCommand) Run(scope scope.Scope, c *cli.Context) { + entityList := entities.EntityList(cmd.network, scope.Application) table := terminal.NewTable([]string{"Id", "Name", "Type"}) for _, entity := range entityList { table.Add(entity.Id, entity.Name, entity.Type) diff --git a/commands/entity-children.go b/commands/entity-children.go index 40736ed..e9a4593 100644 --- a/commands/entity-children.go +++ b/commands/entity-children.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Children struct { @@ -21,14 +22,14 @@ func NewChildren(network *net.Network) (cmd *Children) { func (cmd *Children) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "entity-children", - Description: "Show the children of an application's entity", - Usage: "BROOKLYN_NAME children APPLICATION ENTITY", + Description: "Show the children of an entity", + Usage: "BROOKLYN_NAME [ SCOPE ] entity-children", Flags: []cli.Flag{}, } } -func (cmd *Children) Run(c *cli.Context) { - entityList := entities.Children(cmd.network, c.Args()[0], c.Args()[1]) +func (cmd *Children) Run(scope scope.Scope, c *cli.Context) { + entityList := entities.Children(cmd.network, scope.Application, scope.Entity) table := terminal.NewTable([]string{"Id", "Name", "Type"}) for _, entity := range entityList { table.Add(entity.Id, entity.Name, entity.Type) diff --git a/commands/list.go b/commands/list.go index 5aa27a2..ec0c7a1 100644 --- a/commands/list.go +++ b/commands/list.go @@ -7,6 +7,7 @@ import ( "fmt" "strings" "github.com/brooklyncentral/brooklyn-cli/command" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type List struct { @@ -27,9 +28,9 @@ func NewList(network *net.Network) (cmd *List) { } const ListApplicationCommand = "application" -const ListEntityCommand = "entity" -const ListSensorCommand = "sensor" -const ListEffectorCommand = "effector" +const ListEntityCommand = "entities" +const ListSensorCommand = "sensors" +const ListEffectorCommand = "effectors" var listCommands = []string { ListApplicationCommand, @@ -47,7 +48,7 @@ func (cmd *List) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "list", Description: "List details for a variety of operands", - Usage: "BROOKLYN_NAME list (" + listCommandsUsage + ")", + Usage: "BROOKLYN_NAME [ SCOPE ] list (" + listCommandsUsage + ")", Flags: []cli.Flag{}, Operands: []command_metadata.CommandMetadata { cmd.SubCommand(ListApplicationCommand).Metadata(), @@ -58,6 +59,6 @@ func (cmd *List) Metadata() command_metadata.CommandMetadata { } } -func (cmd *List) Run(c *cli.Context) { +func (cmd *List) Run(scope scope.Scope, c *cli.Context) { fmt.Printf( "Unrecognised item for list, please use one of (%s)\n", listCommandsUsage) } \ No newline at end of file diff --git a/commands/locations.go b/commands/locations.go index 18f3e2f..4f0e344 100644 --- a/commands/locations.go +++ b/commands/locations.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Locations struct { @@ -22,12 +23,12 @@ func (cmd *Locations) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "locations", Description: "List the available locations", - Usage: "BROOKLYN_NAME locations", + Usage: "BROOKLYN_NAME [ SCOPE ] locations", Flags: []cli.Flag{}, } } -func (cmd *Locations) Run(c *cli.Context) { +func (cmd *Locations) Run(scope scope.Scope, c *cli.Context) { locationList := locations.LocationList(cmd.network) table := terminal.NewTable([]string{"Id", "Name", "Spec"}) for _, location := range locationList { diff --git a/commands/login.go b/commands/login.go index 047d1f1..e4728eb 100644 --- a/commands/login.go +++ b/commands/login.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/net" "github.com/codegangsta/cli" "os" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Login struct { @@ -25,12 +26,12 @@ func (cmd *Login) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "login", Description: "Login to brooklyn", - Usage: "BROOKLYN_NAME login URL [USER PASSWORD]", + Usage: "BROOKLYN_NAME [ SCOPE ] login URL [USER PASSWORD]", Flags: []cli.Flag{}, } } -func (cmd *Login) Run(c *cli.Context) { +func (cmd *Login) Run(scope scope.Scope, c *cli.Context) { defer func() { if str := recover(); str != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", str) diff --git a/commands/policies.go b/commands/policies.go index 652e294..92c7ca9 100644 --- a/commands/policies.go +++ b/commands/policies.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Policies struct { @@ -22,13 +23,13 @@ func (cmd *Policies) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "policies", Description: "Show the list of policies for an application and entity", - Usage: "BROOKLYN_NAME policies APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] policies APPLICATION ENTITY", Flags: []cli.Flag{}, } } -func (cmd *Policies) Run(c *cli.Context) { - policies := entity_policies.PolicyList(cmd.network, c.Args()[0], c.Args()[1]) +func (cmd *Policies) Run(scope scope.Scope, c *cli.Context) { + policies := entity_policies.PolicyList(cmd.network, scope.Application, scope.Entity) table := terminal.NewTable([]string{"Name", "State"}) for _, policy := range policies { table.Add(policy.Name, string(policy.State)) diff --git a/commands/policy.go b/commands/policy.go index 7be002c..1179b65 100644 --- a/commands/policy.go +++ b/commands/policy.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entity_policies" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Policy struct { @@ -22,12 +23,12 @@ func (cmd *Policy) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "policy", Description: "Show the status of a policy for an application and entity", - Usage: "BROOKLYN_NAME policy APPLICATION ENITITY POLICY", + Usage: "BROOKLYN_NAME [ SCOPE ] policy POLICY", Flags: []cli.Flag{}, } } -func (cmd *Policy) Run(c *cli.Context) { - policy := entity_policies.PolicyStatus(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2]) +func (cmd *Policy) Run(scope scope.Scope, c *cli.Context) { + policy := entity_policies.PolicyStatus(cmd.network, scope.Application, scope.Entity, c.Args().First()) fmt.Println(policy) } diff --git a/commands/rename-entity.go b/commands/rename-entity.go index 4b38e77..0ff08ea 100644 --- a/commands/rename-entity.go +++ b/commands/rename-entity.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entities" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Rename struct { @@ -22,12 +23,12 @@ func (cmd *Rename) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "rename-entity", Description: "Rename an entity", - Usage: "BROOKLYN_NAME rename-entity APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] rename-entity NEW_NAME", Flags: []cli.Flag{}, } } -func (cmd *Rename) Run(c *cli.Context) { - rename := entities.Rename(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2]) +func (cmd *Rename) Run(scope scope.Scope, c *cli.Context) { + rename := entities.Rename(cmd.network, scope.Application, scope.Entity, c.Args().First()) fmt.Println(rename) } diff --git a/commands/sensor.go b/commands/sensor.go index f171c98..7dec0c1 100644 --- a/commands/sensor.go +++ b/commands/sensor.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entity_sensors" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Sensor struct { @@ -22,12 +23,12 @@ func (cmd *Sensor) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "sensor", Description: "Show the value of a sensor for an application and entity", - Usage: "BROOKLYN_NAME sensor APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] sensor SENSOR_NAME", Flags: []cli.Flag{}, } } -func (cmd *Sensor) Run(c *cli.Context) { - sensor := entity_sensors.SensorValue(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2]) +func (cmd *Sensor) Run(scope scope.Scope, c *cli.Context) { + sensor := entity_sensors.SensorValue(cmd.network, scope.Application, scope.Entity, c.Args().First()) fmt.Println(sensor) } diff --git a/commands/sensors.go b/commands/sensors.go index 056021e..2521a54 100644 --- a/commands/sensors.go +++ b/commands/sensors.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type ListSensorsSubCommand struct { @@ -20,18 +21,18 @@ func NewListSensor(network *net.Network) (cmd *ListSensorsSubCommand) { func (cmd *ListSensorsSubCommand) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ - Name: "sensor", + Name: "sensors", Description: "Show the sensors for an application and entity", - Usage: "list sensor APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] sensors", Flags: []cli.Flag{}, } } -func (cmd *ListSensorsSubCommand) Run(c *cli.Context) { - sensors := entity_sensors.SensorList(cmd.network, c.Args()[0], c.Args()[1]) +func (cmd *ListSensorsSubCommand) Run(scope scope.Scope, c *cli.Context) { + sensors := entity_sensors.SensorList(cmd.network, scope.Application, scope.Entity) table := terminal.NewTable([]string{"Name", "Description", "Value"}) for _, sensor := range sensors { - value := entity_sensors.SensorValue(cmd.network, c.Args()[0], c.Args()[1], sensor.Name) + value := entity_sensors.SensorValue(cmd.network, scope.Application, scope.Entity, sensor.Name) table.Add(sensor.Name, sensor.Description, value) } table.Print() diff --git a/commands/set-config.go b/commands/set.go similarity index 67% rename from commands/set-config.go rename to commands/set.go index 48242b5..cdabdd8 100644 --- a/commands/set-config.go +++ b/commands/set.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entity_config" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type SetConfig struct { @@ -20,14 +21,14 @@ func NewSetConfig(network *net.Network) (cmd *SetConfig) { func (cmd *SetConfig) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ - Name: "set-config", + Name: "set", Description: "Set config for an entity", - Usage: "BROOKLYN_NAME set-config APPLICATION ENTITY CONFIG_KEY VALUE", + Usage: "BROOKLYN_NAME [ SCOPE ] set VALUE", Flags: []cli.Flag{}, } } -func (cmd *SetConfig) Run(c *cli.Context) { - response := entity_config.SetConfig(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2], c.Args()[3]) +func (cmd *SetConfig) Run(scope scope.Scope, c *cli.Context) { + response := entity_config.SetConfig(cmd.network, scope.Application, scope.Entity, scope.Config, c.Args().First()) fmt.Println(response) } diff --git a/commands/spec.go b/commands/spec.go index 8b161c4..7ddfe03 100644 --- a/commands/spec.go +++ b/commands/spec.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entities" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Spec struct { @@ -22,12 +23,12 @@ func (cmd *Spec) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "spec", Description: "Get the YAML spec used to create the entity, if available", - Usage: "BROOKLYN_NAME spec APPLICATION ENTITY", + Usage: "BROOKLYN_NAME [ SCOPE ] spec", Flags: []cli.Flag{}, } } -func (cmd *Spec) Run(c *cli.Context) { - spec := entities.Spec(cmd.network, c.Args()[0], c.Args()[1]) +func (cmd *Spec) Run(scope scope.Scope, c *cli.Context) { + spec := entities.Spec(cmd.network, scope.Application, scope.Entity) fmt.Println(spec) } diff --git a/commands/start-policy.go b/commands/start-policy.go index a01b60a..5fd5362 100644 --- a/commands/start-policy.go +++ b/commands/start-policy.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entity_policies" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type StartPolicy struct { @@ -22,12 +23,12 @@ func (cmd *StartPolicy) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "start-policy", Description: "Start or resume a policy", - Usage: "BROOKLYN_NAME start-policy APPLICATION ENTITY POLICY", + Usage: "BROOKLYN_NAME [ SCOPE ] start-policy POLICY", Flags: []cli.Flag{}, } } -func (cmd *StartPolicy) Run(c *cli.Context) { - spec := entity_policies.StartPolicy(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2]) +func (cmd *StartPolicy) Run(scope scope.Scope, c *cli.Context) { + spec := entity_policies.StartPolicy(cmd.network, scope.Application, scope.Entity, c.Args().First()) fmt.Println(spec) } diff --git a/commands/application.go b/commands/status.go similarity index 77% rename from commands/application.go rename to commands/status.go index 9a9e734..aca3d8b 100644 --- a/commands/application.go +++ b/commands/status.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/net" "github.com/brooklyncentral/brooklyn-cli/terminal" "strings" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Application struct { @@ -21,15 +22,15 @@ func NewApplication(network *net.Network) (cmd *Application) { func (cmd *Application) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ - Name: "application", + Name: "status", Description: "Show the status and location of a running application", - Usage: "BROOKLYN_NAME application APPLICATION", + Usage: "BROOKLYN_NAME [ SCOPE ] status", Flags: []cli.Flag{}, } } -func (cmd *Application) Run(c *cli.Context) { - application := application.Application(cmd.network, c.Args()[0]) +func (cmd *Application) Run(scope scope.Scope, c *cli.Context) { + application := application.Application(cmd.network, scope.Application) table := terminal.NewTable([]string{"Name", "Id", "Status", "Location"}) table.Add(application.Spec.Name, application.Id, string(application.Status), strings.Join(application.Spec.Locations, ", ")) diff --git a/commands/stop-policy.go b/commands/stop-policy.go index 36d2763..a2a0bd3 100644 --- a/commands/stop-policy.go +++ b/commands/stop-policy.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/entity_policies" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type StopPolicy struct { @@ -22,12 +23,12 @@ func (cmd *StopPolicy) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "stop-policy", Description: "Suspends a policy", - Usage: "BROOKLYN_NAME stop-policy APPLICATION ENTITY POLICY", + Usage: "BROOKLYN_NAME [ SCOPE ] stop-policy POLICY", Flags: []cli.Flag{}, } } -func (cmd *StopPolicy) Run(c *cli.Context) { - spec := entity_policies.StopPolicy(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2]) +func (cmd *StopPolicy) Run(scope scope.Scope, c *cli.Context) { + spec := entity_policies.StopPolicy(cmd.network, scope.Application, scope.Entity, c.Args().First()) fmt.Println(spec) } diff --git a/commands/tree.go b/commands/tree.go index 1585ba8..6bed741 100644 --- a/commands/tree.go +++ b/commands/tree.go @@ -7,6 +7,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/models" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Tree struct { @@ -23,12 +24,12 @@ func (cmd *Tree) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "tree", Description: "Show the tree of all applications", - Usage: "BROOKLYN_NAME tree", + Usage: "BROOKLYN_NAME [ SCOPE ] tree", Flags: []cli.Flag{}, } } -func (cmd *Tree) Run(c *cli.Context) { +func (cmd *Tree) Run(scope scope.Scope, c *cli.Context) { trees := application.Tree(cmd.network) cmd.printTrees(trees, "") } diff --git a/commands/version.go b/commands/version.go index 57de5e9..2634ac6 100644 --- a/commands/version.go +++ b/commands/version.go @@ -6,6 +6,7 @@ import ( "github.com/brooklyncentral/brooklyn-cli/api/version" "github.com/brooklyncentral/brooklyn-cli/command_metadata" "github.com/brooklyncentral/brooklyn-cli/net" + "github.com/brooklyncentral/brooklyn-cli/scope" ) type Version struct { @@ -22,12 +23,12 @@ func (cmd *Version) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "version", Description: "Display the version of the connected Brooklyn", - Usage: "BROOKLYN_NAME version", + Usage: "BROOKLYN_NAME [ SCOPE ] version", Flags: []cli.Flag{}, } } -func (cmd *Version) Run(c *cli.Context) { +func (cmd *Version) Run(scope scope.Scope, c *cli.Context) { version := version.Version(cmd.network) fmt.Println(version) } diff --git a/scope/scope.go b/scope/scope.go new file mode 100644 index 0000000..aa8dd63 --- /dev/null +++ b/scope/scope.go @@ -0,0 +1,105 @@ +package scope +import ( + "strings" +) + +type Scope struct { + Application string + Entity string + Effector string + Config string + Activity string +} + +func (scope Scope) String() string { + return strings.Join([]string{ + "{Application: ", scope.Application, + ", Entity: ",scope.Entity, + ", Effector: ", scope.Effector, + ", Config: ", scope.Config, + ", Activity: ", scope.Activity, + "}", + }, "") +} + +func application(scope *Scope, id string) { + scope.Application = id +} + +func entity(scope *Scope, id string) { + scope.Entity = id +} + +func effector(scope *Scope, id string) { + scope.Effector = id +} + +func config(scope *Scope, id string) { + scope.Config = id +} + +func activity(scope *Scope, id string) { + scope.Activity = id +} + +var scopeSpecifier = map[string] func(scope *Scope, id string) { + "application" : application, + "app" : application, + "a" : application, + "entity" : entity, + "ent" : entity, + "e" : entity, + "effector" : effector, + "eff" : effector, + "f" : effector, + "config" : config, + "conf" : config, + "con" : config, + "c" : config, + "activity" : activity, + "act" : activity, + "v" : activity, +} + + +// Scopes the arguments. +// Assumes the arguments are a copy of the program args, including the first member that defines the program name. +// Removes the scope arguments from the array and applies them to a scope object. +// Returns the remaining arguments with the program name restored to first argument. +// For example with input +// br application 1 entity 2 doSomething +// the function will return ([]string{"br", "doSomething"}, Scope{Application:1, Entity:2}) +func ScopeArguments(args []string) ([]string, Scope) { + scope := Scope{} + + if len(args) < 2 { + return args, scope + } + + command := args[0] + args = args[1:] + + allScopesFound := false + for !allScopesFound && len(args) >= 2 { + if setAppropriateScope, nameOfAScope := scopeSpecifier[args[0]]; nameOfAScope { + setAppropriateScope(&scope, args[1]) + args = args[2:] + } else { + allScopesFound = true + } + } + + args = prepend(command, args) + + return args, scope +} + + +func prepend(v string, args[]string) []string { + result := make([]string, len(args) + 1) + result[0] = v + for i, a := range(args) { + result[i+1] = a + } + return result +} \ No newline at end of file