From 6d0d75fbe3ca5c4da9619aa03e6462356e375cf6 Mon Sep 17 00:00:00 2001 From: Shahidh K Muhammed Date: Thu, 28 Jun 2018 14:06:25 +0530 Subject: [PATCH] [cli] add comments (#4) Especially for godoc --- cli/commands/console.go | 4 ++-- cli/commands/init.go | 4 ++-- cli/commands/metadata.go | 6 ++--- cli/commands/metadata_apply.go | 6 ++--- cli/commands/metadata_export.go | 6 ++--- cli/commands/metadata_reset.go | 6 ++--- cli/commands/migrate.go | 6 ++--- cli/commands/migrate_apply.go | 39 +++++++++++++------------------- cli/commands/migrate_create.go | 6 ++--- cli/commands/migrate_status.go | 6 ++--- cli/docs/hasura_migrate_apply.md | 4 ++-- cli/migrate/migrate.go | 5 ++++ cli/util/download.go | 2 ++ cli/util/util.go | 2 ++ cli/util/zip.go | 4 +++- 15 files changed, 55 insertions(+), 51 deletions(-) create mode 100644 cli/util/util.go diff --git a/cli/commands/console.go b/cli/commands/console.go index 17e1bf7e5a8..cd2f682a7e7 100644 --- a/cli/commands/console.go +++ b/cli/commands/console.go @@ -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) diff --git a/cli/commands/init.go b/cli/commands/init.go index 129408b986e..88ac4ff0be1 100644 --- a/cli/commands/init.go +++ b/cli/commands/init.go @@ -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 } diff --git a/cli/commands/metadata.go b/cli/commands/metadata.go index 0301c43489d..4513c00013c 100644 --- a/cli/commands/metadata.go +++ b/cli/commands/metadata.go @@ -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") diff --git a/cli/commands/metadata_apply.go b/cli/commands/metadata_apply.go index c2f34fa4841..3e3aa50524a 100644 --- a/cli/commands/metadata_apply.go +++ b/cli/commands/metadata_apply.go @@ -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") diff --git a/cli/commands/metadata_export.go b/cli/commands/metadata_export.go index 706b924a871..73a3f322498 100644 --- a/cli/commands/metadata_export.go +++ b/cli/commands/metadata_export.go @@ -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") diff --git a/cli/commands/metadata_reset.go b/cli/commands/metadata_reset.go index a696a503a61..a8442c809ae 100644 --- a/cli/commands/metadata_reset.go +++ b/cli/commands/metadata_reset.go @@ -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") diff --git a/cli/commands/migrate.go b/cli/commands/migrate.go index 1cd6249ad12..5369debe51f 100644 --- a/cli/commands/migrate.go +++ b/cli/commands/migrate.go @@ -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") diff --git a/cli/commands/migrate_apply.go b/cli/commands/migrate_apply.go index cf3cf60d565..ddd733289ca 100644 --- a/cli/commands/migrate_apply.go +++ b/cli/commands/migrate_apply.go @@ -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++ diff --git a/cli/commands/migrate_create.go b/cli/commands/migrate_create.go index 01717b41793..1ffa533a0de 100644 --- a/cli/commands/migrate_create.go +++ b/cli/commands/migrate_create.go @@ -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 { diff --git a/cli/commands/migrate_status.go b/cli/commands/migrate_status.go index b48a5cd1105..66b050837f7 100644 --- a/cli/commands/migrate_status.go +++ b/cli/commands/migrate_status.go @@ -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") diff --git a/cli/docs/hasura_migrate_apply.md b/cli/docs/hasura_migrate_apply.md index 1ed6fda9126..7d71f87c878 100644 --- a/cli/docs/hasura_migrate_apply.md +++ b/cli/docs/hasura_migrate_apply.md @@ -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 diff --git a/cli/migrate/migrate.go b/cli/migrate/migrate.go index 04ab4bed09c..db55084d1de 100644 --- a/cli/migrate/migrate.go +++ b/cli/migrate/migrate.go @@ -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 ( diff --git a/cli/util/download.go b/cli/util/download.go index ad5bca58f9d..90b36280c87 100644 --- a/cli/util/download.go +++ b/cli/util/download.go @@ -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 { diff --git a/cli/util/util.go b/cli/util/util.go new file mode 100644 index 00000000000..6d2bc11410d --- /dev/null +++ b/cli/util/util.go @@ -0,0 +1,2 @@ +// Package util contains utility functions used by various commands. +package util diff --git a/cli/util/zip.go b/cli/util/zip.go index 1aaae7206a3..3e3e0edb62e 100644 --- a/cli/util/zip.go +++ b/cli/util/zip.go @@ -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 {