[cli] add comments (#4)

Especially for godoc
This commit is contained in:
Shahidh K Muhammed 2018-06-28 14:06:25 +05:30 committed by GitHub
parent 47c73f750f
commit 6d0d75fbe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 55 additions and 51 deletions

View File

@ -40,7 +40,7 @@ func NewConsoleCmd(ec *cli.ExecutionContext) *cobra.Command {
return ec.Validate() return ec.Validate()
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run() return opts.run()
}, },
} }
f := consoleCmd.Flags() f := consoleCmd.Flags()
@ -71,7 +71,7 @@ type consoleOptions struct {
WG *sync.WaitGroup WG *sync.WaitGroup
} }
func (o *consoleOptions) Run() error { func (o *consoleOptions) run() error {
log := o.EC.Logger log := o.EC.Logger
// Switch to "release" mode in production. // Switch to "release" mode in production.
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)

View File

@ -41,7 +41,7 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command {
return ec.Prepare() return ec.Prepare()
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run() return opts.run()
}, },
} }
@ -60,7 +60,7 @@ type initOptions struct {
InitDir string InitDir string
} }
func (o *initOptions) Run() error { func (o *initOptions) run() error {
if o.EC.ExecutionDirectory == "" { if o.EC.ExecutionDirectory == "" {
o.EC.ExecutionDirectory = o.InitDir o.EC.ExecutionDirectory = o.InitDir
} }

View File

@ -18,9 +18,9 @@ func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
}, },
} }
metadataCmd.AddCommand( metadataCmd.AddCommand(
NewMetadataExportCmd(ec), newMetadataExportCmd(ec),
NewMetadataResetCmd(ec), newMetadataResetCmd(ec),
NewMetadataApplyCmd(ec), newMetadataApplyCmd(ec),
) )
f := metadataCmd.PersistentFlags() f := metadataCmd.PersistentFlags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine") f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")

View File

@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func NewMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command { func newMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &metadataApplyOptions{ opts := &metadataApplyOptions{
EC: ec, EC: ec,
actionType: "apply", actionType: "apply",
@ -22,7 +22,7 @@ func NewMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
hasura metadata apply`, hasura metadata apply`,
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run() return opts.run()
}, },
} }
@ -35,7 +35,7 @@ type metadataApplyOptions struct {
actionType string actionType string
} }
func (o *metadataApplyOptions) Run() error { func (o *metadataApplyOptions) run() error {
dbURL, err := url.Parse(o.EC.Config.Endpoint) dbURL, err := url.Parse(o.EC.Config.Endpoint)
if err != nil { if err != nil {
return errors.Wrap(err, "error parsing Endpoint") return errors.Wrap(err, "error parsing Endpoint")

View File

@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func NewMetadataExportCmd(ec *cli.ExecutionContext) *cobra.Command { func newMetadataExportCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &metadataExportOptions{ opts := &metadataExportOptions{
EC: ec, EC: ec,
actionType: "export", actionType: "export",
@ -22,7 +22,7 @@ func NewMetadataExportCmd(ec *cli.ExecutionContext) *cobra.Command {
hasura metadata export`, hasura metadata export`,
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run() return opts.run()
}, },
} }
@ -35,7 +35,7 @@ type metadataExportOptions struct {
actionType string actionType string
} }
func (o *metadataExportOptions) Run() error { func (o *metadataExportOptions) run() error {
dbURL, err := url.Parse(o.EC.Config.Endpoint) dbURL, err := url.Parse(o.EC.Config.Endpoint)
if err != nil { if err != nil {
return errors.Wrap(err, "error parsing Endpoint") return errors.Wrap(err, "error parsing Endpoint")

View File

@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func NewMetadataResetCmd(ec *cli.ExecutionContext) *cobra.Command { func newMetadataResetCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &metadataResetOptions{ opts := &metadataResetOptions{
EC: ec, EC: ec,
actionType: "reset", actionType: "reset",
@ -22,7 +22,7 @@ func NewMetadataResetCmd(ec *cli.ExecutionContext) *cobra.Command {
hasura metadata reset`, hasura metadata reset`,
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run() return opts.run()
}, },
} }
@ -35,7 +35,7 @@ type metadataResetOptions struct {
actionType string actionType string
} }
func (o *metadataResetOptions) Run() error { func (o *metadataResetOptions) run() error {
dbURL, err := url.Parse(o.EC.Config.Endpoint) dbURL, err := url.Parse(o.EC.Config.Endpoint)
if err != nil { if err != nil {
return errors.Wrap(err, "error parsing Endpoint") return errors.Wrap(err, "error parsing Endpoint")

View File

@ -20,9 +20,9 @@ func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
}, },
} }
migrateCmd.AddCommand( migrateCmd.AddCommand(
NewMigrateApplyCmd(ec), newMigrateApplyCmd(ec),
NewMigrateStatusCmd(ec), newMigrateStatusCmd(ec),
NewMigrateCreateCmd(ec), newMigrateCreateCmd(ec),
) )
f := migrateCmd.PersistentFlags() f := migrateCmd.PersistentFlags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine") f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")

View File

@ -11,15 +11,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
UP_MIGRATION_HELP = "apply all or N up migration steps"
DOWN_MIGRATION_HELP = "apply all or N down migration steps"
VERSION_MIGRATION_HELP = "migrate the database to a specific version specified by the timestamp"
)
const FLAG_NOT_SET = ""
func NewMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &migrateApplyOptions{ opts := &migrateApplyOptions{
EC: ec, EC: ec,
} }
@ -28,15 +20,15 @@ func NewMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
Short: "Apply migrations on the database", Short: "Apply migrations on the database",
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run() return opts.run()
}, },
} }
f := migrateApplyCmd.Flags() f := migrateApplyCmd.Flags()
f.StringVar(&opts.upMigration, "up", FLAG_NOT_SET, UP_MIGRATION_HELP) f.StringVar(&opts.upMigration, "up", "", "apply all or N up migration steps")
f.StringVar(&opts.downMigration, "down", FLAG_NOT_SET, DOWN_MIGRATION_HELP) f.StringVar(&opts.downMigration, "down", "", "apply all or N down migration steps")
f.StringVar(&opts.versionMigration, "version", FLAG_NOT_SET, VERSION_MIGRATION_HELP) f.StringVar(&opts.versionMigration, "version", "", "migrate the database to a specific version")
f.StringVar(&opts.typeMigration, "type", FLAG_NOT_SET, VERSION_MIGRATION_HELP) f.StringVar(&opts.migrationType, "type", "up", "type of migration (up, down) to be used with version flag")
return migrateApplyCmd return migrateApplyCmd
} }
@ -46,11 +38,11 @@ type migrateApplyOptions struct {
upMigration string upMigration string
downMigration string downMigration string
versionMigration string versionMigration string
typeMigration string migrationType string
} }
func (o *migrateApplyOptions) Run() error { func (o *migrateApplyOptions) run() error {
migrationType, step, err := getMigrationTypeAndStep(o.upMigration, o.downMigration, o.versionMigration, o.typeMigration) migrationType, step, err := getMigrationTypeAndStep(o.upMigration, o.downMigration, o.versionMigration, o.migrationType)
if err != nil { if err != nil {
return errors.Wrap(err, "error validating flags") return errors.Wrap(err, "error validating flags")
} }
@ -75,24 +67,25 @@ func (o *migrateApplyOptions) Run() error {
return nil return nil
} }
//Only one flag out of up,down and goto can be set at a time. This function checks whether that is the case and returns an error is not // Only one flag out of up, down and version can be set at a time. This function
func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, typeMigration string) (string, int64, error) { // checks whether that is the case and returns an error is not
func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, migrationType string) (string, int64, error) {
var flagCount = 0 var flagCount = 0
var stepString = "all" var stepString = "all"
var migrationName = "up" var migrationName = "up"
if upMigration != FLAG_NOT_SET { if upMigration != "" {
stepString = upMigration stepString = upMigration
flagCount++ flagCount++
} }
if downMigration != FLAG_NOT_SET { if downMigration != "" {
migrationName = "down" migrationName = "down"
stepString = downMigration stepString = downMigration
flagCount++ flagCount++
} }
if versionMigration != FLAG_NOT_SET { if versionMigration != "" {
migrationName = "version" migrationName = "version"
stepString = versionMigration stepString = versionMigration
if typeMigration != FLAG_NOT_SET { if migrationType == "down" {
stepString = "-" + stepString stepString = "-" + stepString
} }
flagCount++ flagCount++

View File

@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func NewMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command { func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &migrateCreateOptions{ opts := &migrateCreateOptions{
EC: ec, EC: ec,
} }
@ -22,7 +22,7 @@ func NewMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.name = args[0] opts.name = args[0]
return opts.Run() return opts.run()
}, },
} }
@ -35,7 +35,7 @@ type migrateCreateOptions struct {
name string name string
} }
func (o *migrateCreateOptions) Run() error { func (o *migrateCreateOptions) run() error {
timestamp := getTime() timestamp := getTime()
err := mig.CreateCmd(o.EC.MigrationDir, timestamp, o.name) err := mig.CreateCmd(o.EC.MigrationDir, timestamp, o.name)
if err != nil { if err != nil {

View File

@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func NewMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command { func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &migrateStatusOptions{ opts := &migrateStatusOptions{
EC: ec, EC: ec,
} }
@ -18,7 +18,7 @@ func NewMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
Short: "Display current status of migrations on a database", Short: "Display current status of migrations on a database",
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run() return opts.run()
}, },
} }
@ -29,7 +29,7 @@ type migrateStatusOptions struct {
EC *cli.ExecutionContext EC *cli.ExecutionContext
} }
func (o *migrateStatusOptions) Run() error { func (o *migrateStatusOptions) run() error {
dbURL, err := url.Parse(o.EC.Config.Endpoint) dbURL, err := url.Parse(o.EC.Config.Endpoint)
if err != nil { if err != nil {
return errors.Wrap(err, "error parsing Endpoint") return errors.Wrap(err, "error parsing Endpoint")

View File

@ -15,9 +15,9 @@ hasura migrate apply [flags]
``` ```
--down string apply all or N down migration steps --down string apply all or N down migration steps
-h, --help help for apply -h, --help help for apply
--type string migrate the database to a specific version specified by the timestamp --type string type of migration (up, down) to be used with version flag (default "up")
--up string apply all or N up migration steps --up string apply all or N up migration steps
--version string migrate the database to a specific version specified by the timestamp --version string migrate the database to a specific version
``` ```
### Options inherited from parent commands ### Options inherited from parent commands

View File

@ -1,3 +1,8 @@
// Package migrate implements migrations on Hasura GraphQL Engine.
//
// This package is borrowed from https://github.com/golang-migrate/migrate with
// additions for Hasura specific yaml file support and a improved Rails-like
// migration pattern.
package migrate package migrate
import ( import (

View File

@ -6,6 +6,8 @@ import (
"os" "os"
) )
// Download downloads resource given by url and writes it to target.
// target should not exist already, as it is created by the function.
func Download(url, target string) error { func Download(url, target string) error {
out, err := os.Create(target) out, err := os.Create(target)
if err != nil { if err != nil {

2
cli/util/util.go Normal file
View File

@ -0,0 +1,2 @@
// Package util contains utility functions used by various commands.
package util

View File

@ -7,8 +7,10 @@ import (
"path/filepath" "path/filepath"
) )
// https://gist.github.com/svett/424e6784facc0ba907ae // from https://gist.github.com/svett/424e6784facc0ba907ae
// Unzip unzips the archive to target. Both archive and target should be paths
// in the filesystem. target is created if it doesn't exist already.
func Unzip(archive, target string) error { func Unzip(archive, target string) error {
reader, err := zip.OpenReader(archive) reader, err := zip.OpenReader(archive)
if err != nil { if err != nil {