2021-01-18 20:11:05 +03:00
package commands
import (
2021-05-17 18:19:15 +03:00
"fmt"
2021-06-09 10:12:56 +03:00
2022-11-08 15:21:59 +03:00
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2/internal/scripts"
"github.com/hasura/graphql-engine/cli/v2/util"
2021-01-18 20:11:05 +03:00
"github.com/spf13/afero"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
2021-01-18 20:11:05 +03:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newUpdateMultipleSources ( ec * cli . ExecutionContext ) * cobra . Command {
v := viper . New ( )
2021-05-17 18:19:15 +03:00
var opts scripts . UpdateProjectV3Opts
2021-01-18 20:11:05 +03:00
cmd := & cobra . Command {
2022-12-30 06:50:48 +03:00
Use : "update-project-v3" ,
2023-01-11 11:37:47 +03:00
Short : "Update the Hasura Project from config v2 to v3" ,
2023-01-11 16:32:41 +03:00
Long : "This helper script upgrades your CLI project to use config v3. This process is completely independent from your Hasura Graphql Engine server update process." ,
2021-01-18 20:11:05 +03:00
SilenceUsage : true ,
PreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-08 15:21:59 +03:00
op := genOpName ( cmd , "PreRunE" )
2021-01-18 20:11:05 +03:00
ec . Viper = v
err := ec . Prepare ( )
if err != nil {
2022-11-08 15:21:59 +03:00
return errors . E ( op , err )
2021-01-18 20:11:05 +03:00
}
2022-11-08 15:21:59 +03:00
if err := ec . Validate ( ) ; err != nil {
return errors . E ( op , err )
}
return nil
2021-01-18 20:11:05 +03:00
} ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-08 15:21:59 +03:00
op := genOpName ( cmd , "RunE" )
2021-05-17 18:19:15 +03:00
if opts . Force && len ( opts . TargetDatabase ) == 0 {
2022-11-08 15:21:59 +03:00
return errors . E ( op , fmt . Errorf ( "--database-name is required when --force is set" ) )
2021-01-18 20:11:05 +03:00
}
2021-05-17 18:19:15 +03:00
opts . Fs = afero . NewOsFs ( )
opts . ProjectDirectory = ec . ExecutionDirectory
opts . MigrationsAbsDirectoryPath = ec . MigrationDir
opts . SeedsAbsDirectoryPath = ec . SeedsDirectory
opts . Logger = ec . Logger
opts . EC = ec
2022-11-08 15:21:59 +03:00
if err := scripts . UpdateProjectV3 ( opts ) ; err != nil {
return errors . E ( op , err )
}
return nil
2021-01-18 20:11:05 +03:00
} ,
}
2021-02-22 15:42:17 +03:00
f := cmd . Flags ( )
2021-05-17 18:19:15 +03:00
f . StringVar ( & opts . TargetDatabase , "database-name" , "" , "database name for which the current migrations / seeds belong to" )
f . BoolVar ( & opts . Force , "force" , false , "do not ask for confirmation" )
f . BoolVar ( & opts . MoveStateOnly , "move-state-only" , false , "do only a state migration from old hdb_catalog.* table to catalog state and skip others" )
2021-02-22 15:42:17 +03:00
2023-01-11 11:37:47 +03:00
f . String ( "endpoint" , "" , "http(s) endpoint for Hasura GraphQL Engine" )
f . String ( "admin-secret" , "" , "admin secret for Hasura GraphQL Engine" )
f . String ( "access-key" , "" , "access key for Hasura GraphQL Engine" )
2021-10-13 17:38:07 +03:00
if err := f . MarkDeprecated ( "access-key" , "use --admin-secret instead" ) ; err != nil {
ec . Logger . WithError ( err ) . Errorf ( "error while using a dependency library" )
}
2021-02-22 15:42:17 +03:00
f . Bool ( "insecure-skip-tls-verify" , false , "skip TLS verification and disable cert checking (default: false)" )
f . String ( "certificate-authority" , "" , "path to a cert file for the certificate authority" )
// need to create a new viper because https://github.com/spf13/viper/issues/233
util . BindPFlag ( v , "endpoint" , f . Lookup ( "endpoint" ) )
util . BindPFlag ( v , "admin_secret" , f . Lookup ( "admin-secret" ) )
util . BindPFlag ( v , "access_key" , f . Lookup ( "access-key" ) )
util . BindPFlag ( v , "insecure_skip_tls_verify" , f . Lookup ( "insecure-skip-tls-verify" ) )
util . BindPFlag ( v , "certificate_authority" , f . Lookup ( "certificate-authority" ) )
2021-01-18 20:11:05 +03:00
return cmd
}