Skip to content

Commit

Permalink
chore: refactor cmd to make commands importable
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored and aeneasr committed Aug 26, 2020
1 parent ee8ecb9 commit f22c58a
Show file tree
Hide file tree
Showing 21 changed files with 372 additions and 230 deletions.
10 changes: 5 additions & 5 deletions .goreleaser.yml
Expand Up @@ -19,7 +19,7 @@ builds:
- -tags
- sqlite
ldflags:
- -s -w -X github.com/ory/kratos/cmd.BuildVersion={{.Tag}} -X github.com/ory/kratos/cmd.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/cmd.BuildTime={{.Date}}
- -s -w -X github.com/ory/kratos/internal/clihelpers.BuildVersion={{.Tag}} -X github.com/ory/kratos/internal/clihelpers.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/internal/clihelpers.BuildTime={{.Date}}
# - "-extldflags '-static'"
binary: kratos
env:
Expand All @@ -37,7 +37,7 @@ builds:
- -tags
- sqlite
ldflags:
- -s -w -X github.com/ory/kratos/cmd.BuildVersion={{.Tag}} -X github.com/ory/kratos/cmd.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/cmd.BuildTime={{.Date}}
- -s -w -X github.com/ory/kratos/internal/clihelpers.BuildVersion={{.Tag}} -X github.com/ory/kratos/internal/clihelpers.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/internal/clihelpers.BuildTime={{.Date}}
binary: kratos
env:
- CGO_ENABLED=1
Expand All @@ -51,7 +51,7 @@ builds:
- -tags
- sqlite
ldflags:
- -s -w -X github.com/ory/kratos/cmd.BuildVersion={{.Tag}} -X github.com/ory/kratos/cmd.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/cmd.BuildTime={{.Date}}
- -s -w -X github.com/ory/kratos/internal/clihelpers.BuildVersion={{.Tag}} -X github.com/ory/kratos/internal/clihelpers.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/internal/clihelpers.BuildTime={{.Date}}
binary: kratos
env:
- CGO_ENABLED=1
Expand All @@ -66,7 +66,7 @@ builds:
- -tags
- sqlite
ldflags:
- -s -w -X github.com/ory/kratos/cmd.BuildVersion={{.Tag}} -X github.com/ory/kratos/cmd.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/cmd.BuildTime={{.Date}}
- -s -w -X github.com/ory/kratos/internal/clihelpers.BuildVersion={{.Tag}} -X github.com/ory/kratos/internal/clihelpers.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/internal/clihelpers.BuildTime={{.Date}}
- "-extldflags '-static'"
binary: kratos
env:
Expand All @@ -81,7 +81,7 @@ builds:
-
id: kratos
ldflags:
- -s -w -X github.com/ory/kratos/cmd.BuildVersion={{.Tag}} -X github.com/ory/kratos/cmd.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/cmd.BuildTime={{.Date}}
- -s -w -X github.com/ory/kratos/internal/clihelpers.BuildVersion={{.Tag}} -X github.com/ory/kratos/internal/clihelpers.BuildGitHash={{.FullCommit}} -X github.com/ory/kratos/internal/clihelpers.BuildTime={{.Date}}
binary: kratos
env:
- CGO_ENABLED=0
Expand Down
26 changes: 0 additions & 26 deletions cmd/identities.go

This file was deleted.

22 changes: 22 additions & 0 deletions cmd/identities/identities.go
@@ -0,0 +1,22 @@
package identities

import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/identities/port"
)

// identitiesCmd represents the identity command
var identitiesCmd = &cobra.Command{
Use: "identities",
}

func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(identitiesCmd)

port.RegisterCommandRecursive(identitiesCmd)
}

func init() {
identitiesCmd.PersistentFlags().String("endpoint", "", "Specifies the Ory Kratos Admin URL. Defaults to KRATOS_URLS_ADMIN")
}
17 changes: 17 additions & 0 deletions cmd/identities/port/import.go
@@ -0,0 +1,17 @@
package port

import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/client"
)

// importCmd represents the import command
var importCmd = &cobra.Command{
Use: "import <file.json [file-2.json [file-3.json] ...]>",
Run: client.NewIdentityClient().Import,
}

func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(importCmd)
}
27 changes: 0 additions & 27 deletions cmd/identities_import.go

This file was deleted.

11 changes: 8 additions & 3 deletions cmd/jsonnet_format.go → cmd/jsonnet/format/format.go
Expand Up @@ -13,13 +13,15 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package format

import (
"fmt"
"io/ioutil"
"path/filepath"

"github.com/ory/kratos/internal/clihelpers"

"github.com/google/go-jsonnet/formatter"
"github.com/spf13/cobra"

Expand All @@ -34,7 +36,7 @@ var jsonnetFormatCmd = &cobra.Command{
Use -w or --write to write output back to files instead of stdout.
` + globHelp,
` + clihelpers.JsonnetGlobHelp,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, pattern := range args {
Expand All @@ -60,7 +62,10 @@ Use -w or --write to write output back to files instead of stdout.
},
}

func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(jsonnetFormatCmd)
}

func init() {
jsonnetCmd.AddCommand(jsonnetFormatCmd)
jsonnetFormatCmd.Flags().BoolP("write", "w", false, "Write formatted output back to file.")
}
21 changes: 21 additions & 0 deletions cmd/jsonnet/jsonnet.go
@@ -0,0 +1,21 @@
package jsonnet

import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/jsonnet/format"
"github.com/ory/kratos/cmd/jsonnet/lint"
)

// jsonnetCmd represents the jsonnet command
var jsonnetCmd = &cobra.Command{
Use: "jsonnet",
Short: "Helpers for linting and formatting JSONNet code",
}

func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(jsonnetCmd)

format.RegisterCommandRecursive(jsonnetCmd)
lint.RegisterCommandRecursive(jsonnetCmd)
}
10 changes: 6 additions & 4 deletions cmd/jsonnet_lint.go → cmd/jsonnet/lint/lint.go
@@ -1,11 +1,13 @@
package cmd
package lint

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/ory/kratos/internal/clihelpers"

"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/linter"
"github.com/spf13/cobra"
Expand All @@ -18,7 +20,7 @@ var jsonnetLintCmd = &cobra.Command{
Use: "lint path/to/files/*.jsonnet [more/files.jsonnet, [supports/**/{foo,bar}.jsonnet]]",
Long: `Lints JSONNet files using the official JSONNet linter and exits with a status code of 1 when issues are detected.
` + globHelp,
` + clihelpers.JsonnetGlobHelp,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, pattern := range args {
Expand All @@ -43,6 +45,6 @@ var jsonnetLintCmd = &cobra.Command{
},
}

func init() {
jsonnetCmd.AddCommand(jsonnetLintCmd)
func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(jsonnetLintCmd)
}
15 changes: 0 additions & 15 deletions cmd/migrate.go

This file was deleted.

19 changes: 19 additions & 0 deletions cmd/migrate/migrate.go
@@ -0,0 +1,19 @@
package migrate

import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/migrate/sql"
)

// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Various migration helpers",
}

func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(migrateCmd)

sql.RegisterCommandRecursive(migrateCmd)
}
12 changes: 9 additions & 3 deletions cmd/migrate_sql.go → cmd/migrate/sql/migrate_sql.go
Expand Up @@ -13,16 +13,20 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
package sql

import (
"github.com/spf13/cobra"

"github.com/ory/x/logrusx"

"github.com/ory/x/viperx"

"github.com/ory/kratos/cmd/client"
)

var logger *logrusx.Logger

// migrateSqlCmd represents the sql command
var migrateSqlCmd = &cobra.Command{
Use: "sql <database-url>",
Expand All @@ -48,9 +52,11 @@ Before running this command on an existing database, create a back up!
},
}

func init() {
migrateCmd.AddCommand(migrateSqlCmd)
func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(migrateSqlCmd)
}

func init() {
migrateSqlCmd.Flags().BoolP("read-from-env", "e", false, "If set, reads the database connection string from the environment variable DSN or config file key dsn.")
migrateSqlCmd.Flags().BoolP("yes", "y", false, "If set all confirmation requests are accepted without user interaction.")
}
17 changes: 14 additions & 3 deletions cmd/root.go
Expand Up @@ -4,14 +4,18 @@ import (
"fmt"
"os"

"github.com/ory/x/logrusx"
"github.com/ory/kratos/cmd/identities"
"github.com/ory/kratos/cmd/jsonnet"
"github.com/ory/kratos/cmd/migrate"
"github.com/ory/kratos/cmd/serve"
"github.com/ory/kratos/internal/clihelpers"
"github.com/ory/x/cmdx"

"github.com/ory/x/viperx"

"github.com/spf13/cobra"
)

var logger *logrusx.Logger

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "kratos",
Expand All @@ -28,4 +32,11 @@ func Execute() {

func init() {
viperx.RegisterConfigFlag(rootCmd, "kratos")

identities.RegisterCommandRecursive(rootCmd)
jsonnet.RegisterCommandRecursive(rootCmd)
serve.RegisterCommandRecursive(rootCmd)
migrate.RegisterCommandRecursive(rootCmd)

rootCmd.AddCommand(cmdx.Version(&clihelpers.BuildVersion, &clihelpers.BuildGitHash, &clihelpers.BuildTime))
}
21 changes: 14 additions & 7 deletions cmd/serve.go → cmd/serve/serve.go
Expand Up @@ -12,12 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package serve

import (
"os"
"strconv"

"github.com/ory/kratos/internal/clihelpers"
"github.com/ory/x/logrusx"

"github.com/ory/kratos/driver/configuration"

"github.com/ory/x/viperx"
Expand All @@ -31,6 +34,8 @@ import (
"github.com/ory/kratos/x"
)

var logger *logrusx.Logger

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Expand All @@ -50,23 +55,25 @@ DON'T DO THIS IN PRODUCTION!
}

x.WatchAndValidateViper(logger)
d := driver.MustNewDefaultDriver(logger, BuildVersion, BuildTime, BuildGitHash, dev)
d := driver.MustNewDefaultDriver(logger, clihelpers.BuildVersion, clihelpers.BuildTime, clihelpers.BuildGitHash, dev)

configVersion := d.Configuration().ConfigVersion()
if configVersion == configuration.UnknownVersion {
d.Logger().Warn("The config has no version specified. Add the version to improve your development experience.")
} else if BuildVersion != "" &&
configVersion != BuildVersion {
d.Logger().Warnf("Config version is '%s' but kratos runs on version '%s'", configVersion, BuildVersion)
} else if clihelpers.BuildVersion != "" &&
configVersion != clihelpers.BuildVersion {
d.Logger().Warnf("Config version is '%s' but kratos runs on version '%s'", configVersion, clihelpers.BuildVersion)
}

daemon.ServeAll(d)(cmd, args)
},
}

func init() {
rootCmd.AddCommand(serveCmd)
func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(serveCmd)
}

func init() {
disableTelemetryEnv, _ := strconv.ParseBool(os.Getenv("DISABLE_TELEMETRY"))
serveCmd.PersistentFlags().Bool("disable-telemetry", disableTelemetryEnv, "Disable anonymized telemetry reports - for more information please visit https://www.ory.sh/docs/ecosystem/sqa")
serveCmd.PersistentFlags().Bool("dev", false, "Disables critical security features to make development easier")
Expand Down

0 comments on commit f22c58a

Please sign in to comment.