forked from toolshed/abra
refactor!: cobra migrate
This commit is contained in:
@ -2,7 +2,6 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
@ -12,43 +11,62 @@ import (
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
upstream "coopcloud.tech/abra/pkg/upstream/service"
|
||||
stack "coopcloud.tech/abra/pkg/upstream/stack"
|
||||
"github.com/urfave/cli/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var appRestartCommand = cli.Command{
|
||||
Name: "restart",
|
||||
Aliases: []string{"re"},
|
||||
Usage: "Restart an app",
|
||||
UsageText: "abra app restart <domain> [<service>] [options]",
|
||||
Flags: []cli.Flag{
|
||||
internal.AllServicesFlag,
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
Description: `This command restarts services within a deployed app.
|
||||
var AppRestartCommand = &cobra.Command{
|
||||
Use: "restart <app> [[service] | --all-services] [flags]",
|
||||
Aliases: []string{"re"},
|
||||
Short: "Restart an app",
|
||||
Long: `This command restarts services within a deployed app.
|
||||
|
||||
Run "abra app ps <domain>" to see a list of service names.
|
||||
Run "abra app ps <app>" to see a list of service names.
|
||||
|
||||
Pass "--all-services/-a" to restart all services.`,
|
||||
ShellComplete: autocomplete.AppNameComplete,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
app := internal.ValidateApp(cmd)
|
||||
Example: ` # restart a single app service
|
||||
abra app restart 1312.net app
|
||||
|
||||
# restart all app services
|
||||
abra app restart 1312.net -a`,
|
||||
Args: cobra.RangeArgs(1, 2),
|
||||
ValidArgsFunction: func(
|
||||
cmd *cobra.Command,
|
||||
args []string,
|
||||
toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
switch l := len(args); l {
|
||||
case 0:
|
||||
return autocomplete.AppNameComplete()
|
||||
case 1:
|
||||
if !allServices {
|
||||
return autocomplete.ServiceNameComplete(args[0])
|
||||
}
|
||||
return nil, cobra.ShellCompDirectiveDefault
|
||||
default:
|
||||
return nil, cobra.ShellCompDirectiveError
|
||||
}
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
app := internal.ValidateApp(args)
|
||||
|
||||
if err := app.Recipe.Ensure(false, false); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
serviceName := cmd.Args().Get(1)
|
||||
if serviceName == "" && !internal.AllServices {
|
||||
err := errors.New("missing <service>")
|
||||
internal.ShowSubcommandHelpAndError(cmd, err)
|
||||
var serviceName string
|
||||
if len(args) == 2 {
|
||||
serviceName = args[1]
|
||||
}
|
||||
|
||||
if serviceName != "" && internal.AllServices {
|
||||
log.Fatal("cannot use <service> and --all-services together")
|
||||
if serviceName == "" && !allServices {
|
||||
log.Fatal("missing [service]")
|
||||
}
|
||||
|
||||
if serviceName != "" && allServices {
|
||||
log.Fatal("cannot use [service] and --all-services/-a together")
|
||||
}
|
||||
|
||||
var serviceNames []string
|
||||
if internal.AllServices {
|
||||
if allServices {
|
||||
var err error
|
||||
serviceNames, err = appPkg.GetAppServiceNames(app.Name)
|
||||
if err != nil {
|
||||
@ -99,7 +117,17 @@ Pass "--all-services/-a" to restart all services.`,
|
||||
log.Debugf("%s has been scaled to 1", stackServiceName)
|
||||
log.Infof("%s service successfully restarted", serviceName)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var allServices bool
|
||||
|
||||
func init() {
|
||||
AppRestartCommand.Flags().BoolVarP(
|
||||
&allServices,
|
||||
"all-services",
|
||||
"a",
|
||||
false,
|
||||
"restart all services",
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user