Skip to content

Commit

Permalink
Merge pull request #23 from CircleCI-Public/read-from-stdin
Browse files Browse the repository at this point in the history
[CIRCLE-12186] optionally read config file from stdin
  • Loading branch information
Zachary Scott authored Jul 24, 2018
2 parents 6d28439 + f70fabd commit 7643b5e
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 32 deletions.
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) {
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
28 changes: 20 additions & 8 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"gopkg.in/yaml.v2"
)

var orbPath string
var orbVersion string
var orbID string

Expand All @@ -28,23 +27,25 @@ 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{
Use: "publish",
Use: "publish [orb.yml]",
Short: "publish a version of an orb",
RunE: publishOrb,
Args: cobra.MaximumNArgs(1),
}
orbPublishCommand.PersistentFlags().StringVarP(&orbPath, "path", "p", "orb.yml", "path to orb file")
orbPublishCommand.PersistentFlags().StringVarP(&orbVersion, "orb-version", "o", "", "version of orb to publish")
orbPublishCommand.PersistentFlags().StringVarP(&orbID, "orb-id", "i", "", "id of orb to publish")

Expand All @@ -55,10 +56,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 +179,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 +203,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 All @@ -215,6 +223,10 @@ func expandOrb(cmd *cobra.Command, args []string) error {

func publishOrb(cmd *cobra.Command, args []string) error {
ctx := context.Background()
orbPath := defaultOrbPath
if len(args) == 1 {
orbPath = args[0]
}

response, err := api.OrbPublish(ctx, Logger, orbPath, orbVersion, orbID)

Expand Down
Loading

0 comments on commit 7643b5e

Please sign in to comment.