-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
1,123 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package migrate | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/ory/x/cmdx" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/keto/internal/driver" | ||
) | ||
|
||
func newDownCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "down <steps>", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
steps, err := strconv.ParseInt(args[0], 0, 0) | ||
if err != nil { | ||
// return this error so it gets printed along the usage | ||
return fmt.Errorf("malformed argument %s for <steps>: %+v", args[0], err) | ||
} | ||
|
||
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
if err := reg.Migrator().MigrateDown(ctx, int(steps)); err != nil { | ||
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could apply down migrations: %+v\n", err) | ||
return cmdx.FailSilently(cmd) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,46 @@ | ||
package migrate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ory/x/cmdx" | ||
"github.com/ory/x/logrusx" | ||
"github.com/ory/x/flagx" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/keto/internal/driver" | ||
) | ||
|
||
const FlagYes = "yes" | ||
|
||
func newUpCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "up", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
ctx := cmd.Context() | ||
|
||
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
if err := reg.Migrator().MigrationStatus(ctx, cmd.OutOrStdout()); err != nil { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not get migration status: %+v\n", err) | ||
return cmdx.FailSilently(cmd) | ||
} | ||
|
||
if !flagx.MustGetBool(cmd, FlagYes) && !cmdx.AskForConfirmation("Do you want to apply above planned migrations?", cmd.InOrStdin(), cmd.OutOrStdout()) { | ||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Aborting") | ||
return nil | ||
} | ||
|
||
reg := driver.NewDefaultRegistry(ctx, logrusx.New("keto", "test"), cmd.Flags(), "test", "adf", "today") | ||
if err := reg.Migrator().MigrateUp(ctx); err != nil { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not apply migrations: %+v\n", err) | ||
return cmdx.FailSilently(cmd) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().BoolP(FlagYes, "y", false, "yes to all questions, no user input required") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package namespace | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ory/x/cmdx" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/keto/internal/driver" | ||
) | ||
|
||
func NewMigrateDownCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "down <namespace-name> <steps>", | ||
Short: "Migrate a namespace down.", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nm, err := reg.Config().NamespaceManager() | ||
if err != nil { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not initialize the namespace manager: %+v\n", err) | ||
return cmdx.FailSilently(cmd) | ||
} | ||
|
||
n, err := nm.GetNamespace(ctx, args[0]) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not find the namespace with name \"%s\": %+v\n", args[0], err) | ||
return cmdx.FailSilently(cmd) | ||
} | ||
|
||
if err := reg.NamespaceMigrator().MigrateNamespaceDown(ctx, n, 0); err != nil { | ||
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not apply namespace migration: %+v\n", err) | ||
return cmdx.FailSilently(cmd) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
registerYesFlag(cmd.Flags()) | ||
registerPackageFlags(cmd.Flags()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.