Skip to content

Commit

Permalink
Remove local command and move diagnostic and setup back
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Aug 2, 2018
1 parent 19f60ac commit dc10296
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 290 deletions.
35 changes: 35 additions & 0 deletions cmd/diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func newDiagnosticCommand() *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 {
endpoint := viper.GetString("endpoint")
token := viper.GetString("token")

Logger.Infoln("\n---\nCircleCI CLI Diagnostics\n---\n")
Logger.Infof("Config found: %v\n", viper.ConfigFileUsed())

Logger.Infof("GraphQL API endpoint: %s\n", endpoint)

if token == "token" || token == "" {
return errors.New("please set a token")
}
Logger.Infoln("OK, got a token.")
Logger.Infof("Verbose mode: %v\n", viper.GetBool("verbose"))

return nil
}
96 changes: 96 additions & 0 deletions cmd/diagnostic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmd_test

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)

var _ = Describe("Diagnostic", func() {
var (
tempHome string
command *exec.Cmd
)

BeforeEach(func() {
var err error
tempHome, err = ioutil.TempDir("", "circleci-cli-test-")
Expect(err).ToNot(HaveOccurred())

command = exec.Command(pathCLI, "diagnostic")
command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
)
})

AfterEach(func() {
Expect(os.RemoveAll(tempHome)).To(Succeed())
})

Describe("existing config file", func() {
var config *os.File

BeforeEach(func() {
const (
configDir = ".circleci"
configFile = "cli.yml"
)

Expect(os.Mkdir(filepath.Join(tempHome, configDir), 0700)).To(Succeed())

var err error
config, err = os.OpenFile(
filepath.Join(tempHome, configDir, configFile),
os.O_RDWR|os.O_CREATE,
0600,
)
Expect(err).ToNot(HaveOccurred())
})

Describe("token and endpoint set in config file", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com/graphql
token: mytoken
`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
})

It("print success", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out).Should(gbytes.Say("GraphQL API endpoint: https://example.com/graphql"))
Eventually(session.Out).Should(gbytes.Say("OK, got a token."))
Eventually(session).Should(gexec.Exit(0))
})
})

Context("token set to empty string in config file", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`
endpoint: https://example.com/graphql
token:
`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
})

It("print error", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err).Should(gbytes.Say("Error: please set a token"))
Eventually(session.Out).Should(gbytes.Say("GraphQL API endpoint: https://example.com/graphql"))
Eventually(session).Should(gexec.Exit(255))
})
})
})
})
181 changes: 0 additions & 181 deletions cmd/local_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func MakeCommands() *cobra.Command {
rootCmd.AddCommand(newOrbCommand())
rootCmd.AddCommand(newBuildCommand())
rootCmd.AddCommand(newVersionCommand())
rootCmd.AddCommand(newLocalCommand())
rootCmd.AddCommand(newDiagnosticCommand())
rootCmd.AddCommand(newSetupCommand())
rootCmd.AddCommand(newUpdateCommand())
rootCmd.AddCommand(newRegistryCommand())
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose logging.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ = Describe("Root", func() {

It("can create commands", func() {
commands := cmd.MakeCommands()
Expect(len(commands.Commands())).To(Equal(8))
Expect(len(commands.Commands())).To(Equal(9))
})

})
Expand Down
Loading

0 comments on commit dc10296

Please sign in to comment.