Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack listing options #331

Merged
merged 3 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions api/rpc/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,16 @@ func (s *Server) List(ctx context.Context, in *ListRequest) (*ListReply, error)
if err != nil {
return nil, err
}
listInfo := make([]*StackInfo, len(idList), len(idList))
listInfo := []*StackInfo{}
for i, ID := range idList {
obj, _ := ID.(*StackID)
listInfo[i] = s.getStackInfo(ctx, obj.Id)
if in.Limit == 0 || len(idList)-i <= int(in.Limit) {
obj, _ := ID.(*StackID)
info := s.getStackInfo(ctx, obj.Id)
fmt.Println("info", info)
if in.All || info.State == "Running" {
listInfo = append(listInfo, s.getStackInfo(ctx, obj.Id))
}
}
}
reply := ListReply{
List: listInfo,
Expand Down
111 changes: 57 additions & 54 deletions api/rpc/stack/stack.pb.go

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

4 changes: 3 additions & 1 deletion api/rpc/stack/stack.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ message StackReply {
}

// struct for list request function
message ListRequest {
message ListRequest {
bool all = 1;
int64 limit = 2;
}

// struct for list reply function
Expand Down
107 changes: 107 additions & 0 deletions cmd/amp/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cli_test

import (
"github.com/appcelerator/amp/api/server"
"gopkg.in/yaml.v2"
"io/ioutil"
"os/exec"
"path"
"strings"
"testing"
"time"
)

type TestSpec struct {
fileName string
contents []byte
}

type commandSpec struct {
Cmd string `yaml:"cmd"`
Args []string `yaml:"args"`
Options []string `yaml:"options"`
}

var (
testDir = "./test_samples"
)

func TestMain(t *testing.T) {
_, conn := server.StartTestServer()
t.Log(conn)
}

func TestCmds(t *testing.T) {
tests := loadFiles(t)
for _, test := range tests {
parse(t, test)
}
}

func loadFiles(t *testing.T) []*TestSpec {
tests := []*TestSpec{}
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Error(err)
return nil
}
for _, f := range files {
name := f.Name()
t.Log("Loading file:", name)
contents, err := ioutil.ReadFile(path.Join(testDir, name))
if err != nil {
t.Errorf("unable to load test sample: %s. Error: %v", name, err)
}
testSpec := &TestSpec{
fileName: name,
contents: contents,
}
tests = append(tests, testSpec)
}
return tests
}

func parse(t *testing.T, test *TestSpec) {
commandMap, err := parseCommandMap(test.contents)
if err != nil {
t.Error(err)
return
}
for _, spec := range commandMap {
cmdSplit := strings.Fields(spec.Cmd)
optionsSplit := []string{}
for _, val := range spec.Options {
optionsSplit = append(optionsSplit, strings.Fields(val)...)
}
commandFinal := append(cmdSplit, spec.Args...)
commandFinal = append(commandFinal, optionsSplit...)
t.Log(commandFinal, "Command passed.")
runCmd(t, commandFinal)
}
}

func runCmd(t *testing.T, cmdString []string) {
t.Log(cmdString, "Running...")
for i := 0; i < 10; i++ {
t.Log(cmdString, " Iteration:", i+1)
cmd := exec.Command(cmdString[0], cmdString[1:]...)
result, err := cmd.CombinedOutput()
if err != nil && i >= 9 {
t.Log(cmdString, " Error:", err)
t.Log(cmdString, " Command has failed, exiting.")
t.Fail()
} else if err != nil {
t.Log(cmdString, " Error:", err)
t.Log(cmdString, " Command failed, retrying.")
time.Sleep(1 * time.Second)
} else {
t.Log(cmdString, " Command result:\n", string(result))
break
}
}
}

func parseCommandMap(b []byte) (out map[string]commandSpec, err error) {
err = yaml.Unmarshal(b, &out)
return
}
7 changes: 7 additions & 0 deletions cmd/amp/cli/test_samples/01-create-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
create-service:
cmd: amp service create
args:
- appcelerator/pinger
options:
- --name pinger
- -p www:90:3000
4 changes: 4 additions & 0 deletions cmd/amp/cli/test_samples/02-list-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
list-service:
cmd: docker service ls
args:
options:
5 changes: 5 additions & 0 deletions cmd/amp/cli/test_samples/03-curl-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl-service:
cmd: curl
args:
- localhost:90/ping
options:
9 changes: 9 additions & 0 deletions cmd/amp/cli/test_samples/04-remove-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
remove-service:
cmd: amp service rm
args:
- pinger
options:
list-service:
cmd: docker service ls
args:
options:
20 changes: 17 additions & 3 deletions cmd/amp/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ var (
}
},
}
listQuiet *bool
listAll *bool
listLast *int64
listLatest *bool
)

func init() {
RootCmd.AddCommand(StackCmd)
flags := upCmd.Flags()
flags.StringVarP(&stackfile, "file", "f", stackfile, "The name of the stackfile")
rmCmd.Flags().BoolP("force", "f", false, "Remove the stack whatever condition")
listCmd.Flags().BoolP("quiet", "q", false, "return only stack id to be use with grep")
listQuiet = listCmd.Flags().BoolP("quiet", "q", false, "Only display numeric IDs")
listAll = listCmd.Flags().BoolP("all", "a", false, "Show all stacks (default shows just running)")
listLast = listCmd.Flags().Int64P("last", "n", 0, "Show n last created stacks (includes all states)")
listLatest = listCmd.Flags().BoolP("latest", "l", false, "Show the latest created stack (includes all states)")
StackCmd.AddCommand(upCmd)
StackCmd.AddCommand(startCmd)
StackCmd.AddCommand(stopCmd)
Expand Down Expand Up @@ -204,14 +211,21 @@ func remove(amp *client.AMP, cmd *cobra.Command, args []string) error {
}

func list(amp *client.AMP, cmd *cobra.Command, args []string) error {
request := &stack.ListRequest{}
var limit = *listLast
if *listLatest {
limit = 1
}
request := &stack.ListRequest{
All: *listAll,
Limit: limit,
}
client := stack.NewStackServiceClient(amp.Conn)
reply, err := client.List(context.Background(), request)
if err != nil {
return err
}
//Manage -q
if cmd.Flag("quiet").Value.String() == "true" {
if *listQuiet {
for _, info := range reply.List {
fmt.Println(info.Id)
}
Expand Down