Skip to content

Commit

Permalink
Merge pull request #4 from circleci/CIRCLE-11151/setup_ci
Browse files Browse the repository at this point in the history
Circle 11151/setup ci
  • Loading branch information
Zachary Scott authored Jun 5, 2018
2 parents d7e1768 + 2430763 commit a984096
Show file tree
Hide file tree
Showing 212 changed files with 188,079 additions and 32 deletions.
29 changes: 29 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: 2

workflows:
version: 2
ci:
jobs:
- test
- lint

jobs:
test:
docker:
- image: circleci/golang:1.10
working_directory: /go/src/github.com/circleci/circleci-cli
steps:
- checkout
- run: make test
lint:
docker:
- image: circleci/golang:1.10
working_directory: /go/src/github.com/circleci/circleci-cli
steps:
- checkout
- run:
name: Install
command: |
go get -u github.com/alecthomas/gometalinter
gometalinter --install
- run: make lint
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build/
TODO
circleci-cli
74 changes: 72 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ clean:

.PHONY: test
test:
go test -short ./...
go test -v -short ./...

.PHONY: cover
coverage:
go test -coverprofile=coverage.txt -covermode=count ./...

.PHONY: lint
lint:
gometalinter --deadline 60s --vendor ./...
26 changes: 26 additions & 0 deletions circleci_cli_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main_test

import (
"testing"

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

var pathCLI string

var _ = BeforeSuite(func() {
var err error
pathCLI, err = gexec.Build("github.com/circleci/circleci-cli")
Ω(err).ShouldNot(HaveOccurred())
})

var _ = AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})

func TestCircleciCli(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CircleciCli Suite")
}
5 changes: 4 additions & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func query(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

b, _ := json.MarshalIndent(resp, "", " ")
b, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.Fatalln("Could not parse graphql response", err.Error())
}
fmt.Print("Result: \n\n")
fmt.Println(string(b))
}
73 changes: 45 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var RootCmd = &cobra.Command{
// Execute adds all child commands to RootCmd and
// sets flags appropriately. This function is called
// by main.main(). It only needs to happen once to
// the RootCmd.
func Execute() {
addCommands()
if err := rootCmd.Execute(); err != nil {
os.Exit(-1)
}
}

var rootCmd = &cobra.Command{
Use: "cli",
Short: "Use CircleCI from the command line.",
Long: `Use CircleCI from the command line.`,
Expand All @@ -20,42 +32,42 @@ var (
cfgPathDefault = fmt.Sprintf("%s/.circleci/%s.yml", os.Getenv("HOME"), cfgName)
)

func AddCommands() {
RootCmd.AddCommand(diagnosticCmd)
RootCmd.AddCommand(queryCmd)
func addCommands() {
rootCmd.AddCommand(diagnosticCmd)
rootCmd.AddCommand(queryCmd)
}

// Add all child commands to RootCmd and set flags appropriately.
// This function is called by main.main().
// It only needs to happen once to the RootCmd.
func Execute() {
AddCommands()
if err := RootCmd.Execute(); err != nil {
os.Exit(-1)
func fatalOnError(msg string, err error) {
if err == nil {
return
}
log.Fatalln(msg, err.Error())
}

func init() {
cobra.OnInitialize(initConfig)

RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.circleci/cli.yml)")
RootCmd.PersistentFlags().StringP("host", "H", "https://circleci.com", "the host of your CircleCI install")
RootCmd.PersistentFlags().StringP("token", "t", "", "your token for using CircleCI")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.circleci/cli.yml)")
rootCmd.PersistentFlags().StringP("host", "H", "https://circleci.com", "the host of your CircleCI install")
rootCmd.PersistentFlags().StringP("token", "t", "", "your token for using CircleCI")

viper.BindPFlag("host", RootCmd.PersistentFlags().Lookup("host"))
viper.BindPFlag("token", RootCmd.PersistentFlags().Lookup("token"))
fatalOnError("Error binding host flag", viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host")))
fatalOnError("Error binding token flag", viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")))
}

// TODO: move config stuff to it's own package
func initConfig() {
if err := readConfig(); err != nil {
if err = createConfig(); err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
cfgFile = cfgPathDefault
readConfig() // reload config after creating it
if err := readConfig(); err == nil {
return
}

fatalOnError("Error creating a new config file", createConfig())

cfgFile = cfgPathDefault
fatalOnError(
"Failed to re-read config after creating a new file",
readConfig(), // reload config after creating it
)
}

func readConfig() (err error) {
Expand Down Expand Up @@ -86,22 +98,27 @@ func createConfig() (err error) {

path := fmt.Sprintf("%s/.circleci", os.Getenv("HOME"))

if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0775)
if _, err = os.Stat(path); os.IsNotExist(err) {
fatalOnError(
fmt.Sprintf("Error creating directory: '%s'", path),
os.Mkdir(path, 0644),
)
} else {
fatalOnError(fmt.Sprintf("Error accessing '%s'", path), err)
}

// Create default config file
if _, err := os.Create(cfgPathDefault); err != nil {
if _, err = os.Create(cfgPathDefault); err != nil {
return err
}

// open file with read & write
file, err := os.OpenFile(cfgPathDefault, os.O_RDWR, 0644)
file, err := os.OpenFile(cfgPathDefault, os.O_RDWR, 0600)
if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
defer file.Close()
defer fatalOnError("Error closing config file", file.Close())

// read flag values
host := viper.GetString("host")
Expand Down
23 changes: 23 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main_test

import (
"os/exec"

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

var _ = Describe("Main", func() {
Describe("when run with --help", func() {
It("return exit code 0 with help message", func() {
command := exec.Command(pathCLI, "--help")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
Eventually(session.Out).Should(gbytes.Say("Usage:"))
Eventually(session.Err.Contents()).Should(BeEmpty())
})
})
})
6 changes: 6 additions & 0 deletions vendor/github.com/onsi/ginkgo/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/onsi/ginkgo/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a984096

Please sign in to comment.