Skip to content

Commit

Permalink
Merge pull request #38 from CircleCI-Public/12762-cleanup
Browse files Browse the repository at this point in the history
[CIRCLE-12762] Command cosmetics cleanup
  • Loading branch information
Zachary Scott authored Aug 2, 2018
2 parents e58a277 + e8f77a8 commit 01aa6aa
Show file tree
Hide file tree
Showing 13 changed files with 486 additions and 347 deletions.
38 changes: 37 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ func OrbQuery(ctx context.Context, logger *logger.Logger, configPath string) (*C

// OrbPublish publishes a new version of an orb
func OrbPublish(ctx context.Context, logger *logger.Logger,
configPath string, orbVersion string, orbID string) (*PublishOrbResponse, error) {
name string, configPath string, orbVersion string) (*PublishOrbResponse, error) {
orbID, err := getOrbID(ctx, logger, name)
if err != nil {
return nil, err
}

var response struct {
PublishOrb struct {
PublishOrbResponse
Expand Down Expand Up @@ -194,6 +199,37 @@ func OrbPublish(ctx context.Context, logger *logger.Logger,
return &response.PublishOrb.PublishOrbResponse, err
}

func getOrbID(ctx context.Context, logger *logger.Logger, name string) (string, error) {
var response struct {
Orb struct {
ID string
}
}

query := `query($name: String!) {
orb(name: $name) {
id
}
}`

request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("name", name)

graphQLclient := client.NewClient(viper.GetString("endpoint"), logger)

err := graphQLclient.Run(ctx, request, &response)

if err != nil {
return "", err
}

if response.Orb.ID == "" {
return "", fmt.Errorf("the %s orb could not be found", name)
}

return response.Orb.ID, nil
}

func createNamespaceWithOwnerID(ctx context.Context, logger *logger.Logger, name string, ownerID string) (*CreateNamespaceResponse, error) {
var response struct {
CreateNamespace struct {
Expand Down
36 changes: 0 additions & 36 deletions cmd/collapse.go

This file was deleted.

92 changes: 0 additions & 92 deletions cmd/collapse_test.go

This file was deleted.

30 changes: 29 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/filetree"
"github.com/pkg/errors"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)

const defaultConfigPath = ".circleci/config.yml"
Expand All @@ -15,6 +18,13 @@ func newConfigCommand() *cobra.Command {
Short: "Operate on build config files",
}

collapseCommand := &cobra.Command{
Use: "collapse [path]",
Short: "Collapse your CircleCI configuration to a single file",
RunE: collapseConfig,
Args: cobra.MaximumNArgs(1),
}

validateCommand := &cobra.Command{
Use: "validate [config.yml]",
Aliases: []string{"check"},
Expand All @@ -30,14 +40,14 @@ func newConfigCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
}

configCmd.AddCommand(collapseCommand)
configCmd.AddCommand(validateCommand)
configCmd.AddCommand(expandCommand)

return configCmd
}

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

configPath := defaultConfigPath
if len(args) == 1 {
configPath = args[0]
Expand Down Expand Up @@ -76,3 +86,21 @@ func expandConfig(cmd *cobra.Command, args []string) error {
Logger.Info(response.OutputYaml)
return nil
}

func collapseConfig(cmd *cobra.Command, args []string) error {
root := "."
if len(args) > 0 {
root = args[0]
}
tree, err := filetree.NewTree(root)
if err != nil {
return errors.Wrap(err, "An error occurred trying to build the tree")
}

y, err := yaml.Marshal(&tree)
if err != nil {
return errors.Wrap(err, "Failed trying to marshal the tree to YAML ")
}
Logger.Infof("%s\n", string(y))
return nil
}
82 changes: 82 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -206,4 +207,85 @@ var _ = Describe("Config", func() {
})
})
})

Describe("collapse", func() {
var (
command *exec.Cmd
results []byte
)

Describe("a .circleci folder with config.yml and local orbs folder containing the hugo orb", func() {
BeforeEach(func() {
var err error
command = exec.Command(pathCLI, "config", "collapse", "testdata/hugo-collapse/.circleci")
results, err = ioutil.ReadFile("testdata/hugo-collapse/result.yml")
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("local orbs folder with mixed inline and local commands, jobs, etc", func() {
BeforeEach(func() {
var err error
var path string = "nested-orbs-and-local-commands-etc"
command = exec.Command(pathCLI, "config", "collapse", filepath.Join("testdata", path, "test"))
results, err = ioutil.ReadFile(filepath.Join("testdata", path, "result.yml"))
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("an orb containing local executors and commands in folder", func() {
BeforeEach(func() {
var err error
command = exec.Command(pathCLI, "config", "collapse", "testdata/myorb/test")
results, err = ioutil.ReadFile("testdata/myorb/result.yml")
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("with a large nested config including rails orb", func() {
BeforeEach(func() {
var err error
var path string = "test-with-large-nested-rails-orb"
command = exec.Command(pathCLI, "config", "collapse", filepath.Join("testdata", path, "test"))
results, err = ioutil.ReadFile(filepath.Join("testdata", path, "result.yml"))
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})
})
})
4 changes: 3 additions & 1 deletion cmd/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
)

func newDiagnosticCommand() *cobra.Command {
return &cobra.Command{
diagnosticCommand := &cobra.Command{
Use: "diagnostic",
Short: "Check the status of your CircleCI CLI.",
RunE: diagnostic,
}

return diagnosticCommand
}

func diagnostic(cmd *cobra.Command, args []string) error {
Expand Down
Loading

0 comments on commit 01aa6aa

Please sign in to comment.