2018-06-24 16:40:48 +03:00
package commands
import (
2021-06-16 14:44:15 +03:00
"fmt"
2019-10-30 16:54:22 +03:00
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
"github.com/spf13/cobra"
2018-06-24 16:40:48 +03:00
)
2018-06-28 11:36:25 +03:00
func newMetadataApplyCmd ( ec * cli . ExecutionContext ) * cobra . Command {
2020-02-24 19:14:46 +03:00
opts := & MetadataApplyOptions {
2021-05-14 22:09:01 +03:00
EC : ec ,
2018-06-24 16:40:48 +03:00
}
metadataApplyCmd := & cobra . Command {
Use : "apply" ,
Short : "Apply Hasura metadata on a database" ,
2021-03-15 18:40:52 +03:00
Example : ` # Apply Hasura GraphQL engine metadata present in metadata . [ yaml | json ] file :
2019-12-12 08:16:36 +03:00
hasura metadata apply
# Use with admin secret :
hasura metadata apply -- admin - secret "<admin-secret>"
# Apply metadata to an instance specified by the flag :
2022-08-16 12:57:53 +03:00
hasura metadata apply -- endpoint "<endpoint>"
# Prevent inconsistent metadata from getting applied :
hasura metadata apply -- disallow - inconsistent - metadata ` ,
2018-06-24 16:40:48 +03:00
SilenceUsage : true ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2021-05-14 22:09:01 +03:00
if opts . FromFile {
return fmt . Errorf ( "use of deprecated flag" )
2019-10-30 16:54:22 +03:00
}
2021-09-29 14:11:24 +03:00
if ! opts . DryRun && len ( opts . rawOutput ) == 0 {
ec . Spin ( "Applying metadata..." )
}
2020-02-24 19:14:46 +03:00
err := opts . Run ( )
2021-09-29 14:11:24 +03:00
ec . Spinner . Stop ( )
2019-04-03 08:52:03 +03:00
if err != nil {
2021-05-14 22:09:01 +03:00
return err
2019-04-03 08:52:03 +03:00
}
2021-09-29 14:11:24 +03:00
if len ( opts . rawOutput ) <= 0 && ! opts . DryRun {
opts . EC . Logger . Info ( "Metadata applied" )
}
2019-04-03 08:52:03 +03:00
return nil
2018-06-24 16:40:48 +03:00
} ,
}
2018-09-27 16:57:17 +03:00
f := metadataApplyCmd . Flags ( )
2021-05-14 22:09:01 +03:00
// deprecated flag
2020-02-24 19:14:46 +03:00
f . BoolVar ( & opts . FromFile , "from-file" , false , "apply metadata from migrations/metadata.[yaml|json]" )
2021-10-13 17:38:07 +03:00
if err := f . MarkDeprecated ( "from-file" , "deprecation is a side effect of config v1 deprecation from v2.0.0" ) ; err != nil {
ec . Logger . WithError ( err ) . Errorf ( "error while using a dependency library" )
}
2021-05-14 22:09:01 +03:00
f . BoolVar ( & opts . DryRun , "dry-run" , false , "show metadata generated from project directory without applying to server. generated metadata will be printed as JSON by default, use -o flag for other display formats" )
f . StringVarP ( & opts . rawOutput , "output" , "o" , "" , ` specify an output format to show applied metadata. Allowed values: json, yaml (default "json") ` )
2022-08-16 12:57:53 +03:00
f . BoolVar ( & opts . DisallowInconsistencies , "disallow-inconsistent-metadata" , false , "disallow inconsistent metadata to be applied. Defaults to false" )
2018-06-24 16:40:48 +03:00
return metadataApplyCmd
}
2020-02-24 19:14:46 +03:00
type MetadataApplyOptions struct {
2018-06-24 16:40:48 +03:00
EC * cli . ExecutionContext
2022-08-16 12:57:53 +03:00
FromFile bool
DryRun bool
rawOutput string
DisallowInconsistencies bool
2018-06-24 16:40:48 +03:00
}
2020-02-24 19:14:46 +03:00
func ( o * MetadataApplyOptions ) Run ( ) error {
2021-09-29 14:11:24 +03:00
return getMetadataModeHandler ( o . EC . MetadataMode ) . Apply ( o )
2021-05-14 22:09:01 +03:00
}
func errorApplyingMetadata ( err error ) error {
// a helper function to have consistent error messages for errors
// when applying metadata
return fmt . Errorf ( "error applying metadata \n%w" , err )
2021-06-16 14:44:15 +03:00
}