Skip to content

Commit

Permalink
Make -a param optional for some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Aug 1, 2024
1 parent 4339d84 commit 1f96544
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 52 deletions.
4 changes: 2 additions & 2 deletions tsuru/admin/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func (c *AppRoutesRebuild) Info() *cmd.Info {
return &cmd.Info{
Name: "app-routes-rebuild",
MinArgs: 0,
Usage: "app-routes-rebuild -a <app-name>",
Usage: "app-routes-rebuild <app-name>",
Desc: `Rebuild routes for an application.
This can be used to recover from some failure in the router that caused
existing routes to be lost.`,
}
}

func (c *AppRoutesRebuild) Run(ctx *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppName(ctx.Args)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions tsuru/admin/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (*AppQuotaView) Info() *cmd.Info {
return &cmd.Info{
Name: "app-quota-view",
MinArgs: 0,
Usage: "app-quota-view [-a/--app appname]",
Usage: "app-quota-view [appname]",
Desc: "Displays the current usage and limit of the given app.",
}
}
Expand All @@ -117,7 +117,7 @@ func (c *AppQuotaView) Flags() *gnuflag.FlagSet {

func (c *AppQuotaView) Run(context *cmd.Context) error {
context.RawOutput()
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down Expand Up @@ -164,7 +164,7 @@ The new limit must be an integer, it may also be "unlimited".`

func (c *AppQuotaChange) Run(context *cmd.Context) error {
context.RawOutput()
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tsuru/admin/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *S) TestAppQuotaViewRunFailure(c *check.C) {
trans := cmdtest.Transport{Message: "app not found", Status: http.StatusNotFound}
s.setupFakeTransport(&trans)
command := AppQuotaView{}
command.Flags().Parse(true, []string{"--app", "hibria"})
command.Flags().Parse(true, []string{})
err := command.Run(&context)
c.Assert(err, check.NotNil)
c.Assert(tsuruHTTP.UnwrapErr(err).Error(), check.Equals, "app not found")
Expand Down
14 changes: 13 additions & 1 deletion tsuru/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ type AppNameMixIn struct {
appName string
}

func (cmd *AppNameMixIn) AppName() (string, error) {
func (cmd *AppNameMixIn) AppName(args []string) (string, error) {
if len(args) > 0 {
if cmd.appName != "" {
return "", errors.New("You can't use the app flag and specify the app name as an argument at the same time.")
}

return args[0], nil
}

return cmd.AppNameByFlag()
}

func (cmd *AppNameMixIn) AppNameByFlag() (string, error) {
if cmd.appName == "" {
return "", errors.Errorf(`The name of the app is required.
Expand Down
22 changes: 19 additions & 3 deletions tsuru/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,38 @@ var _ = check.Suite(&S{})
func (s *S) TestAppNameMixInWithFlagDefined(c *check.C) {
g := AppNameMixIn{}
g.Flags().Parse(true, []string{"--app", "myapp"})
name, err := g.AppName()
name, err := g.AppNameByFlag()
c.Assert(err, check.IsNil)
c.Assert(name, check.Equals, "myapp")
}

func (s *S) TestAppNameMixInWithShortFlagDefined(c *check.C) {
g := AppNameMixIn{}
g.Flags().Parse(true, []string{"-a", "myapp"})
name, err := g.AppName()
name, err := g.AppNameByFlag()
c.Assert(err, check.IsNil)
c.Assert(name, check.Equals, "myapp")
}

func (s *S) TestAppNameMixInArgs(c *check.C) {
g := AppNameMixIn{}
g.Flags().Parse(true, []string{})
name, err := g.AppName([]string{"myapp"})
c.Assert(err, check.IsNil)
c.Assert(name, check.Equals, "myapp")
}

func (s *S) TestAppNameMixInArgsConflict(c *check.C) {
g := AppNameMixIn{}
g.Flags().Parse(true, []string{"-a", "myapp"})
_, err := g.AppName([]string{"myapp2"})
c.Assert(err, check.Not(check.IsNil))
c.Assert(err.Error(), check.Equals, "You can't use the app flag and specify the app name as an argument at the same time.")
}

func (s *S) TestAppNameMixInWithoutFlagDefinedFails(c *check.C) {
g := AppNameMixIn{}
name, err := g.AppName()
name, err := g.AppNameByFlag()
c.Assert(name, check.Equals, "")
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, `The name of the app is required.
Expand Down
24 changes: 12 additions & 12 deletions tsuru/client/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ type AppInfo struct {
func (c *AppInfo) Info() *cmd.Info {
return &cmd.Info{
Name: "app-info",
Usage: "app info [-a/--app appname]",
Usage: "app info [appname]",
Desc: `Shows information about a specific app. Its state, platform, git repository,
etc. You need to be a member of a team that has access to the app to be able to
see information about it.`,
Expand All @@ -418,7 +418,7 @@ func (cmd *AppInfo) Flags() *gnuflag.FlagSet {
}

func (c *AppInfo) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down Expand Up @@ -1212,7 +1212,7 @@ app to a team.`,
}

func (c *AppGrant) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -1251,7 +1251,7 @@ authorized team.`,
}

func (c *AppRevoke) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -1470,15 +1470,15 @@ type AppStop struct {
func (c *AppStop) Info() *cmd.Info {
return &cmd.Info{
Name: "app-stop",
Usage: "app stop [-a/--app appname] [-p/--process processname] [--version version]",
Usage: "app stop [appname] [-p/--process processname] [--version version]",
Desc: "Stops an application, or one of the processes of the application.",
MinArgs: 0,
}
}

func (c *AppStop) Run(context *cmd.Context) error {
context.RawOutput()
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down Expand Up @@ -1522,15 +1522,15 @@ type AppStart struct {
func (c *AppStart) Info() *cmd.Info {
return &cmd.Info{
Name: "app-start",
Usage: "app start [-a/--app appname] [-p/--process processname] [--version version]",
Usage: "app start [appname] [-p/--process processname] [--version version]",
Desc: "Starts an application, or one of the processes of the application.",
MinArgs: 0,
}
}

func (c *AppStart) Run(context *cmd.Context) error {
context.RawOutput()
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down Expand Up @@ -1573,7 +1573,7 @@ type AppRestart struct {

func (c *AppRestart) Run(context *cmd.Context) error {
context.RawOutput()
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand All @@ -1600,7 +1600,7 @@ func (c *AppRestart) Run(context *cmd.Context) error {
func (c *AppRestart) Info() *cmd.Info {
return &cmd.Info{
Name: "app-restart",
Usage: "app restart [-a/--app appname] [-p/--process processname] [--version version]",
Usage: "app restart [appname] [-p/--process processname] [--version version]",
Desc: `Restarts an application, or one of the processes of the application.`,
MinArgs: 0,
}
Expand Down Expand Up @@ -1667,7 +1667,7 @@ After unsetting the CNAME from the app, [[tsuru app list]] and [[tsuru app info]
}

func unsetCName(cnames []string, g tsuruClientApp.AppNameMixIn) error {
appName, err := g.AppName()
appName, err := g.AppNameByFlag()
if err != nil {
return err
}
Expand All @@ -1688,7 +1688,7 @@ func unsetCName(cnames []string, g tsuruClientApp.AppNameMixIn) error {
}

func addCName(cnames []string, g tsuruClientApp.AppNameMixIn) error {
appName, err := g.AppName()
appName, err := g.AppNameByFlag()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions tsuru/client/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *AutoScaleSet) Run(ctx *cmd.Context) error {
if err != nil {
return err
}
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func (c *AutoScaleUnset) Run(ctx *cmd.Context) error {
if err != nil {
return err
}
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tsuru/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *AppBuild) Run(context *cmd.Context) error {
return errors.New("You should provide at least one file to build the image.\n")
}

appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions tsuru/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *CertificateSet) Flags() *gnuflag.FlagSet {
}

func (c *CertificateSet) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *CertificateUnset) Flags() *gnuflag.FlagSet {
}

func (c *CertificateUnset) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func (c *CertificateList) Flags() *gnuflag.FlagSet {
}

func (c *CertificateList) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions tsuru/client/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type AppDeployList struct {
func (c *AppDeployList) Info() *cmd.Info {
return &cmd.Info{
Name: "app-deploy-list",
Usage: "app deploy list [-a/--app <appname>]",
Usage: "app deploy list [<appname>]",
Desc: "List information about deploys for an application.",
}
}
Expand All @@ -71,7 +71,7 @@ func (c *AppDeployList) Flags() *gnuflag.FlagSet {
}

func (c *AppDeployList) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func (c *AppDeploy) Run(context *cmd.Context) error {
return errors.New("You can't deploy container image and container file at same time.\n")
}

appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -408,7 +408,7 @@ func (c *AppDeployRollback) Info() *cmd.Info {
}

func (c *AppDeployRollback) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppNameByFlag()
if err != nil {
return err
}
Expand Down Expand Up @@ -454,16 +454,16 @@ func (c *AppDeployRebuild) Info() *cmd.Info {
desc := "Rebuild and deploy the last app image."
return &cmd.Info{
Name: "app-deploy-rebuild",
Usage: "app deploy rebuild [-a/--app appname]",
Usage: "app deploy rebuild [appname]",
Desc: desc,
MinArgs: 0,
MaxArgs: 0,
MaxArgs: 1,
}
}

func (c *AppDeployRebuild) Run(context *cmd.Context) error {
context.RawOutput()
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down Expand Up @@ -507,10 +507,10 @@ func (c *AppDeployRollbackUpdate) Info() *cmd.Info {
`
return &cmd.Info{
Name: "app-deploy-rollback-update",
Usage: "app deploy rollback update [-a/--app appName] [-i/--image imageName] [-d/--disable] [-r/--reason reason]",
Usage: "app deploy rollback update [appName] [-i/--image imageName] [-d/--disable] [-r/--reason reason]",
Desc: desc,
MinArgs: 0,
MaxArgs: 0,
MaxArgs: 1,
}
}

Expand All @@ -531,7 +531,7 @@ func (c *AppDeployRollbackUpdate) Flags() *gnuflag.FlagSet {
}

func (c *AppDeployRollbackUpdate) Run(context *cmd.Context) error {
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions tsuru/client/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type AppLog struct {
func (c *AppLog) Info() *cmd.Info {
return &cmd.Info{
Name: "app-log",
Usage: "app log [-a/--app appname] [-l/--lines numberOfLines] [-s/--source source] [-u/--unit unit] [-f/--follow]",
Usage: "app log [appname] [-l/--lines numberOfLines] [-s/--source source] [-u/--unit unit] [-f/--follow]",
Desc: `Shows log entries for an application. These logs include everything the
application send to stdout and stderr, alongside with logs from tsuru server
(deployments, restarts, etc.)
Expand Down Expand Up @@ -117,7 +117,7 @@ type log struct {

func (c *AppLog) Run(context *cmd.Context) error {
context.RawOutput()
appName, err := c.AppName()
appName, err := c.AppName(context.Args)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 1f96544

Please sign in to comment.