Skip to content

Commit

Permalink
Merge pull request #3 from pepabo/add-structs
Browse files Browse the repository at this point in the history
Use struct instead of map
  • Loading branch information
linyows committed Apr 10, 2018
2 parents e9147c8 + a1f9763 commit cb67738
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
10 changes: 6 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
)

func TestNew(t *testing.T) {
now := os.Getenv("LOLP_TOKEN")
currentToken := os.Getenv("LOLP_TOKEN")
currentEndpoint := os.Getenv("LOLP_ENDPOINT")
os.Unsetenv("LOLP_TOKEN")
defer os.Setenv("LOLP_TOKEN", now)
defer os.Unsetenv("LOLP_ENDPOINT")
os.Unsetenv("LOLP_ENDPOINT")
defer os.Setenv("LOLP_TOKEN", currentToken)
defer os.Setenv("LOLP_ENDPOINT", currentEndpoint)

c := New()
if c.URL.String() != "https://api.mc.lolipop.jp/" {
Expand All @@ -28,7 +30,7 @@ func TestNew(t *testing.T) {
t.Errorf("Content-Type header expects %s, but got %s", cte, ct)
}
ua := strings.Join(c.DefaultHeader["User-Agent"], "")
uae := "lolp/0.0.1 (+https://github.com/pepabo/golipop; go"
uae := "lolp/" + Version + " (+https://github.com/pepabo/golipop; go"
if !strings.HasPrefix(ua, uae) {
t.Errorf("User-Agent header expects %s, but got %s", uae, ua)
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/lolp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"reflect"
"strings"
"time"

"github.com/hashicorp/logutils"
flags "github.com/jessevdk/go-flags"
Expand Down Expand Up @@ -292,8 +293,16 @@ func (c *CLI) deleteProject() error {
func (c *CLI) showStruct(s interface{}) {
ss := reflect.ValueOf(s).Elem()
typeOfT := ss.Type()
// RFC3339
layout := "2006-01-02T15:04:05Z07:00"
for i := 0; i < ss.NumField(); i++ {
f := ss.Field(i)
fmt.Fprintf(c.outStream, "%-20s %#v\n", typeOfT.Field(i).Name, f.Interface())
v := f.Interface()
isTime := reflect.TypeOf(v) == reflect.TypeOf(time.Now())
if isTime {
fmt.Fprintf(c.outStream, "%-20s %s\n", typeOfT.Field(i).Name, v.(time.Time).Format(layout))
} else {
fmt.Fprintf(c.outStream, "%-20s %#v\n", typeOfT.Field(i).Name, v)
}
}
}
31 changes: 22 additions & 9 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@ import (

// Project struct
type Project struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Kind string `json:"kind,omitempty"`
Domain string `json:"domain,omitempty"`
SubDomain string `json:"subDomain,omitempty"`
CustomDomains []string `json:"customDomains,omitempty"`
Database map[string]string `json:"database,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Kind string `json:"kind,omitempty"`
Domain string `json:"domain,omitempty"`
SubDomain string `json:"subDomain,omitempty"`
CustomDomains []string `json:"customDomains,omitempty"`
Database Database `json:"database,omitempty"`
SSH SSH `json:"ssh,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}

type Database struct {
Host string `json:"host,omitempty"`
User string `json:"user,omitempty"`
Name string `json:"name,omitempty"`
}

type SSH struct {
User string `json:"user,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
}

// ProjectNew struct on create
Expand Down
13 changes: 9 additions & 4 deletions project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,15 @@ func TestProject(t *testing.T) {
Name: "rails-1.lolipop.io",
Domain: "rails-1.lolipop.io",
SubDomain: "rails-1",
Database: map[string]string{
"host": "mysql-1.mc.lolipop.lan",
"name": "7e7aef038f314742c064deb6e6e84714",
"user": "7e7aef038f314742c064deb6e6e84714",
Database: Database{
Host: "mysql-1.mc.lolipop.lan",
Name: "7e7aef038f314742c064deb6e6e84714",
User: "7e7aef038f314742c064deb6e6e84714",
},
SSH: SSH{
User: "sweet-ebino-9052",
Host: "ssh-1.mc.lolipop.jp",
Port: 12345,
},
CustomDomains: []string{},
CreatedAt: tt,
Expand Down
25 changes: 15 additions & 10 deletions testdata/v1/projects/rails-1/GET--ok.response.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
{
"id":"58b22c80-5c64-41ed-ac51-7ca0c695e592",
"name":"rails-1.lolipop.io",
"kind":"rails",
"domain":"rails-1.lolipop.io",
"subDomain":"rails-1",
"customDomains":[],
"database":{
"host":"mysql-1.mc.lolipop.lan",
"user":"7e7aef038f314742c064deb6e6e84714",
"name":"7e7aef038f314742c064deb6e6e84714"
},
"name":"rails-1.lolipop.io",
"kind":"rails",
"domain":"rails-1.lolipop.io",
"subDomain":"rails-1",
"customDomains":[],
"database":{
"host":"mysql-1.mc.lolipop.lan",
"user":"7e7aef038f314742c064deb6e6e84714",
"name":"7e7aef038f314742c064deb6e6e84714"
},
"ssh":{
"user":"sweet-ebino-9052",
"host":"ssh-1.mc.lolipop.jp",
"port":12345
},
"createdAt":"2018-02-13T08:36:06.380Z",
"updatedAt":"2018-02-13T08:36:06.380Z"
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ package lolp
const Name string = "lolp"

// Version for this
const Version string = "0.0.1"
const Version string = "0.0.2"

0 comments on commit cb67738

Please sign in to comment.