Skip to content

Commit

Permalink
Move core utilities and services
Browse files Browse the repository at this point in the history
  • Loading branch information
pratishshr committed Oct 23, 2019
1 parent 9274bb1 commit bc4fec1
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 5 deletions.
31 changes: 31 additions & 0 deletions core/services/platforms/aws/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
)

func failOnError(err error, msg string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err))
}
}

// GetSession returns Session for AWS.
func GetSession(profile string, region string) *session.Session {
sess, err := session.NewSessionWithOptions(session.Options{
Profile: profile,
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{
Region: aws.String(region),
},
})

if err != nil {
failOnError(err, "Couldnot connect to AWS.")
}

return sess
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func handle(err error) {
if err != nil {
exit.Error(err)
exit.Error(err, "")
}
}

Expand Down
21 changes: 21 additions & 0 deletions core/services/slack/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package slack

import (
"fmt"
"strings"

"github.com/leapfrogtechnology/shift/deployment/utils/http"
)

// Notify sends data to slack webhook url
func Notify(url string, text string, color string) {

message := strings.Replace(text, "\"", "'", -1)

content := fmt.Sprintf(`{"attachments": [{"text": %q, "color": "%s"}]}`, message, color)

http.Client.R().
SetHeader("Content-Type", "application/json").
SetBody([]byte(content)).
Post(url)
}
14 changes: 14 additions & 0 deletions core/utils/escape/escape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package escape

import (
"regexp"
)

const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"

var re = regexp.MustCompile(ansi)

// Strip returns the same string by removeing ansi char.
func Strip(str string) string {
return re.ReplaceAllString(str, "")
}
19 changes: 19 additions & 0 deletions core/utils/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package file

import (
"mime"
"strings"
)

// GetFileContentType returns the content type of the file.
func GetFileContentType(filename string) string {
split := strings.Split(filename, ".")

if len(split) == 0 {
return "binary/octet-stream"
}

ext := split[len(split)-1]

return mime.TypeByExtension("." + ext)
}
15 changes: 15 additions & 0 deletions core/utils/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ func LogOutput(msg string) {
func LogInfo(msg string) {
log.Println(aurora.Blue(msg))
}

// Success logs msg with green color.
func Success(msg string) {
log.Println(aurora.Green(msg))
}

// Info logs msg with blue color.
func Info(msg string) {
log.Println(aurora.Cyan(msg))
}

// Error logs msg with Red color.
func Error(err error, msg string) {
log.Printf("%s: %s", aurora.Red(msg), aurora.Red(err))
}
34 changes: 34 additions & 0 deletions core/utils/shell/shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package shell

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"

"github.com/leapfrogtechnology/shift/deployment/utils/escape"
)

// Execute runs the process with the supplied environment.
func Execute(command string) error {
cmd := exec.Command("bash", "-c", command)

var out bytes.Buffer
var stderr bytes.Buffer

cmd.Stderr = &stderr
cmd.Stdout = &out

cmd.Stdout = os.Stdout

err := cmd.Run()
if err != nil {
fmt.Println(stderr.String())

errorMessage := escape.Strip(err.Error() + " :- " + stderr.String())

return errors.New(errorMessage)
}
return nil
}
8 changes: 4 additions & 4 deletions core/utils/system/exit/exit.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package exit

import (
"fmt"
"os"

"github.com/leapfrogtechnology/shift/core/utils/logger"
)

// Error exits the application by showing the given message.
func Error(err interface{}) {
fmt.Println("Error")
fmt.Println(err)
func Error(err error, msg string) {
logger.Error(err, msg)

os.Exit(1)
}

0 comments on commit bc4fec1

Please sign in to comment.