Skip to content

Commit

Permalink
Merge pull request #248 from CircleCI-Public/setup-no-prompt
Browse files Browse the repository at this point in the history
[CIRCLE-13842]: Add circleci setup --no-prompt for easy scripting of config
  • Loading branch information
Zachary Scott authored Jan 4, 2019
2 parents f9fe918 + 8937c9b commit df380e9
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 149 deletions.
42 changes: 15 additions & 27 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd_test

import (
"fmt"
"net/http"
"os"
"os/exec"
Expand All @@ -17,48 +16,41 @@ import (

var _ = Describe("Check", func() {
var (
command *exec.Cmd
err error
checkCLI string
tempHome string
testServer *ghttp.Server
updateCheck *settings.UpdateCheck
updateFile *os.File
command *exec.Cmd
err error
checkCLI string
tempSettings *temporarySettings
testServer *ghttp.Server
updateCheck *settings.UpdateCheck
)

BeforeEach(func() {
checkCLI, err = gexec.Build("github.com/CircleCI-Public/circleci-cli")
Expect(err).ShouldNot(HaveOccurred())

tempHome, _, updateFile = withTempSettings()
tempSettings = withTempSettings()

updateCheck = &settings.UpdateCheck{
LastUpdateCheck: time.Time{},
}

updateCheck.FileUsed = updateFile.Name()
updateCheck.FileUsed = tempSettings.updateFile.Name()
err = updateCheck.WriteToDisk()
Expect(err).ShouldNot(HaveOccurred())

testServer = ghttp.NewServer()

command = exec.Command(checkCLI, "help",
"--github-api", testServer.URL(),
)

command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
command = commandWithHome(checkCLI, tempSettings.home,
"help", "--github-api", testServer.URL(),
)
})

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

Describe("update auto checks with a new release", func() {
var (
response string
)
var response string

BeforeEach(func() {
checkCLI, err = gexec.Build("github.com/CircleCI-Public/circleci-cli",
Expand All @@ -67,14 +59,10 @@ var _ = Describe("Check", func() {
)
Expect(err).ShouldNot(HaveOccurred())

tempHome, _, _ = withTempSettings()

command = exec.Command(checkCLI, "help",
"--github-api", testServer.URL(),
)
tempSettings = withTempSettings()

command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
command = commandWithHome(checkCLI, tempSettings.home,
"help", "--github-api", testServer.URL(),
)

response = `
Expand Down
65 changes: 47 additions & 18 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"testing"

Expand All @@ -22,40 +23,68 @@ var _ = BeforeSuite(func() {
Ω(err).ShouldNot(HaveOccurred())
})

func withTempSettings() (string, *os.File, *os.File) {
var (
tempHome string
err error
config *os.File
update *os.File
)
type temporarySettings struct {
home string
configFile *os.File
configPath string
updateFile *os.File
updatePath string
}

tempHome, err = ioutil.TempDir("", "circleci-cli-test-")
func (tempSettings temporarySettings) writeToConfigAndClose(contents []byte) {
_, err := tempSettings.configFile.Write(contents)
Expect(err).ToNot(HaveOccurred())
Expect(tempSettings.configFile.Close()).To(Succeed())
}

func (tempSettings temporarySettings) assertConfigRereadMatches(contents string) {
file, err := os.Open(tempSettings.configPath)
Expect(err).ShouldNot(HaveOccurred())

const (
settingsPath = ".circleci"
configFilename = "cli.yml"
updateCheckFilename = "update_check.yml"
reread, err := ioutil.ReadAll(file)
Expect(err).ShouldNot(HaveOccurred())
Expect(string(reread)).To(Equal(contents))
}

func commandWithHome(bin, home string, args ...string) *exec.Cmd {
command := exec.Command(bin, args...)

command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", home),
fmt.Sprintf("USERPROFILE=%s", home), // windows
)

Expect(os.Mkdir(filepath.Join(tempHome, settingsPath), 0700)).To(Succeed())
return command
}

func withTempSettings() *temporarySettings {
var err error

tempSettings := &temporarySettings{}

tempSettings.home, err = ioutil.TempDir("", "circleci-cli-test-")
Expect(err).ToNot(HaveOccurred())

settingsPath := filepath.Join(tempSettings.home, ".circleci")

Expect(os.Mkdir(settingsPath, 0700)).To(Succeed())

tempSettings.configPath = filepath.Join(settingsPath, "cli.yml")

config, err = os.OpenFile(
filepath.Join(tempHome, settingsPath, configFilename),
tempSettings.configFile, err = os.OpenFile(tempSettings.configPath,
os.O_RDWR|os.O_CREATE,
0600,
)
Expect(err).ToNot(HaveOccurred())

update, err = os.OpenFile(
filepath.Join(tempHome, settingsPath, updateCheckFilename),
tempSettings.updatePath = filepath.Join(settingsPath, "update_check.yml")
tempSettings.updateFile, err = os.OpenFile(tempSettings.updatePath,
os.O_RDWR|os.O_CREATE,
0600,
)
Expect(err).ToNot(HaveOccurred())

return tempHome, config, update
return tempSettings
}

var _ = AfterSuite(func() {
Expand Down
12 changes: 4 additions & 8 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -1760,23 +1759,20 @@ query namespaceOrbs ($namespace: String, $after: String!) {
})

Describe("when creating an orb without a token", func() {
var tempHome string
var tempSettings *temporarySettings

BeforeEach(func() {
tempHome, _, _ = withTempSettings()
tempSettings = withTempSettings()

command = exec.Command(pathCLI,
command = commandWithHome(pathCLI, tempSettings.home,
"orb", "create", "bar-ns/foo-orb",
"--skip-update-check",
"--token", "",
)
command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
)
})

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

It("instructs the user to run 'circleci setup' and create a new token", func() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func MakeCommands() *cobra.Command {
rootCmd.PersistentFlags().BoolVar(&rootOptions.Debug,
"debug", rootOptions.Debug, "Enable debug logging.")
rootCmd.PersistentFlags().StringVar(&rootTokenFromFlag,
"token", "", "your token for using CircleCI")
"token", "", "your token for using CircleCI, also CIRCLECI_CLI_TOKEN")
rootCmd.PersistentFlags().StringVar(&rootOptions.Host,
"host", rootOptions.Host, "URL to your CircleCI host")
"host", rootOptions.Host, "URL to your CircleCI host, also CIRCLECI_CLI_HOST")
rootCmd.PersistentFlags().StringVar(&rootOptions.Endpoint,
"endpoint", rootOptions.Endpoint, "URI to your CircleCI GraphQL API endpoint")

Expand Down
61 changes: 15 additions & 46 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package cmd_test

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

"github.com/CircleCI-Public/circleci-cli/cmd"
. "github.com/onsi/ginkgo"
Expand All @@ -24,14 +21,14 @@ var _ = Describe("Root", func() {

Describe("build without auto update", func() {
var (
command *exec.Cmd
err error
noUpdateCLI string
tempHome string
command *exec.Cmd
err error
noUpdateCLI string
tempSettings *temporarySettings
)

BeforeEach(func() {
tempHome, _, _ = withTempSettings()
tempSettings = withTempSettings()

noUpdateCLI, err = gexec.Build("github.com/CircleCI-Public/circleci-cli",
"-ldflags",
Expand All @@ -41,15 +38,12 @@ var _ = Describe("Root", func() {
})

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

It("reports update command as unavailable", func() {
command = exec.Command(noUpdateCLI, "help",
"--skip-update-check",
)
command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
command = commandWithHome(noUpdateCLI, tempSettings.home,
"help", "--skip-update-check",
)

session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expand Down Expand Up @@ -105,50 +99,25 @@ var _ = Describe("Root", func() {

Describe("token in help text", func() {
var (
command *exec.Cmd
tempHome string
)

const (
configDir = ".circleci"
configFile = "cli.yml"
command *exec.Cmd
tempSettings *temporarySettings
)

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

command = exec.Command(pathCLI, "help",
"--skip-update-check",
)
command.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", tempHome),
fmt.Sprintf("USERPROFILE=%s", tempHome), // windows
command = commandWithHome(pathCLI, tempSettings.home,
"help", "--skip-update-check",
)
})

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

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

BeforeEach(func() {
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())

_, err = config.Write([]byte(`token: secret`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
tempSettings.writeToConfigAndClose([]byte(`token: secret`))
})

It("does not include the users token in help text", func() {
Expand Down
Loading

0 comments on commit df380e9

Please sign in to comment.