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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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
import (

View File

@ -6,6 +6,8 @@ import (
"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 {
out, err := os.Create(target)
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"
)
// 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 {
reader, err := zip.OpenReader(archive)
if err != nil {