super-graph/internal/serv/cmd.go

190 lines
4.1 KiB
Go
Raw Normal View History

2019-09-20 06:19:11 +02:00
package serv
import (
"database/sql"
2019-09-20 06:19:11 +02:00
"fmt"
_log "log"
"os"
"runtime"
2019-09-20 06:19:11 +02:00
2019-09-26 06:35:31 +02:00
"github.com/spf13/cobra"
"go.uber.org/zap"
2019-09-20 06:19:11 +02:00
)
//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 = "unknown"
gitBranch string = "unknown"
lastCommitSHA string
lastCommitTime string
)
2019-09-20 06:19:11 +02:00
var (
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
)
func Cmd() {
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,
})
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
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,
// })
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Super Graph binary version information",
Run: cmdVersion,
})
rootCmd.PersistentFlags().StringVar(&confPath,
2019-09-26 06:35:31 +02:00
"path", "./config", "path to config files")
if err := rootCmd.Execute(); err != nil {
log.Fatalf("ERR %s", err)
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 {
if version == "" {
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-04-16 06:26:32 +02:00
}
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
2020-04-16 06:26:32 +02:00
Copyright 2020, Vikram Rangnekar
`,
version,
lastCommitSHA,
lastCommitTime,
gitBranch,
runtime.Version())
}