MCPcopy
hub / github.com/hasura/graphql-engine / apply

Function apply

cli/commands/metadata_handlers.go:278–333  ·  view source on GitHub ↗
(o *MetadataApplyOptions, mode cli.MetadataMode)

Source from the content-addressed store, hash-verified

276}
277
278func apply(o *MetadataApplyOptions, mode cli.MetadataMode) error {
279 var op errors.Op = "commands.apply"
280 var localMetadataBytes []byte
281 var err error
282
283 localMetadataBytes, err = ioutil.ReadFile(o.EC.MetadataFile)
284 if err != nil {
285 return errors.E(op, fmt.Errorf("reading metadata file: %w", err))
286 }
287 if o.DryRun {
288 if len(o.rawOutput) == 0 {
289 o.rawOutput = string(rawOutputFormatJSON)
290 }
291 if err := writeByOutputFormat(o.EC.Stdout, localMetadataBytes, rawOutputFormat(o.rawOutput)); err != nil {
292 return errors.E(op, fmt.Errorf("displaying metadata failed: %w", err))
293 }
294 return nil
295 }
296 if mode == cli.MetadataModeYAML {
297 localMetadataBytes, err = metadatautil.YAMLToJSON(localMetadataBytes)
298 if err != nil {
299 return errors.E(op, fmt.Errorf("parsing yaml metadata to json: %w", err))
300 }
301 }
302 var metadata interface{}
303 err = json.Unmarshal(localMetadataBytes, &metadata)
304 if err != nil {
305 return errors.E(op, fmt.Errorf("parsing metadata as json: %w", err))
306 }
307 if o.EC.Config.Version == cli.V2 {
308 _, err := cli.GetCommonMetadataOps(o.EC).ReplaceMetadata(bytes.NewReader(localMetadataBytes))
309 if err != nil {
310 return errors.E(op, err)
311 }
312 return nil
313 }
314 r, err := o.EC.APIClient.V1Metadata.V2ReplaceMetadata(hasura.V2ReplaceMetadataArgs{
315 AllowInconsistentMetadata: !o.DisallowInconsistencies,
316 Metadata: metadata,
317 })
318 if err != nil {
319 return errors.E(op, errorApplyingMetadata(err))
320 }
321 if !r.IsConsistent {
322 o.EC.Logger.Warn("Metadata is inconsistent")
323 o.EC.Logger.Warn("Use 'hasura metadata ic list' command to list inconsistent objects")
324 }
325 if len(o.rawOutput) != 0 {
326 // if not a dry run fetch metadata from and server and print it to stdout
327 if err := getMetadataFromServerAndWriteToStdoutByFormat(o.EC, rawOutputFormat(o.rawOutput)); err != nil {
328 return errors.E(op, err)
329 }
330 return nil
331 }
332 return nil
333}
334
335func diff(o *MetadataDiffOptions, diffType DiffType) error {

Callers 2

ApplyMethod · 0.70
ApplyMethod · 0.70

Calls 6

writeByOutputFormatFunction · 0.85
rawOutputFormatTypeAlias · 0.85
errorApplyingMetadataFunction · 0.85
ReplaceMetadataMethod · 0.65
V2ReplaceMetadataMethod · 0.65

Tested by

no test coverage detected