Skip to content

Commit

Permalink
use flag to load config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
syronz committed Oct 5, 2019
1 parent 15ffe89 commit 237d5af
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 205 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ session.vim
bin/
routes/
samples/
go_crud
20 changes: 4 additions & 16 deletions basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"local": {
"_TOKEN1_":"Bearer 234235078971",
"_TOKEN2_":"Bearer 234235078971",
"_TOKEN2_":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFwcHVzZXIiLCJleHAiOjE1NzQ2ODU3Njh9.5xS1e9CdSoYWiOqwUbrvRGDdku03ObpF-zP8F20z1Go",
"_URL_":"http://127.0.0.1:8080",
"_NAME_":"Diako Amir"
}
Expand All @@ -17,25 +17,13 @@
}
],

"headers":{
"header":{
"Content-Type": "application/json",
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFwcHVzZXIiLCJleHAiOjE1NzQ2ODU3Njh9.5xS1e9CdSoYWiOqwUbrvRGDdku03ObpF-zP8F20z1Go",
"User-Agent": "go_crud/0.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Host": "127.0.0.1:8080",
"Accept-Encoding": "gzip: deflate",
"Content-Length": "82",
"Connection": "keep-alive",
"cache-control": "no-cache"
},
"Connection": "keep-alive"
}

"payload":[{
"name":"diako",
"age":33
},
{
"mark":300
}]

}
3 changes: 2 additions & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type Engine struct {
EnvArr []string

SelectedDir string
SelectedFile int
SelectedFile string
SelectedEnv string
Content string
Config models.Config
Header models.Header
}
64 changes: 64 additions & 0 deletions engine/sendRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package engine

import (
"bytes"
"encoding/json"
"fmt"
"go_crud/models"
"io/ioutil"
"log"
"net/http"
"strings"
)

func (e *Engine) SendRequest() {
var req models.Request
fullContent := e.TranslatedContent()
fullContentByted := []byte(fullContent)
err := json.Unmarshal(fullContentByted, &req)
if err != nil {
log.Fatal("Error in unmarshal content", err.Error())
}
payloadJSON, err := json.Marshal(req.Payload)
if err != nil {
log.Fatal("Error in marshal content", err.Error())
}
payload := bytes.NewReader(payloadJSON)

if req.Url == "" || req.Method == "" {
fmt.Println("Invalid format for json file")
return
}

url := req.Url

result, _ := http.NewRequest(strings.ToUpper(req.Method), url, payload)

result.Header.Add("Content-Type", e.Header.ContentType)
result.Header.Add("Authorization", req.Authorization)
result.Header.Add("User-Agent", e.Header.UserAgent)
result.Header.Add("Accept", e.Header.Accept)
result.Header.Add("Cache-Control", e.Header.CacheControl)
result.Header.Add("Connection", e.Header.Connection)

res, _ := http.DefaultClient.Do(result)

defer res.Body.Close()
resBody, _ := ioutil.ReadAll(res.Body)

var resInterface interface{}
err = json.Unmarshal(resBody, &resInterface)
if err != nil {
log.Println("Error in unmarshal resBody ", err.Error())
//fmt.Println("Response: ", string(resBody))
}

finalJSON, err := json.MarshalIndent(resInterface, "", "\t")
if err != nil {
log.Println("Error in marshal resInterface ", err.Error())
}

fmt.Printf("Response: %+v\n", res.Status)
fmt.Println(string(finalJSON))

}
5 changes: 3 additions & 2 deletions engine/showPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var NumForList = map[int]string{
}

func (e *Engine) ShowPage(arr []string, msg string) {
fmt.Println(" ")
fmt.Println(msg)
for i, v := range arr {
if i > 35 {
Expand All @@ -26,9 +27,9 @@ func (e *Engine) Show() {
case DIRECTORIES:
e.ShowPage(e.Dirs, "Directories: ")
case FILES:
e.ShowPage(e.Files, "Files: ")
e.ShowPage(e.Files, "Files: "+e.SelectedDir)
case ENVIRONMENTS:
e.ShowPage(e.EnvArr, "Environments: ")
e.ShowPage(e.EnvArr, "Environments: "+e.SelectedDir+"/"+e.SelectedFile)

}
}
12 changes: 12 additions & 0 deletions engine/showPath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package engine

import (
"fmt"
)

func (e *Engine) ShowCurrentPath() {
fmt.Printf("Path: %v/%v\n", e.SelectedDir, e.SelectedFile)
translatedContent := e.TranslatedContent()
fmt.Printf("Content:\n %v\n", translatedContent)

}
11 changes: 11 additions & 0 deletions engine/translatedContent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package engine

import (
"go_crud/helper"
)

func (e *Engine) TranslatedContent() string {
contentInJSON := helper.EnvReplace(e.Content, e.GetEnv())

return contentInJSON
}
File renamed without changes.
9 changes: 9 additions & 0 deletions global/keyMap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package global

// key range for selection 0-9 and A-Z
var SelectionKey = map[int32]int32{
48: 0, 49: 1, 50: 2, 51: 3, 52: 4, 53: 5, 54: 6, 55: 7, 56: 8, 57: 9,
65: 10, 66: 11, 67: 12, 68: 13, 69: 14, 70: 15, 71: 16, 72: 17, 73: 18,
74: 19, 75: 20, 76: 21, 77: 22, 78: 23, 79: 24, 80: 25, 81: 26, 82: 27,
83: 28, 84: 29, 85: 30, 86: 31, 87: 32, 88: 33, 89: 34, 90: 35,
}
5 changes: 1 addition & 4 deletions helper/envReplace.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package helper

import (
"fmt"
"go_crud/models"
"strings"
)

func EnvReplace(str string, env models.Env) string {
for i, v := range env.Dict {
fmt.Printf(".......... %+v ... %+v\n", i, v)
fmt.Println("∫∫∫∫∫∫∫∫∫∫∫∫∫∫∫∫∫∫∫ ", i, v)
for _, v := range env.Dict {
str = strings.Replace(str, v.Key, v.Value, -1)
}

Expand Down
55 changes: 0 additions & 55 deletions helper/testSimple.go

This file was deleted.

11 changes: 9 additions & 2 deletions loaders/loadConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"go_crud/models"
"log"
"os"
//"os/user"
)

// load config.json
func LoadConfig() models.Config {
func LoadConfig(configPath string) models.Config {
// usr, err := user.Current()
//if err != nil {
//log.Fatal(err)
//}

var config models.Config
configFile, err := os.Open("config.json")
//configFile, err := os.Open(usr.HomeDir + "/.go_crud/config.json")
configFile, err := os.Open(configPath)
if err != nil {
log.Fatal("can't open config.json: ", err)
}
Expand Down
Loading

0 comments on commit 237d5af

Please sign in to comment.