forked from toolshed/abra
refactor!: cobra migrate
This commit is contained in:
@ -21,46 +21,25 @@ import (
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
const SERVER = "localhost"
|
||||
|
||||
var majorUpdate bool
|
||||
var majorFlag = &cli.BoolFlag{
|
||||
Name: "major",
|
||||
Aliases: []string{"m"},
|
||||
Usage: "Also check for major updates",
|
||||
Destination: &majorUpdate,
|
||||
}
|
||||
|
||||
var updateAll bool
|
||||
var allFlag = &cli.BoolFlag{
|
||||
Name: "all",
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Update all deployed apps",
|
||||
Destination: &updateAll,
|
||||
}
|
||||
|
||||
// Notify checks for available upgrades
|
||||
var Notify = cli.Command{
|
||||
Name: "notify",
|
||||
Aliases: []string{"n"},
|
||||
Usage: "Check for available upgrades",
|
||||
UsageText: "kadabra notify [options]",
|
||||
Flags: []cli.Flag{
|
||||
majorFlag,
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
Description: `Notify on new versions for deployed apps.
|
||||
// NotifyCommand checks for available upgrades.
|
||||
var NotifyCommand = &cobra.Command{
|
||||
Use: "notify [flags]",
|
||||
Aliases: []string{"n"},
|
||||
Short: "Check for available upgrades",
|
||||
Long: `Notify on new versions for deployed apps.
|
||||
|
||||
If a new patch/minor version is available, a notification is printed.
|
||||
|
||||
Use "--major" to include new major versions.`,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
Use "--major/-m" to include new major versions.`,
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cl, err := client.New("default")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -85,24 +64,15 @@ Use "--major" to include new major versions.`,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// UpgradeApp upgrades apps.
|
||||
var UpgradeApp = cli.Command{
|
||||
Name: "upgrade",
|
||||
Aliases: []string{"u"},
|
||||
Usage: "Upgrade apps",
|
||||
UsageText: "kadabra notify <stack> <recipe> [options]",
|
||||
Flags: []cli.Flag{
|
||||
internal.ChaosFlag,
|
||||
majorFlag,
|
||||
allFlag,
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
Description: `Upgrade an app by specifying stack name and recipe.
|
||||
// UpgradeCommand upgrades apps.
|
||||
var UpgradeCommand = &cobra.Command{
|
||||
Use: "upgrade [[stack] [recipe] | --all] [flags]",
|
||||
Aliases: []string{"u"},
|
||||
Short: "Upgrade apps",
|
||||
Long: `Upgrade an app by specifying stack name and recipe.
|
||||
|
||||
Use "--all" to upgrade every deployed app.
|
||||
|
||||
@ -110,25 +80,37 @@ For each app with auto updates enabled, the deployed version is compared with
|
||||
the current recipe catalogue version. If a new patch/minor version is
|
||||
available, the app is upgraded.
|
||||
|
||||
To include major versions use the "--major" flag. You probably don't want that
|
||||
as it will break things. Only apps that are not deployed with "--chaos" are
|
||||
upgraded, to update chaos deployments use the "--chaos" flag. Use it with care.`,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
To include major versions use the "--major/-m" flag. You probably don't want
|
||||
that as it will break things. Only apps that are not deployed with "--chaos/-C"
|
||||
are upgraded, to update chaos deployments use the "--chaos/-C" flag. Use it
|
||||
with care.`,
|
||||
Args: cobra.RangeArgs(0, 2),
|
||||
// TODO(d1): complete stack/recipe
|
||||
// ValidArgsFunction: func(
|
||||
// cmd *cobra.Command,
|
||||
// args []string,
|
||||
// toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
// },
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cl, err := client.New("default")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if !updateAll && len(args) != 2 {
|
||||
log.Fatal("missing arguments or --all/-a flag")
|
||||
}
|
||||
|
||||
if !updateAll {
|
||||
stackName := cmd.Args().Get(0)
|
||||
recipeName := cmd.Args().Get(1)
|
||||
stackName := args[0]
|
||||
recipeName := args[1]
|
||||
|
||||
err = tryUpgrade(cl, stackName, recipeName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
stacks, err := stack.GetStacks(cl)
|
||||
@ -148,8 +130,6 @@ upgraded, to update chaos deployments use the "--chaos" flag. Use it with care.`
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@ -309,7 +289,7 @@ func getAvailableUpgrades(cl *dockerclient.Client, stackName string, recipeName
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if 0 < versionDelta.UpgradeType() && (versionDelta.UpgradeType() < 4 || majorUpdate) {
|
||||
if 0 < versionDelta.UpgradeType() && (versionDelta.UpgradeType() < 4 || includeMajorUpdates) {
|
||||
availableUpgrades = append(availableUpgrades, version)
|
||||
}
|
||||
}
|
||||
@ -466,48 +446,87 @@ func upgrade(cl *dockerclient.Client, stackName, recipeName, upgradeVersion stri
|
||||
return err
|
||||
}
|
||||
|
||||
func newKadabraApp(version, commit string) *cli.Command {
|
||||
app := &cli.Command{
|
||||
Name: "kadabra",
|
||||
Version: fmt.Sprintf("%s-%s", version, commit[:7]),
|
||||
Usage: "The Co-op Cloud auto-updater 🤖 🚀",
|
||||
UsageText: "kadabra [command] [options]",
|
||||
UseShortOptionHandling: true,
|
||||
HideHelpCommand: true,
|
||||
Flags: []cli.Flag{
|
||||
// NOTE(d1): "GLOBAL OPTIONS" flags
|
||||
internal.DebugFlag,
|
||||
internal.NoInputFlag,
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
&Notify,
|
||||
&UpgradeApp,
|
||||
func newKadabraApp(version, commit string) *cobra.Command {
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "kadabra [cmd] [flags]",
|
||||
Version: fmt.Sprintf("%s-%s", version, commit[:7]),
|
||||
Short: "The Co-op Cloud auto-updater 🤖 🚀",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
log.Logger.SetStyles(log.Styles())
|
||||
charmLog.SetDefault(log.Logger)
|
||||
|
||||
if internal.Debug {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.SetOutput(os.Stderr)
|
||||
log.SetReportCaller(true)
|
||||
}
|
||||
|
||||
log.Debugf("kadabra version %s, commit %s", version, commit)
|
||||
},
|
||||
}
|
||||
|
||||
app.Before = func(ctx context.Context, cmd *cli.Command) error {
|
||||
log.Logger.SetStyles(log.Styles())
|
||||
charmLog.SetDefault(log.Logger)
|
||||
rootCmd.PersistentFlags().BoolVarP(
|
||||
&internal.Debug, "debug", "d", false,
|
||||
"show debug messages",
|
||||
)
|
||||
|
||||
log.Debugf("kadabra version %s, commit %s", version, commit)
|
||||
rootCmd.PersistentFlags().BoolVarP(
|
||||
&internal.NoInput, "no-input", "n", false,
|
||||
"toggle non-interactive mode",
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
rootCmd.AddCommand(
|
||||
NotifyCommand,
|
||||
UpgradeCommand,
|
||||
)
|
||||
|
||||
cli.HelpFlag = &cli.BoolFlag{
|
||||
Name: "help",
|
||||
Aliases: []string{"h, H"},
|
||||
Usage: "Show help",
|
||||
}
|
||||
|
||||
return app
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
// RunApp runs CLI abra app.
|
||||
func RunApp(version, commit string) {
|
||||
app := newKadabraApp(version, commit)
|
||||
|
||||
if err := app.Run(context.Background(), os.Args); err != nil {
|
||||
if err := app.Execute(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
includeMajorUpdates bool
|
||||
updateAll bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
NotifyCommand.Flags().BoolVarP(
|
||||
&includeMajorUpdates,
|
||||
"major",
|
||||
"m",
|
||||
false,
|
||||
"check for major updates",
|
||||
)
|
||||
|
||||
UpgradeCommand.Flags().BoolVarP(
|
||||
&internal.Chaos,
|
||||
"chaos",
|
||||
"C",
|
||||
false,
|
||||
"ignore uncommitted recipes changes",
|
||||
)
|
||||
|
||||
UpgradeCommand.Flags().BoolVarP(
|
||||
&includeMajorUpdates,
|
||||
"major",
|
||||
"m",
|
||||
false,
|
||||
"check for major updates",
|
||||
)
|
||||
|
||||
UpgradeCommand.Flags().BoolVarP(
|
||||
&updateAll,
|
||||
"all",
|
||||
"a",
|
||||
false,
|
||||
"update all deployed apps",
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user