-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from circleci/validate
Skeleton for Config Validate
- Loading branch information
Showing
9 changed files
with
199 additions
and
68 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 @@ | ||
/vendor linguist-generated=true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/machinebox/graphql" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// Path to the config.yml file to operate on. | ||
var configPath string | ||
|
||
var configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "Operate on build config files", | ||
} | ||
|
||
var validateCommand = &cobra.Command{ | ||
Use: "validate", | ||
Aliases: []string{"check"}, | ||
Short: "Check that the config file is well formed.", | ||
RunE: validateConfig, | ||
} | ||
|
||
func init() { | ||
validateCommand.Flags().StringVarP(&configPath, "path", "p", ".circleci/config.yml", "path to build config") | ||
configCmd.AddCommand(validateCommand) | ||
} | ||
|
||
func validateConfig(cmd *cobra.Command, args []string) error { | ||
|
||
ctx := context.Background() | ||
|
||
// Define 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 validateResult struct { | ||
BuildConfig struct { | ||
Valid bool | ||
SourceYaml string | ||
Errors []struct { | ||
Message string | ||
} | ||
} | ||
} | ||
|
||
request := graphql.NewRequest(` | ||
query ValidateConfig ($config: String!) { | ||
buildConfig(configYaml: $config) { | ||
valid, | ||
errors { message }, | ||
sourceYaml | ||
} | ||
}`) | ||
|
||
config, err := ioutil.ReadFile(configPath) | ||
|
||
if err != nil { | ||
return errors.Wrapf(err, "Could not load config file at %s", configPath) | ||
} | ||
|
||
request.Var("config", string(config)) | ||
|
||
client := graphql.NewClient(viper.GetString("endpoint")) | ||
|
||
var result validateResult | ||
|
||
err = client.Run(ctx, request, &result) | ||
|
||
if err != nil { | ||
return errors.Wrap(err, "GraphQL query failed") | ||
} | ||
|
||
if !result.BuildConfig.Valid { | ||
|
||
var buffer bytes.Buffer | ||
|
||
for i := range result.BuildConfig.Errors { | ||
buffer.WriteString(result.BuildConfig.Errors[i].Message) | ||
buffer.WriteString("\n") | ||
} | ||
|
||
return fmt.Errorf("config file is invalid:\n%s", buffer.String()) | ||
} | ||
|
||
fmt.Println("Config is valid") | ||
return nil | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package settings | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"runtime" | ||
) | ||
|
||
// UserHomeDir returns the path to the current user's HOME directory. | ||
func UserHomeDir() string { | ||
if runtime.GOOS == "windows" { | ||
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") | ||
if home == "" { | ||
home = os.Getenv("USERPROFILE") | ||
} | ||
return home | ||
} | ||
return os.Getenv("HOME") | ||
} | ||
|
||
// EnsureSettingsFileExists does just that. | ||
func EnsureSettingsFileExists(filepath, filename string) error { | ||
// TODO - handle invalid YAML config files. | ||
_, err := os.Stat(filepath) | ||
|
||
if !os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
if err = os.MkdirAll(filepath, 0700); err != nil { | ||
return err | ||
} | ||
|
||
if _, err = os.Create(path.Join(filepath, filename)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |