2019-12-25 11:33:06 +03:00
package commands
import (
2021-07-16 08:26:00 +03:00
"encoding/json"
2019-12-25 11:33:06 +03:00
"fmt"
2022-11-08 16:04:14 +03:00
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
2021-07-23 12:49:44 +03:00
"github.com/hasura/graphql-engine/cli/v2/internal/projectmetadata"
2020-04-07 12:23:20 +03:00
2019-12-25 11:33:06 +03:00
"github.com/spf13/cobra"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/util"
2019-12-25 11:33:06 +03:00
)
func newMetadataInconsistencyListCmd ( ec * cli . ExecutionContext ) * cobra . Command {
opts := & metadataInconsistencyListOptions {
EC : ec ,
}
metadataInconsistencyListCmd := & cobra . Command {
Use : "list" ,
Aliases : [ ] string { "ls" } ,
2022-12-30 06:50:48 +03:00
Short : "List all inconsistent objects from the Hasura Metadata" ,
Long : "At times, when developing, the Hasura Metadata can become inconsistent. This command can be used to list all inconsistent objects from the Hasura Metadata and allow you to understand why your project's Metadata is in an inconsistent state." ,
2019-12-25 11:33:06 +03:00
SilenceUsage : true ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-08 16:04:14 +03:00
op := genOpName ( cmd , "RunE" )
2019-12-25 11:33:06 +03:00
err := opts . run ( )
opts . EC . Spinner . Stop ( )
if err != nil {
2022-11-08 16:04:14 +03:00
return errors . E ( op , fmt . Errorf ( "failed to list inconsistent metadata: %w" , err ) )
2019-12-25 11:33:06 +03:00
}
if opts . isConsistent {
opts . EC . Logger . Println ( "metadata is consistent" )
}
return nil
} ,
}
2021-07-16 08:26:00 +03:00
f := metadataInconsistencyListCmd . Flags ( )
f . StringVarP ( & opts . outputFormat , "output" , "o" , "" , "select output format for inconsistent metadata objects(Allowed values: json)" )
2019-12-25 11:33:06 +03:00
return metadataInconsistencyListCmd
}
type metadataInconsistencyListOptions struct {
EC * cli . ExecutionContext
2021-07-16 08:26:00 +03:00
outputFormat string
2019-12-25 11:33:06 +03:00
isConsistent bool
2021-07-23 12:49:44 +03:00
inconsistentObjects [ ] projectmetadata . InconsistentMetadataObject
2019-12-25 11:33:06 +03:00
}
2021-07-23 12:49:44 +03:00
func ( o * metadataInconsistencyListOptions ) read ( handler * projectmetadata . Handler ) error {
2022-11-08 16:04:14 +03:00
var op errors . Op = "commands.metadataInconsistencyListOptions.read"
2021-04-01 08:13:24 +03:00
var err error
o . isConsistent , o . inconsistentObjects , err = handler . GetInconsistentMetadata ( )
2019-12-25 11:33:06 +03:00
if err != nil {
2022-11-08 16:04:14 +03:00
return errors . E ( op , err )
2019-12-25 11:33:06 +03:00
}
return nil
}
func ( o * metadataInconsistencyListOptions ) run ( ) error {
2022-11-08 16:04:14 +03:00
var op errors . Op = "commands.metadataInconsistencyListOptions.run"
2019-12-25 11:33:06 +03:00
o . EC . Spin ( "Getting inconsistent metadata..." )
2021-07-23 12:49:44 +03:00
err := o . read ( projectmetadata . NewHandlerFromEC ( o . EC ) )
2019-12-25 11:33:06 +03:00
if err != nil {
2022-11-08 16:04:14 +03:00
return errors . E ( op , err )
2019-12-25 11:33:06 +03:00
}
if o . isConsistent {
return nil
}
2021-07-16 08:26:00 +03:00
if o . outputFormat == "json" {
jsonBytes , err := json . MarshalIndent ( o . inconsistentObjects , "" , " " )
if err != nil {
2022-11-08 16:04:14 +03:00
return errors . E ( op , err )
2021-07-16 08:26:00 +03:00
}
o . EC . Spinner . Stop ( )
fmt . Fprintln ( o . EC . Stdout , string ( jsonBytes ) )
return nil
}
table := util . NewTableWriter ( o . EC . Stdout )
table . SetHeader ( [ ] string { "NAME" , "TYPE" , "DESCRIPTION" , "REASON" } )
2019-12-25 11:33:06 +03:00
for _ , obj := range o . inconsistentObjects {
2021-07-16 08:26:00 +03:00
table . Append ( [ ] string {
2019-12-25 11:33:06 +03:00
obj . GetName ( ) ,
obj . GetType ( ) ,
obj . GetDescription ( ) ,
obj . GetReason ( ) ,
2021-07-16 08:26:00 +03:00
} )
2019-12-25 11:33:06 +03:00
}
o . EC . Spinner . Stop ( )
2021-07-16 08:26:00 +03:00
table . Render ( )
2019-12-25 11:33:06 +03:00
return nil
}