Skip to content

Commit

Permalink
Merge pull request #1 from circleci/init
Browse files Browse the repository at this point in the history
CIRCLE-11398: User can install CLI that will talk to registry
  • Loading branch information
Zachary Scott authored Jun 1, 2018
2 parents b27fe3f + 86b4e40 commit 5788fd6
Show file tree
Hide file tree
Showing 488 changed files with 230,893 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
TODO
132 changes: 132 additions & 0 deletions Gopkg.lock

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

42 changes: 42 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/machinebox/graphql"
branch = "master"

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"

[[constraint]]
name = "github.com/spf13/viper"
version = "1.0.2"

[prune]
go-tests = true
unused-packages = true
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
VERSION=0.1
DATE = $(shell date "+%FT%T%z")
SHA=$(shell git rev-parse --short HEAD)

GOFILES = $(shell find . -name '*.go' -not -path './vendor/*')

CLIPATH=github.com/circleci/cli

EXECUTABLE=cli
BUILD_DIR=build

.PHONY: build/linux
$(BUILD_DIR)/%/amd64/$(EXECUTABLE): $(GOFILES)
GOOS=$* GOARCH=amd64 CGO_ENABLED=0 go build $(LDFLAGS) -o $(BUILD_DIR)/$*/amd64/$(EXECUTABLE) .

.PHONY: build
build: $(BUILD_DIR)/darwin/amd64/$(EXECUTABLE) $(BUILD_DIR)/linux/amd64/$(EXECUTABLE)

.PHONY: clean
clean:
go clean
rm -rf $(BUILD_DIR)

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

.PHONY: cover
coverage:
go test -coverprofile=coverage.txt -covermode=count ./...
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# circleci/cli

Summary

## Requirements

* Go 1.9+
* Make
* ...

It's written in Go. If you are new to Go, we recommend the following resources:

* [A Tour of Go](https://tour.golang.org/welcome/1)
* [The Go documentation](https://golang.org/doc/)

## Development Workflow

### 1. Go Dependencies

Install `dep`:
https://github.com/golang/dep

On MacOS:

```
$ brew install dep
$ brew upgrade dep
```

On Linux, etc:

```
$ curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
```

Ensure dependencies are installed:

```
$ dep ensure
```

### 2. Build it

```
$ make
```

### 3. Run Diagnostic check

```
$ ./build/target/linux/amd64/cli diagnostic
Please enter your CircleCI API token:
OK.
Your configuration has been created in `/home/zzak/.circleci/cli.yml`.
It can edited manually for advanced settings.
---
CircleCI CLI Diagnostics
---
Config found: `/home/zzak/.circleci/cli.yml`
Host is: https://circleci.com
OK, got a token.
```

## Known Issues

* ...

## Doc

You can view godoc of cli in your browser.

1. Run `godoc -http=:6060`
2. Access http://localhost:6060/pkg/github.com/circleci/cli/

## Editor support

Go has great tooling for editors.

```
$ go get golang.org/x/tools/cmd/goimports
```

More [here](https://blog.golang.org/go-fmt-your-code).
34 changes: 34 additions & 0 deletions cmd/diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"

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

var diagnosticCmd = &cobra.Command{
Use: "diagnostic",
Short: "Check the status of your CircleCI CLI.",
Run: diagnostic,
}

func diagnostic(cmd *cobra.Command, args []string) {
host := viper.GetString("host")
token := viper.GetString("token")

fmt.Printf("\n---\nCircleCI CLI Diagnostics\n---\n\n")
fmt.Printf("Config found: `%v`\n", viper.ConfigFileUsed())

if host == "host" || host == "" {
fmt.Println("Please set a host!")
} else {
fmt.Printf("Host is: %s\n", host)
}

if token == "token" || token == "" {
fmt.Println("Please set a token!")
} else {
fmt.Println("OK, got a token.")
}
}
64 changes: 64 additions & 0 deletions cmd/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"log"

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

var queryCmd = &cobra.Command{
Use: "query",
Short: "Query the CircleCI GraphQL API.",
Run: query,
}

func query(cmd *cobra.Command, args []string) {
host := viper.GetString("host")
token := viper.GetString("token")
client := graphql.NewClient(host + "/graphql")

query := `
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
}
}`

req := graphql.NewRequest(query)
req.Header.Set("Authorization", token)

ctx := context.Background()
var resp map[string]interface{}

fmt.Print("Querying ", host, " with:\n\n", query, "\n\n")
if err := client.Run(ctx, req, &resp); err != nil {
log.Fatal(err)
}

b, _ := json.MarshalIndent(resp, "", " ")
fmt.Print("Result: \n\n")
fmt.Println(string(b))
}
Loading

0 comments on commit 5788fd6

Please sign in to comment.