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

Fixed offenses goreport #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 21 additions & 22 deletions src/manageiq-exchange/api/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -10,10 +12,8 @@ import (
user "manageiq-exchange/models/user"
"net"
"net/http"
"time"
"bufio"
"strconv"
"errors"
"time"
)

var netTransport = &http.Transport{
Expand All @@ -28,25 +28,25 @@ var netClient = &http.Client{
Transport: netTransport,
}

type Api struct {
type API struct {
Server string
Port int
Client *http.Client
Data DataApi
Data DataAPI
}

type DataApi struct {
type DataAPI struct {
Data interface{} `json:"data"`
Meta meta.Metadata `json:"meta"`
}

func (a *Api) Init(server string, port int) {
func (a *API) Init(server string, port int) {
a.Server = server
a.Port = port
a.Client = netClient
}

func (a *Api) CheckConnectionServer() bool {
func (a *API) CheckConnectionServer() bool {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", a.Server, strconv.Itoa(a.Port)))
if err != nil {
fmt.Printf("Fatal error: %s\n", err.Error())
Expand All @@ -62,22 +62,22 @@ func (a *Api) CheckConnectionServer() bool {
return true
}

func (a *Api) URL() string {
func (a *API) URL() string {
url := fmt.Sprintf("http://%s", a.Server)
if a.Port > 0 {
url += fmt.Sprintf(":%d", a.Port)
}
return url
}

func (a *Api) GetInfo() info.Info {
func (a *API) GetInfo() info.Info {
a.Request("GET", "", nil)
var info info.Info
info.Init(a.Data.Data.(map[string]interface{}))
return info
}

func (a *Api) GetUsers(expand bool) user.UserCollection {
func (a *API) GetUsers(expand bool) user.UserCollection {
var path string
if path = "/v1/users"; expand {
path = "/v1/users?expand=resources"
Expand All @@ -91,27 +91,26 @@ func (a *Api) GetUsers(expand bool) user.UserCollection {
return users
}

func (a *Api) Request(method string, path string, data io.Reader) error {
func (a *API) Request(method string, path string, data io.Reader) error {
req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", a.URL(), path), data)
if err != nil {
return err
}
resp, err := a.Client.Do(req)
defer resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(strconv.Itoa(resp.StatusCode))
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
jsonErr := json.Unmarshal(body, &a.Data)
if jsonErr != nil {
return jsonErr
}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
jsonErr := json.Unmarshal(body, &a.Data)
if jsonErr != nil {
return jsonErr
}
return nil
}
44 changes: 22 additions & 22 deletions src/manageiq-exchange/api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package api

import (
"fmt"
"manageiq-exchange/models/info"
meta "manageiq-exchange/models/metadata"
user "manageiq-exchange/models/user"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"testing"
"fmt"
meta "manageiq-exchange/models/metadata"
user "manageiq-exchange/models/user"
"manageiq-exchange/models/info"
)

var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux

// client is the client being tested.
client *Api
client *API

// urlTest is the url test server
urlTest *url.URL
Expand All @@ -38,7 +38,7 @@ func setup() {
// http client configured to use test server
urlTest, _ = url.Parse(server.URL + "/")
i, _ := strconv.Atoi(urlTest.Port())
client = &Api{}
client = &API{}
client.Init(urlTest.Hostname(), i)
}

Expand All @@ -48,7 +48,7 @@ func teardown() {
}

func TestApi_Init(t *testing.T) {
var server Api
var server API
inputServer := "localhost"
inputPort := 3000
wantServer := "localhost"
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestApi_URL(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.inputServer, func(t *testing.T) {
var server Api
var server API
server.Init(tt.inputServer, tt.inputPort)
gotURL := server.URL()
if !reflect.DeepEqual(gotURL, tt.wantURL) {
Expand Down Expand Up @@ -122,15 +122,15 @@ func TestApi_Request(t *testing.T) {
"A": "a",
}
wantMeta := meta.Metadata{
CurrentPage:0,
TotalPages:0,
TotalCount:0,
CurrentPage: 0,
TotalPages: 0,
TotalCount: 0,
}
if !reflect.DeepEqual(client.Data.Data, wantData){
if !reflect.DeepEqual(client.Data.Data, wantData) {
t.Errorf("client.Data.Data returned %+v want %+v", client.Data.Data, wantData)
}

if !reflect.DeepEqual(client.Data.Meta, wantMeta){
if !reflect.DeepEqual(client.Data.Meta, wantMeta) {
t.Errorf("client.Data.Meta returned %+v want %+v", client.Data.Meta, wantMeta)
}
}
Expand Down Expand Up @@ -168,15 +168,15 @@ func TestApi_GetInfo(t *testing.T) {
Version: "1.0",
Providers: map[string]info.Provider{
"github.com": info.Provider{
ApplicationId: "abc",
ApplicationID: "abc",
Server: "github.com",
Version: "v3",
},
},
}

if !reflect.DeepEqual(gotInfo, wantInfo){
t.Errorf("Api.GetInfo() returned %+v want %+v",gotInfo , wantInfo)
if !reflect.DeepEqual(gotInfo, wantInfo) {
t.Errorf("Api.GetInfo() returned %+v want %+v", gotInfo, wantInfo)
}
}

Expand All @@ -195,14 +195,14 @@ func TestApi_GetUsers_withoutExpandResources(t *testing.T) {
wantUser := user.User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
wantUsers := user.UserCollection{}
wantUsers.Users = append(wantUsers.Users, wantUser)
wantUsers.Total = len(wantUsers.Users)

if !reflect.DeepEqual(gotUsers, wantUsers){
t.Errorf("Api.GetUsers(false) returned %+v want %+v",gotUsers , wantUsers)
if !reflect.DeepEqual(gotUsers, wantUsers) {
t.Errorf("Api.GetUsers(false) returned %+v want %+v", gotUsers, wantUsers)
}
}

Expand All @@ -221,13 +221,13 @@ func TestApi_GetUsers_withExpandResources(t *testing.T) {
wantUser := user.User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
wantUsers := user.UserCollection{}
wantUsers.Users = append(wantUsers.Users, wantUser)
wantUsers.Total = len(wantUsers.Users)

if !reflect.DeepEqual(gotUsers, wantUsers){
t.Errorf("Api.GetUsers(true) returned %+v want %+v",gotUsers , wantUsers)
if !reflect.DeepEqual(gotUsers, wantUsers) {
t.Errorf("Api.GetUsers(true) returned %+v want %+v", gotUsers, wantUsers)
}
}
16 changes: 8 additions & 8 deletions src/manageiq-exchange/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ func Menu() {
if err != nil {
fmt.Print(err)
}
var miq_exchange api.Api
miq_exchange.Init(server, port)
var miqExchange api.API
miqExchange.Init(server, port)

statusConnection := miq_exchange.CheckConnectionServer()
statusConnection := miqExchange.CheckConnectionServer()

if version && statusConnection{
info := miq_exchange.GetInfo()
if version && statusConnection {
info := miqExchange.GetInfo()
fmt.Printf(info.Print())
}

if users && statusConnection{
users := miq_exchange.GetUsers(expand)
fmt.Printf(users.Print(miq_exchange.Data.Meta.TotalCount))
if users && statusConnection {
users := miqExchange.GetUsers(expand)
fmt.Printf(users.Print(miqExchange.Data.Meta.TotalCount))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/manageiq-exchange/models/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Info struct {
type Provider struct {
Type string `json:"type"`
Enabled bool `json:"enabled"`
ApplicationId string `json:"id_application"`
ApplicationID string `json:"id_application"`
Server string `json:"server"`
Version string `json:"version"`
Verify bool `json:"verify"`
Expand All @@ -36,7 +36,7 @@ func (a *Info) Print() string {
for k, v := range a.Providers {
result += fmt.Sprintf(" %s: \n", k)
result += fmt.Sprintf(" %s: %s\n", "Server", v.Server)
result += fmt.Sprintf(" %s: %s\n", "ApplicationId", v.ApplicationId)
result += fmt.Sprintf(" %s: %s\n", "ApplicationId", v.ApplicationID)
result += fmt.Sprintf(" %s: %s\n", "Version", v.Version)
}
return result
Expand Down
4 changes: 2 additions & 2 deletions src/manageiq-exchange/models/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestInit(t *testing.T) {
Version: "1.0",
Providers: map[string]Provider{
"github.com": Provider{
ApplicationId: "abc",
ApplicationID: "abc",
Server: "github.com",
Version: "v3",
},
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestPrint(t *testing.T) {
Version: "1.0",
Providers: map[string]Provider{
"github.com": Provider{
ApplicationId: "abc",
ApplicationID: "abc",
Server: "github.com",
Version: "v3",
},
Expand Down
8 changes: 4 additions & 4 deletions src/manageiq-exchange/models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type User struct {
GithubId int `json:"github_id"`
GithubID int `json:"github_id"`
Login string `json:"login"`
Url_profile string `json:"url_profile"`
URLProfile string `json:"url_profile"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Company string `json:"company"`
Expand Down Expand Up @@ -40,8 +40,8 @@ func (u *User) Init(data map[string]interface{}) {

func (u *User) Print() string {
var result string
result = fmt.Sprintf("%s: %s (%d)\n\n", utils.PrintColor("User", "Red"), u.Login, u.GithubId)
result += utils.PrintValues(u, " ", []string{"Login", "GithubId"})
result = fmt.Sprintf("%s: %s (%d)\n\n", utils.PrintColor("User", "Red"), u.Login, u.GithubID)
result += utils.PrintValues(u, " ", []string{"Login", "GithubID"})
return result
}

Expand Down
8 changes: 4 additions & 4 deletions src/manageiq-exchange/models/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestUserInit(t *testing.T) {
want := User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
var data = map[string]interface{}{
"login": "aljesusg",
Expand All @@ -33,7 +33,7 @@ func TestUserPrint(t *testing.T) {
user := User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
if !reflect.DeepEqual(user.Print(), want) {
t.Errorf("User Print returned -%+v-, want -%+v-", user.Print(), want)
Expand All @@ -44,7 +44,7 @@ func TestUserCollectionInit(t *testing.T) {
user := User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
users := UserCollection{}
users.Users = append(users.Users, user)
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestUserCollectionPrint(t *testing.T) {
user := User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
userCollection := UserCollection{}
userCollection.Users = append(userCollection.Users, user)
Expand Down
4 changes: 3 additions & 1 deletion src/manageiq-exchange/models/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"reflect"
)

const NC string = "\033[0m" // No Color
// NC is No Color
const NC string = "\033[0m"

// COLOR is color to print message with color
var COLOR = map[string]string{
"Black": "0;30",
"Dark Gray": "1;30",
Expand Down
Loading