2019-09-20 06:19:11 +02:00
|
|
|
package serv
|
|
|
|
|
|
|
|
import (
|
2020-04-10 08:27:43 +02:00
|
|
|
"database/sql"
|
2019-09-20 06:19:11 +02:00
|
|
|
"fmt"
|
2020-04-10 08:27:43 +02:00
|
|
|
_log "log"
|
|
|
|
"os"
|
2019-11-28 07:25:46 +01:00
|
|
|
"runtime"
|
2019-09-20 06:19:11 +02:00
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
"github.com/spf13/cobra"
|
2020-04-10 08:27:43 +02:00
|
|
|
"go.uber.org/zap"
|
2019-09-20 06:19:11 +02:00
|
|
|
)
|
|
|
|
|
2019-09-27 08:30:07 +02:00
|
|
|
//go:generate rice embed-go
|
|
|
|
|
2019-09-20 06:19:11 +02:00
|
|
|
const (
|
|
|
|
serverName = "Super Graph"
|
|
|
|
)
|
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
var (
|
|
|
|
// These variables are set using -ldflags
|
2020-05-23 17:43:57 +02:00
|
|
|
version string = "unknown"
|
|
|
|
gitBranch string = "unknown"
|
2019-11-28 07:25:46 +01:00
|
|
|
lastCommitSHA string
|
|
|
|
lastCommitTime string
|
|
|
|
)
|
|
|
|
|
2019-09-20 06:19:11 +02:00
|
|
|
var (
|
2020-05-01 08:20:13 +02:00
|
|
|
log *_log.Logger // logger
|
|
|
|
zlog *zap.Logger // fast logger
|
|
|
|
logLevel int // log level
|
|
|
|
conf *Config // parsed config
|
|
|
|
confPath string // path to the config file
|
|
|
|
db *sql.DB // database connection pool
|
2019-09-20 06:19:11 +02:00
|
|
|
)
|
|
|
|
|
2020-02-01 16:54:19 +01:00
|
|
|
func Cmd() {
|
2020-04-10 08:27:43 +02:00
|
|
|
log = _log.New(os.Stdout, "", 0)
|
|
|
|
zlog = zap.NewExample()
|
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,
|
|
|
|
})
|
|
|
|
|
2019-11-22 06:07:06 +01:00
|
|
|
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",
|
2019-09-27 08:19:24 +02:00
|
|
|
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
|
|
|
|
2020-04-11 08:45:06 +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,
|
|
|
|
// })
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
rootCmd.AddCommand(&cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "Super Graph binary version information",
|
|
|
|
Run: cmdVersion,
|
|
|
|
})
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
rootCmd.PersistentFlags().StringVar(&confPath,
|
2019-09-26 06:35:31 +02:00
|
|
|
"path", "./config", "path to config files")
|
|
|
|
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2020-04-10 08:27:43 +02:00
|
|
|
log.Fatalf("ERR %s", err)
|
2019-09-26 06:35:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
func cmdVersion(cmd *cobra.Command, args []string) {
|
2019-11-28 21:27:20 +01:00
|
|
|
fmt.Printf("%s\n", BuildDetails())
|
2019-11-28 07:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func BuildDetails() string {
|
2020-06-15 16:16:47 +02:00
|
|
|
if version == "" {
|
2020-05-31 08:34:28 +02:00
|
|
|
return `
|
2020-04-16 06:26:32 +02:00
|
|
|
Super Graph (unknown version)
|
|
|
|
For documentation, visit https://supergraph.dev
|
|
|
|
|
|
|
|
To build with version information please use the Makefile
|
|
|
|
> git clone https://github.com/dosco/super-graph
|
|
|
|
> cd super-graph && make install
|
|
|
|
|
|
|
|
Licensed under the Apache Public License 2.0
|
|
|
|
Copyright 2020, Vikram Rangnekar
|
2020-05-31 08:34:28 +02:00
|
|
|
`
|
2020-04-16 06:26:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
return fmt.Sprintf(`
|
2019-11-28 21:27:20 +01:00
|
|
|
Super Graph %v
|
|
|
|
For documentation, visit https://supergraph.dev
|
2019-11-28 07:25:46 +01:00
|
|
|
|
|
|
|
Commit SHA-1 : %v
|
|
|
|
Commit timestamp : %v
|
|
|
|
Branch : %v
|
|
|
|
Go version : %v
|
|
|
|
|
|
|
|
Licensed under the Apache Public License 2.0
|
2020-04-16 06:26:32 +02:00
|
|
|
Copyright 2020, Vikram Rangnekar
|
2019-11-28 07:25:46 +01:00
|
|
|
`,
|
|
|
|
version,
|
|
|
|
lastCommitSHA,
|
|
|
|
lastCommitTime,
|
|
|
|
gitBranch,
|
|
|
|
runtime.Version())
|
|
|
|
}
|