-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor common code between orb and config commands
- Loading branch information
1 parent
916757f
commit b558ce3
Showing
5 changed files
with
131 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/client" | ||
"github.com/CircleCI-Public/circleci-cli/logger" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// ConfigResponse is a structure that matches the result of the GQL | ||
// query, so that we can use mapstructure to convert from | ||
// nested maps to a strongly typed struct. | ||
type ConfigResponse struct { | ||
Valid bool | ||
SourceYaml string | ||
OutputYaml string | ||
|
||
Errors []struct { | ||
Message string | ||
} | ||
} | ||
|
||
// ToError returns an error created from any error messages, or nil. | ||
func (response ConfigResponse) ToError() error { | ||
messages := []string{} | ||
|
||
for i := range response.Errors { | ||
messages = append(messages, response.Errors[i].Message) | ||
} | ||
|
||
return errors.New(strings.Join(messages, ": ")) | ||
} | ||
|
||
func loadYaml(path string) (string, error) { | ||
|
||
config, err := ioutil.ReadFile(path) | ||
|
||
if err != nil { | ||
return "", errors.Wrapf(err, "Could not load config file at %s", path) | ||
} | ||
|
||
return string(config), nil | ||
} | ||
|
||
func buildAndOrbQuery(ctx context.Context, logger *logger.Logger, configPath string, response interface{}, query string) error { | ||
config, err := loadYaml(configPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
request := client.NewAuthorizedRequest(viper.GetString("token"), query) | ||
request.Var("config", config) | ||
client := client.NewClient(viper.GetString("endpoint"), logger) | ||
|
||
err = client.Run(ctx, request, response) | ||
|
||
if err != nil { | ||
return errors.Wrap(err, "Unable to validate config") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ConfigQuery calls the GQL API to validate and expand config | ||
func ConfigQuery(ctx context.Context, logger *logger.Logger, configPath string) (*ConfigResponse, error) { | ||
var response struct { | ||
BuildConfig struct { | ||
ConfigResponse | ||
} | ||
} | ||
return &response.BuildConfig.ConfigResponse, buildAndOrbQuery(ctx, logger, configPath, &response, ` | ||
query ValidateConfig ($config: String!) { | ||
buildConfig(configYaml: $config) { | ||
valid, | ||
errors { message }, | ||
sourceYaml, | ||
outputYaml | ||
} | ||
}`) | ||
} | ||
|
||
// OrbQuery validated and expands an orb. | ||
func OrbQuery(ctx context.Context, logger *logger.Logger, configPath string) (*ConfigResponse, error) { | ||
var response struct { | ||
OrbConfig struct { | ||
ConfigResponse | ||
} | ||
} | ||
|
||
return &response.OrbConfig.ConfigResponse, buildAndOrbQuery(ctx, logger, configPath, &response, ` | ||
query ValidateOrb ($config: String!) { | ||
orbConfig(orbYaml: $config) { | ||
valid, | ||
errors { message }, | ||
sourceYaml, | ||
outputYaml | ||
} | ||
}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.