Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIRCLE-12186] optionally read config file from stdin #23

Merged
merged 12 commits into from
Jul 24, 2018
10 changes: 8 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"io/ioutil"
"os"
"strings"

"github.com/CircleCI-Public/circleci-cli/client"
Expand Down Expand Up @@ -54,8 +55,13 @@ func (response GQLResponseErrors) ToError() error {
}

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

config, err := ioutil.ReadFile(path)
var err error
var config []byte
if path == "-" {
config, err = ioutil.ReadAll(os.Stdin)
} else {
config, err = ioutil.ReadFile(path)
}

if err != nil {
return "", errors.Wrapf(err, "Could not load config file at %s", path)
Expand Down
22 changes: 14 additions & 8 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,25 @@ func (f tmpFile) write(fileContent string) error {
return err
}

func openTmpFile(path string) (tmpFile, error) {
func openTmpDir(prefix string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think prefix is ever used here - copy + paste bug?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used here:
https://github.com/CircleCI-Public/circleci-cli/pull/23/files/1c600eaec59f3c63688668b38f7e6753e88de3bb#diff-bee1de2bffa142cede85d49cea1a90f7R76

So you can create a temp directory without specifying the prefix, just do openTmpDir("").

var dir string
if prefix == "" {
dir = "circleci-cli-test-"
} else {
dir = prefix
}
tmpDir, err := ioutil.TempDir("", dir)
return tmpDir, err
}

func openTmpFile(directory string, path string) (tmpFile, error) {
var (
config tmpFile = tmpFile{}
err error
)

tmpDir, err := ioutil.TempDir("", "circleci-cli-test-")
if err != nil {
return config, err
}

config.RootDir = tmpDir
config.Path = filepath.Join(tmpDir, path)
config.RootDir = directory
config.Path = filepath.Join(directory, path)

err = os.MkdirAll(filepath.Dir(config.Path), 0700)
if err != nil {
Expand Down
17 changes: 14 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ import (
"github.com/spf13/cobra"
)

const defaultConfigPath = ".circleci/config.yml"

func newConfigCommand() *cobra.Command {
configCmd := &cobra.Command{
Use: "config",
Short: "Operate on build config files",
}

validateCommand := &cobra.Command{
Use: "validate",
Use: "validate [config.yml]",
Aliases: []string{"check"},
Short: "Check that the config file is well formed.",
RunE: validateConfig,
Args: cobra.MaximumNArgs(1),
}

expandCommand := &cobra.Command{
Use: "expand",
Use: "expand [config.yml]",
Short: "Expand the config.",
RunE: expandConfig,
Args: cobra.MaximumNArgs(1),
}

configCmd.AddCommand(validateCommand)
Expand All @@ -34,6 +38,10 @@ func newConfigCommand() *cobra.Command {

func validateConfig(cmd *cobra.Command, args []string) error {

configPath := defaultConfigPath
if len(args) == 1 {
configPath = args[0]
}
ctx := context.Background()
response, err := api.ConfigQuery(ctx, Logger, configPath)

Expand All @@ -51,7 +59,10 @@ func validateConfig(cmd *cobra.Command, args []string) error {

func expandConfig(cmd *cobra.Command, args []string) error {
ctx := context.Background()

configPath := defaultConfigPath
if len(args) == 1 {
configPath = args[0]
}
response, err := api.ConfigQuery(ctx, Logger, configPath)

if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd_test

import (
"net/http"
"os"
"os/exec"
"path/filepath"

Expand All @@ -17,18 +18,23 @@ var _ = Describe("Config", func() {
var (
testServer *ghttp.Server
config tmpFile
tmpDir string
)

BeforeEach(func() {
var err error
config, err = openTmpFile(filepath.Join(".circleci", "config.yaml"))
tmpDir, err = openTmpDir("")
Expect(err).ToNot(HaveOccurred())

config, err = openTmpFile(tmpDir, filepath.Join(".circleci", "config.yaml"))
Expect(err).ToNot(HaveOccurred())

testServer = ghttp.NewServer()
})

AfterEach(func() {
config.close()
os.RemoveAll(tmpDir)
testServer.Close()
})

Expand All @@ -44,7 +50,7 @@ var _ = Describe("Config", func() {
"config", "validate",
"-t", token,
"-e", testServer.URL(),
"-c", config.Path,
config.Path,
)
})

Expand Down Expand Up @@ -120,7 +126,7 @@ var _ = Describe("Config", func() {
"config", "expand",
"-t", token,
"-e", testServer.URL(),
"-c", config.Path,
config.Path,
)
})

Expand Down
19 changes: 14 additions & 5 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ func newOrbCommand() *cobra.Command {
}

orbValidateCommand := &cobra.Command{
Use: "validate",
Use: "validate [orb.yml]",
Short: "validate an orb.yml",
RunE: validateOrb,
Args: cobra.MaximumNArgs(1),
}

orbExpandCommand := &cobra.Command{
Use: "expand",
Use: "expand [orb.yml]",
Short: "expand an orb.yml",
RunE: expandOrb,
Args: cobra.MaximumNArgs(1),
}

orbPublishCommand := &cobra.Command{
Expand All @@ -55,10 +57,8 @@ func newOrbCommand() *cobra.Command {

orbCommand.AddCommand(orbListCommand)

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

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

orbCommand.AddCommand(orbPublishCommand)
Expand Down Expand Up @@ -180,8 +180,14 @@ query ListOrbs ($after: String!) {
return nil
}

const defaultOrbPath = "orb.yml"

func validateOrb(cmd *cobra.Command, args []string) error {
ctx := context.Background()
orbPath := defaultOrbPath
if len(args) == 1 {
orbPath = args[0]
}
response, err := api.OrbQuery(ctx, Logger, orbPath)

if err != nil {
Expand All @@ -198,7 +204,10 @@ func validateOrb(cmd *cobra.Command, args []string) error {

func expandOrb(cmd *cobra.Command, args []string) error {
ctx := context.Background()

orbPath := defaultOrbPath
if len(args) == 1 {
orbPath = args[0]
}
response, err := api.OrbQuery(ctx, Logger, orbPath)

if err != nil {
Expand Down
Loading