Skip to content

Commit

Permalink
minor fixes for transform command
Browse files Browse the repository at this point in the history
  • Loading branch information
mlapshin committed Aug 31, 2018
1 parent d86b6ba commit 9e8159d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion init.go
Expand Up @@ -61,7 +61,7 @@ func InitCommand(c *cli.Context) error {
err := PerformInit(db, fhirVersion)

if err != nil {
return errors.Wrap(err, "failed to perform init command")
return errors.Wrap(err, "Failed to perform init command. Perhaps target database is not empty?")
}

log.Printf("Database initialized with FHIR schema version '%s'", fhirVersion)
Expand Down
22 changes: 10 additions & 12 deletions transform.go
Expand Up @@ -180,20 +180,19 @@ func doTransform(res map[string]interface{}, fhirVersion string) (map[string]int

// TransformCommand transforms FHIR resource to internal JSON representation
func TransformCommand(c *cli.Context) error {
if c.NArg() < 2 {
if c.NArg() != 1 {
cli.ShowCommandHelp(c, "transform")
fmt.Printf("You must provide a FHIR version for `fhirbase transform` command.\nKnow FHIR versions are: %v", AvailableSchemas)
os.Exit(1)

return fmt.Errorf("Missing required argument FILE")
}

fhirVersion := c.Args().Get(0)
filename := c.Args().Get(1)
fhirVersion := c.GlobalString("fhir")
filename := c.Args().Get(0)

fileContent, err := ioutil.ReadFile(filename)

if err != nil {
fmt.Printf("Error reading file %s: %v", filename, err)
os.Exit(1)
return errors.Wrapf(err, "Error reading file %s", filename)
}

iter := jsoniter.ConfigFastest.BorrowIterator(fileContent)
Expand All @@ -202,20 +201,19 @@ func TransformCommand(c *cli.Context) error {
res := iter.Read()

if res == nil {
fmt.Printf("Error parsing file %s", filename)
os.Exit(1)
return errors.Wrapf(err, "Error parsing file %s as JSON", filename)
}

out, err := doTransform(res.(map[string]interface{}), fhirVersion)

if err != nil {
fmt.Printf("Error performing transformation: %v", err)
os.Exit(1)
return errors.Wrap(err, "Error performing transformation")
}

outJson, err := jsoniter.ConfigFastest.MarshalIndent(out, "", " ")

fmt.Printf("%s\n", outJson)
os.Stdout.Write(outJson)
os.Stdout.Write([]byte("\n"))

return nil
}

0 comments on commit 9e8159d

Please sign in to comment.