Skip to content

Commit

Permalink
WIP: passing test, needs refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
circlecai committed Jun 28, 2018
1 parent 7b711bf commit 70d5395
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
36 changes: 24 additions & 12 deletions cmd/orb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"context"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -47,7 +48,7 @@ func newOrbCommand() *cobra.Command {
Short: "Operate on orbs",
}

orbValidateCommand.PersistentFlags().StringVarP(&orbPath, "path", "p", "orb.yml", "path to orb config")
orbValidateCommand.PersistentFlags().StringVarP(&orbPath, "path", "p", "orb.yml", "path to orb file")
orbCommand.AddCommand(orbListCommand)
orbCommand.AddCommand(orbValidateCommand)

Expand Down Expand Up @@ -125,7 +126,7 @@ query ListOrbs ($after: String!) {

func loadOrbYaml(path string) (string, error) {

config, err := ioutil.ReadFile(path)
orb, err := ioutil.ReadFile(path)

fmt.Fprintln(os.Stderr, "******************** orb.go")
fmt.Fprintln(os.Stderr, path)
Expand All @@ -134,51 +135,62 @@ func loadOrbYaml(path string) (string, error) {
return "", errors.Wrapf(err, "Could not load orb file at %s", path)
}

return string(config), nil
return string(orb), nil
}


func (response orbConfigResponse) processErrors() error {
var buffer bytes.Buffer

buffer.WriteString("\n")
for i := range response.OrbConfig.Errors {
buffer.WriteString("-- ")
buffer.WriteString(response.OrbConfig.Errors[i].Message)
buffer.WriteString(",\n")
}

return errors.New(buffer.String())
}

func orbValidateQuery(ctx context.Context) (*orbConfigResponse, error) {

query := `
query ValidateOrb ($orb: String!) {
orbConfig(configYaml: $orb) {
orbConfig(orbYaml: $orb) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`

fmt.Fprintln(os.Stderr, "********************asdfasdfasdfsa")
fmt.Fprintln(os.Stderr, orbPath)

config, err := loadOrbYaml(orbPath)
orb, err := loadOrbYaml(orbPath)
if err != nil {
return nil, err
}

variables := map[string]string{
"config": config,
"orb": orb,
}

var response orbConfigResponse
err = queryAPI(ctx, query, variables, &response)
if err != nil {
return nil, errors.Wrap(err, "Unable to validate config")
return nil, errors.Wrap(err, "Unable to validate orb")
}

return &response, nil
}

func validateOrb(cmd *cobra.Command, args []string) error {
ctx := context.Background()
response, err := configQuery(ctx)
response, err := orbValidateQuery(ctx)

if err != nil {
return err
}

if !response.BuildConfig.Valid {
if !response.OrbConfig.Valid {
return response.processErrors()
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ var _ = Describe("Orb", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Orb at myorb/orb.yml is valid"))
// the .* is because the full path with temp dir is printed
Eventually(session.Out).Should(gbytes.Say("Orb at .*myorb/orb.yml is valid"))
Eventually(session).Should(gexec.Exit(0))
})

Expand Down

0 comments on commit 70d5395

Please sign in to comment.