Skip to content

Commit

Permalink
add: clean up directory structure with package control in golang
Browse files Browse the repository at this point in the history
  • Loading branch information
k1m0ch1 committed Dec 23, 2020
1 parent b80107e commit 65f8b56
Show file tree
Hide file tree
Showing 22 changed files with 517 additions and 473 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.compile
.dev
.coral
coral

sessions
.sessions
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/eFishery/nemo
module github.com/eFishery/NeMo

go 1.15

Expand Down
31 changes: 12 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
cron "github.com/robfig/cron/v3"
godotenv "github.com/joho/godotenv"
// "github.com/davecgh/go-spew/spew"

"github.com/eFishery/NeMo/utils"
)

type waHandler struct {
Expand All @@ -25,20 +27,16 @@ type waHandler struct {

}

var Schedules []Schedule

var Settings *Setting

var BuildCommands []BuildCommand

var BuildGreetings []BuildGreeting

func init() {
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
}
}

var Settings *utils.Setting
var BuildGreetings []utils.BuildGreeting
var BuildCommands []utils.BuildCommand

func main() {

if len(os.Args) < 2 {
Expand All @@ -47,18 +45,13 @@ func main() {

sender := os.Args[1]

Settings = LoadSetting()

builder()

readGreetingsFile()
Settings = utils.LoadSetting()
Settings.Builder()
BuildGreetings = utils.ReadGreetingsFile()

jadwal := cron.New()

cmds := readBuildCommandsFiles()
if !cmds {
return
}
BuildCommands = utils.ReadBuildCommandsFiles()

wac, err := whatsapp.NewConn(5 * time.Second)
// wac.SetClientVersion(0, 4, 2080)
Expand All @@ -80,8 +73,8 @@ func main() {
log.Fatalf("error pinging in: %v\n", err)
}

schedule := readScheduleFiles()
if !schedule {
isLoaded, Schedules := utils.ReadScheduleFiles()
if !isLoaded {
log.Println("Can't read Schedule Files")
return
}
Expand Down
6 changes: 4 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io/ioutil"
"os"
"strings"

"github.com/eFishery/NeMo/utils"
)

var (
Expand All @@ -27,13 +29,13 @@ var (
// if no URL is found in pesan, pesan is returned as is
// if URL is found, try POST request to url
// currently only supports JSON response with message key
func nemoParser(pesan string, Sessions Session) (string, error) {
func nemoParser(pesan string, Sessions utils.Session) (string, error) {
urlCount := strings.Count(pesan, "{{")
if urlCount == 0 {
return pesan, nil
}
for i := 0; i < urlCount; i++ {
url := between(pesan, "{{", "}}")
url := utils.Between(pesan, "{{", "}}")
r, err := req.Post(url, req.BodyJSON(Sessions))
if err != nil {
pesan = strings.Replace(pesan, fmt.Sprintf("{{%s}}", url), errReqErr(url), -1)
Expand Down
22 changes: 12 additions & 10 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"log"
"fmt"
"time"

"github.com/eFishery/NeMo/utils"
)

func newSession(phone_number string, current_process string, timeout int) Session{
func newSession(phone_number string, current_process string, timeout int) utils.Session{

savedSession := Session {
savedSession := utils.Session {
PhoneNumber: phone_number,
CurrentProcess: current_process,
CurrentQuestionSlug: 0,
Expand All @@ -21,19 +23,19 @@ func newSession(phone_number string, current_process string, timeout int) Sessio
}

file, _ := json.MarshalIndent(savedSession, "", " ")
_ = ioutil.WriteFile(fileSession(phone_number), file, 0644)
_ = ioutil.WriteFile(utils.FileSession(phone_number), file, 0644)

return savedSession
}

func loadSession(phone_number string) (Session, error) {
var s Session
file_session, err := ioutil.ReadFile(fileSession(phone_number))
func loadSession(phone_number string) (utils.Session, error) {
var s utils.Session
file_session, err := ioutil.ReadFile(utils.FileSession(phone_number))

if err != nil {
log.Println("Create a new file")
file, _ := json.MarshalIndent(Session{}, "", " ")
_ = ioutil.WriteFile(fileSession(phone_number), file, 0644)
file, _ := json.MarshalIndent(utils.Session{}, "", " ")
_ = ioutil.WriteFile(utils.FileSession(phone_number), file, 0644)
return s, fmt.Errorf("Session hasn't been created")
}

Expand All @@ -45,7 +47,7 @@ func loadSession(phone_number string) (Session, error) {
return s, nil
}

func saveSession(s Session, phone_number string) {
func saveSession(s utils.Session, phone_number string) {
file, _ := json.MarshalIndent(s, "", " ")
_ = ioutil.WriteFile(fileSession(phone_number), file, 0644)
_ = ioutil.WriteFile(utils.FileSession(phone_number), file, 0644)
}
4 changes: 3 additions & 1 deletion builder_test.go → test/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main
package test

import (
"io/ioutil"
"testing"
"os"

"github.com/eFishery/NeMo/utils"
)

func TestBuildCommandsAutoCreated( t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions parser_test.go → test/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main
package test

import (
"fmt"
"os"
"testing"

"github.com/eFishery/NeMo/utils"
)

const (
Expand Down Expand Up @@ -38,7 +40,7 @@ func TestNemoParser(t *testing.T) {
name: "three_url_from_file",
pesan: fmt.Sprintf("hello {{%s}} hello {{%s}} hello {{%s}}", testURL1, testURL2, testURL3),
res: "hello 1 hello 2 hello 3",
config: "config/keys-example.json",
config: "test/config/keys-example.json",
},
}
sess := Session{
Expand Down
12 changes: 6 additions & 6 deletions builder.go → utils/builder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"io/ioutil"
Expand All @@ -17,8 +17,8 @@ func matchYAMLFile(filename string) bool {
}


func builder() {
linter := builder_linter_all()
func (Settings *Setting) Builder() {
linter := Settings.builder_linter()
if len(linter) > 0 {
for i:= range(linter){
log.Println(linter[i])
Expand All @@ -39,7 +39,7 @@ func builder() {
log.Println("Build file " + file.Name())
processName := file.Name()
var coral Coral
coral.getCoral(processName)
coral.GetCoral(processName)

var commandCompile = BuildCommand {
Prefix: coral.Commands.Prefix,
Expand Down Expand Up @@ -138,14 +138,14 @@ func builder() {
}
}

func builder_linter_all() []string {
func (Settings *Setting) builder_linter() []string {
var result []string
files,_ := ioutil.ReadDir(Settings.CoralDir)
for _, file := range files {
if matchYAMLFile(file.Name()) {
var coral Coral

coral.getCoral(file.Name())
coral.GetCoral(file.Name())

if !coral.valAuthor() {
result = append(result, file.Name() + ": Author must complete")
Expand Down
2 changes: 1 addition & 1 deletion coral.go → utils/coral.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

func (coral *Coral) valCommands() bool{
if coral.Commands.Prefix == ""{ return false }
Expand Down
4 changes: 2 additions & 2 deletions struct.go → utils/struct.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils


type Setting struct {
Expand Down Expand Up @@ -108,7 +108,7 @@ type Session struct {
Finished string `json:"finished"`
}

type discord struct {
type Discord struct {
Content string `json:"content"`
}

Expand Down
44 changes: 27 additions & 17 deletions util.go → utils/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"path/filepath"
Expand Down Expand Up @@ -53,7 +53,7 @@ func LoadSetting() *Setting {
}


func between(value string, a string, b string) string {
func Between(value string, a string, b string) string {
// Get substring between two strings.
posFirst := strings.Index(value, a)
if posFirst == -1 {
Expand All @@ -70,7 +70,7 @@ func between(value string, a string, b string) string {
return value[posFirstAdjusted:posLast]
}

func after(value string, a string) string {
func After(value string, a string) string {
// Get substring after a string.
pos := strings.LastIndex(value, a)
if pos == -1 {
Expand All @@ -83,7 +83,7 @@ func after(value string, a string) string {
return value[adjustedPos:len(value)]
}

func AddFileToS3(fileDir string) string {
func (Settings *Setting) AddFileToS3(fileDir string) string {

creds := credentials.NewStaticCredentials(Settings.AwsAccessKeyId, Settings.AwsSecretAccessKey, "")
_, err := creds.Get()
Expand Down Expand Up @@ -128,8 +128,9 @@ func AddFileToS3(fileDir string) string {
return Settings.AwsS3EndpointUrl + Settings.AwsS3Dir + fileName
}

func (c *Coral) getCoral(filename string) *Coral {
yamlFile, err := ioutil.ReadFile( Settings.CoralDir + "/" + filename)
func (c *Coral) GetCoral(filename string) *Coral {
Settings := LoadSetting()
yamlFile, err := ioutil.ReadFile(Settings.CoralDir + "/" + filename)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
Expand All @@ -141,22 +142,28 @@ func (c *Coral) getCoral(filename string) *Coral {
return c
}

func readScheduleFiles() bool{
func ReadScheduleFiles() (bool, []Schedule){
var Schedules []Schedule

Settings := LoadSetting()
content, err := ioutil.ReadFile(Settings.BuildDir + "/schedules.json")
if err != nil {
log.Fatal(err)
}

jsonErr := json.Unmarshal(content, &Schedules)
if jsonErr != nil {
log.Fatal(jsonErr)
}

return true
return true, Schedules

}

func readBuildCommandsFiles() bool{
func ReadBuildCommandsFiles() []BuildCommand {
var BuildCommands []BuildCommand

Settings := LoadSetting()
content, err := ioutil.ReadFile(Settings.BuildDir + "/commands.json")
if err != nil {
os.OpenFile(Settings.BuildDir + "/commands.json", os.O_RDONLY|os.O_CREATE, 0755)
Expand All @@ -168,11 +175,14 @@ func readBuildCommandsFiles() bool{
log.Fatal(jsonErr)
}

return true
return BuildCommands

}

func readGreetingsFile() bool{
func ReadGreetingsFile() []BuildGreeting{
var BuildGreetings []BuildGreeting

Settings := LoadSetting()
content, err := ioutil.ReadFile(Settings.BuildDir + "/greetings.json")
if err != nil {
os.OpenFile(Settings.BuildDir + "/greetings.json", os.O_RDONLY|os.O_CREATE, 0755)
Expand All @@ -182,14 +192,14 @@ func readGreetingsFile() bool{
jsonErr := json.Unmarshal(content, &BuildGreetings)
if jsonErr != nil {
log.Fatal(jsonErr)
}

return true

}

return BuildGreetings
}


func fileSession(phone_number string) string {
func FileSession(phone_number string) string {
Settings := LoadSetting()
return Settings.BuildDir + "/sessions/" + phone_number + ".session"
}

Expand Down
Loading

0 comments on commit 65f8b56

Please sign in to comment.