super-graph/serv/cmd.go

181 lines
4.1 KiB
Go
Raw Normal View History

2019-09-20 06:19:11 +02:00
package serv
import (
"fmt"
"runtime"
2019-09-20 06:19:11 +02:00
"strings"
"github.com/dosco/super-graph/allow"
2019-09-20 06:19:11 +02:00
"github.com/dosco/super-graph/psql"
"github.com/dosco/super-graph/qcode"
2019-09-26 06:35:31 +02:00
"github.com/jackc/pgx/v4/pgxpool"
2019-09-20 06:19:11 +02:00
"github.com/rs/zerolog"
2019-09-26 06:35:31 +02:00
"github.com/spf13/cobra"
2019-09-20 06:19:11 +02:00
"github.com/spf13/viper"
)
//go:generate rice embed-go
2019-09-20 06:19:11 +02:00
const (
serverName = "Super Graph"
)
var (
// These variables are set using -ldflags
version string
gitBranch string
lastCommitSHA string
lastCommitTime string
)
2019-09-20 06:19:11 +02:00
var (
logger zerolog.Logger // logger for everything but errors
errlog zerolog.Logger // logger for errors includes line numbers
conf *config // parsed config
confPath string // path to the config file
db *pgxpool.Pool // database connection pool
schema *psql.DBSchema // database tables, columns and relationships
allowList *allow.List // allow.list is contains queries allowed in production
qcompile *qcode.Compiler // qcode compiler
pcompile *psql.Compiler // postgres sql compiler
2019-09-20 06:19:11 +02:00
)
func Cmd() {
2019-11-25 08:22:33 +01:00
initLog()
2019-09-28 17:34:03 +02:00
rootCmd := &cobra.Command{
2019-09-26 06:35:31 +02:00
Use: "super-graph",
2019-11-28 21:27:20 +01:00
Short: BuildDetails(),
2019-09-26 06:35:31 +02:00
}
2019-09-28 17:34:03 +02:00
rootCmd.AddCommand(&cobra.Command{
2019-09-26 06:35:31 +02:00
Use: "serv",
Short: "Run the super-graph service",
Run: cmdServ,
2019-09-28 17:34:03 +02:00
})
rootCmd.AddCommand(&cobra.Command{
Use: "db:create",
Short: "Create database",
Run: cmdDBCreate,
})
rootCmd.AddCommand(&cobra.Command{
Use: "db:drop",
Short: "Drop database",
Run: cmdDBDrop,
})
rootCmd.AddCommand(&cobra.Command{
Use: "db:seed",
Short: "Run the seed script to seed the database",
Run: cmdDBSeed,
})
2019-09-26 06:35:31 +02:00
2019-09-28 17:34:03 +02:00
rootCmd.AddCommand(&cobra.Command{
Use: "db:migrate",
2019-09-26 06:35:31 +02:00
Short: "Migrate the database",
Long: `Migrate the database to destination migration version.
Destination migration version can be one of the following value types:
2019-09-28 17:34:03 +02:00
Migrate to the most recent migration.
2019-09-29 02:46:55 +02:00
e.g. db:migrate up
2019-09-28 17:34:03 +02:00
Rollback the most recent migration.
2019-09-29 02:46:55 +02:00
e.g. db:migrate down
2019-09-28 17:34:03 +02:00
2019-09-26 06:35:31 +02:00
Migrate to a specific migration.
2019-09-29 02:46:55 +02:00
e.g. db:migrate 42
2019-09-26 06:35:31 +02:00
Migrate forward N steps.
2019-09-29 02:46:55 +02:00
e.g. db:migrate +3
2019-09-26 06:35:31 +02:00
Migrate backward N steps.
2019-09-29 02:46:55 +02:00
e.g. db:migrate -2
2019-09-26 06:35:31 +02:00
Redo previous N steps (migrate backward N steps then forward N steps).
2019-09-29 02:46:55 +02:00
e.g. db:migrate -+1
2019-09-26 06:35:31 +02:00
`,
2019-09-28 17:34:03 +02:00
Run: cmdDBMigrate,
})
2019-09-26 06:35:31 +02:00
2019-09-28 17:34:03 +02:00
rootCmd.AddCommand(&cobra.Command{
Use: "db:status",
2019-09-26 06:35:31 +02:00
Short: "Print current migration status",
2019-09-28 17:34:03 +02:00
Run: cmdDBStatus,
})
2019-09-26 06:35:31 +02:00
2019-09-28 17:34:03 +02:00
rootCmd.AddCommand(&cobra.Command{
Use: "db:new NAME",
2019-09-26 06:35:31 +02:00
Short: "Generate a new migration",
Long: "Generate a new migration with the next sequence number and provided name",
2019-09-28 17:34:03 +02:00
Run: cmdDBNew,
})
rootCmd.AddCommand(&cobra.Command{
Use: "db:setup",
Short: "Setup database",
Long: "This command will create, migrate and seed the database",
Run: cmdDBSetup,
})
rootCmd.AddCommand(&cobra.Command{
Use: "db:reset",
Short: "Reset database",
Long: "This command will drop, create, migrate and seed the database (won't run in production)",
Run: cmdDBReset,
})
2019-09-28 17:34:03 +02:00
rootCmd.AddCommand(&cobra.Command{
Use: "new APP-NAME",
Short: "Create a new application",
Long: "Generate all the required files to start on a new Super Graph app",
2019-09-28 17:34:03 +02:00
Run: cmdNew,
})
2019-09-26 06:35:31 +02:00
rootCmd.AddCommand(&cobra.Command{
Use: fmt.Sprintf("conf:dump [%s]", strings.Join(viper.SupportedExts, "|")),
Short: "Dump config to file",
Long: "Dump current config to a file in the selected format",
Run: cmdConfDump,
})
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Super Graph binary version information",
Run: cmdVersion,
})
2019-09-26 06:35:31 +02:00
rootCmd.Flags().StringVar(&confPath,
"path", "./config", "path to config files")
if err := rootCmd.Execute(); err != nil {
2019-11-25 08:22:33 +01:00
errlog.Fatal().Err(err).Send()
2019-09-26 06:35:31 +02:00
}
}
func cmdVersion(cmd *cobra.Command, args []string) {
2019-11-28 21:27:20 +01:00
fmt.Printf("%s\n", BuildDetails())
}
func BuildDetails() string {
return fmt.Sprintf(`
2019-11-28 21:27:20 +01:00
Super Graph %v
For documentation, visit https://supergraph.dev
Commit SHA-1 : %v
Commit timestamp : %v
Branch : %v
Go version : %v
Licensed under the Apache Public License 2.0
Copyright 2020, Vikram Rangnekar.
`,
version,
lastCommitSHA,
lastCommitTime,
gitBranch,
runtime.Version())
}