Skip to content

Commit

Permalink
ref(client): rewrite client in go.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Anderson committed Jul 17, 2015
1 parent 352575c commit 3b5bb9c
Show file tree
Hide file tree
Showing 50 changed files with 4,181 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ builder/rootfs/usr/bin
cache/image/bin/
client/dist/
client/makeself/
client-go/deis
contrib/azure/azure-user-data
contrib/bumpver/bumpver
deisctl/deisctl
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GO_PACKAGES_REPO_PATH = $(addprefix $(repo_path)/,$(GO_PACKAGES))

COMPONENTS=builder cache controller database logger logspout publisher registry router store swarm
START_ORDER=publisher store logger logspout database cache registry controller builder router
CLIENTS=client deisctl
CLIENTS=client client-go deisctl

all: build run

Expand Down
13 changes: 13 additions & 0 deletions client-go/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2015 Engine Yard, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
40 changes: 40 additions & 0 deletions client-go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include ../includes.mk

# the filepath to this repository, relative to $GOPATH/src
repo_path = github.com/deis/deis/client-go

GO_FILES = $(wildcard *.go)
GO_PACKAGES = parser cmd controller/api controller/client $(wildcard controller/models/*)
GO_PACKAGES_REPO_PATH = $(addprefix $(repo_path)/,$(GO_PACKAGES))

COMPONENT = $(notdir $(repo_path))
IMAGE = $(IMAGE_PREFIX)/$(COMPONENT):$(BUILD_TAG)

build:
CGO_ENABLED=0 godep go build -a -installsuffix cgo -ldflags '-s' -o deis .
$(call check-static-binary,deis)

install:
godep go install -v .

setup-root-gotools:
sudo GOPATH=/tmp/tmpGOPATH go get -u -v golang.org/x/tools/cmd/cover
sudo GOPATH=/tmp/tmpGOPATH go get -u -v golang.org/x/tools/cmd/vet
sudo rm -rf /tmp/tmpGOPATH

setup-gotools:
go get -u github.com/golang/lint/golint
go get -u golang.org/x/tools/cmd/cover
go get -u golang.org/x/tools/cmd/vet

test: test-style test-unit

test-style:
# display output, then check
$(GOFMT) $(GO_PACKAGES) $(GO_FILES)
@$(GOFMT) $(GO_PACKAGES) $(GO_FILES) | read; if [ $$? == 0 ]; then echo "gofmt check failed."; exit 1; fi
$(GOVET) $(repo_path) $(GO_PACKAGES_REPO_PATH)
$(GOLINT) ./...

test-unit:
$(GOTEST) ./...
13 changes: 13 additions & 0 deletions client-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Deis Client

`deis` is a command line utility used to interact with a Deis Cluster.

This is a WIP rewrite of the client in go.

## License

Copyright 2015, Engine Yard, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
215 changes: 215 additions & 0 deletions client-go/cmd/apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package cmd

import (
"fmt"
"net/url"
"os"
"strings"

"github.com/deis/deis/pkg/prettyprint"

"github.com/deis/deis/client-go/controller/client"
"github.com/deis/deis/client-go/controller/models/apps"
"github.com/deis/deis/client-go/controller/models/config"
)

// AppCreate creates an app.
func AppCreate(id string, buildpack string, remote string, noRemote bool) error {
c, err := client.New()

fmt.Print("Creating Application... ")
quit := progress()
app, err := apps.New(c, id)

quit <- true
<-quit

if err != nil {
return err
}

fmt.Printf("done, created %s\n", app.ID)

if buildpack != "" {
configValues := map[string]string{
"BUILDPACK_URL": buildpack,
}
if err = config.Set(c, app.ID, configValues); err != nil {
return err
}
}

if !noRemote {
return c.CreateRemote(remote, app.ID)
}

fmt.Println("remote available at", c.RemoteURL(app.ID))

return nil
}

// AppsList lists apps on the Deis controller.
func AppsList() error {
c, err := client.New()

if err != nil {
return err
}

apps, err := apps.List(c)

if err != nil {
return err
}

fmt.Println("=== Apps")

for _, app := range apps {
fmt.Println(app.ID)
}
return nil
}

// AppInfo prints info about app.
func AppInfo(appID string) error {
c, appID, err := load(appID)

if err != nil {
return err
}

app, err := apps.Get(c, appID)

if err != nil {
return err
}

fmt.Printf("=== %s Application\n", app.ID)
fmt.Println("updated: ", app.Updated)
fmt.Println("uuid: ", app.UUID)
fmt.Println("created: ", app.Created)
fmt.Println("url: ", app.URL)
fmt.Println("owner: ", app.Owner)
fmt.Println("id: ", app.ID)

return nil
}

// AppOpen opens an app in the default webbrowser.
func AppOpen(appID string) error {
c, appID, err := load(appID)

if err != nil {
return err
}

app, err := apps.Get(c, appID)

if err != nil {
return err
}

u, err := url.Parse(app.URL)

if err != nil {
return err
}

u.Scheme = "http"

return client.Webbrowser(u.String())
}

// AppLogs returns the logs from an app.
func AppLogs(appID string, lines int) error {
c, appID, err := load(appID)

if err != nil {
return err
}

logs, err := apps.Logs(c, appID, lines)

if err != nil {
return err
}

for _, log := range strings.Split(strings.Trim(logs, `\n`), `\n`) {
catagory := strings.Split(strings.Split(log, ": ")[0], " ")[1]
colorVars := map[string]string{
"Color": chooseColor(catagory),
"Log": log,
}
fmt.Println(prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars))
}

return nil
}

// AppRun runs a one time command in the app.
func AppRun(appID, command string) error {
c, appID, err := load(appID)

if err != nil {
return err
}

fmt.Printf("Running '%s'...\n", command)

out, err := apps.Run(c, appID, command)

if err != nil {
return err
}

fmt.Print(out.Output)
os.Exit(out.ReturnCode)
return nil
}

// AppDestroy destroys an app.
func AppDestroy(appID, confirm string) error {
gitSession := false

c, err := client.New()

if err != nil {
return err
}

if appID == "" {
appID, err = c.DetectApp()

if err != nil {
return err
}

gitSession = true
}

if confirm == "" {
fmt.Printf(` ! WARNING: Potentially Destructive Action
! This command will destroy the application: %s
! To proceed, type "%s" or re-run this command with --confirm=%s
>`, appID, appID, appID)

fmt.Scanln(&confirm)
}

if confirm != appID {
return fmt.Errorf("App %s does not match confirm %s, aborting.", appID, confirm)
}

fmt.Printf("Destroying %s...", appID)

if err = apps.Delete(c, appID); err != nil {
return err
}

if gitSession {
return c.DeleteRemote(appID)
}

return nil
}
Loading

0 comments on commit 3b5bb9c

Please sign in to comment.