Skip to content
Snippets Groups Projects
Commit 6795ce9e authored by Tamer Tas's avatar Tamer Tas
Browse files

Refactor utils

parent 44ba4e10
No related branches found
No related tags found
No related merge requests found
......@@ -9,17 +9,24 @@ import (
const (
CodeOK = 0
CodeFatal = 1
CodeError = 2
)
func Error(errs ...error) {
for _, err := range errs {
tlog.Error(fmt.Sprint(err))
}
func Fatal(err error) {
tlog.Fatal(fmt.Sprint(err))
os.Exit(CodeFatal)
}
func Error(err error) {
tlog.Error(err.Error())
os.Exit(CodeError)
}
func OK(msg string) {
func OK(fmtStr string, s ...interface{}) {
tlog.Success(fmt.Sprintf(fmtStr, s...))
os.Exit(CodeOK)
}
package osutil
import (
"fmt"
"os"
"os/exec"
)
func FileExists(filename string) error {
_, err := os.Stat(filename)
func FileExists(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
return err
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return nil
if info.IsDir() {
return false, fmt.Errorf("%v: is a directory, expected file")
}
return true, nil
}
func DirExists(dirname string) error {
_, err := os.Stat(dirname)
func DirExists(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
return err
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return nil
if !info.IsDir() {
return false, fmt.Errorf("%v: is a file, expected directory")
}
return true, nil
}
func CreateDirs(dirPaths ...string) error {
......
// Package tlog implements logging utilities for tmplt
package tlog
import "github.com/Sirupsen/logrus"
import (
"fmt"
"github.com/fatih/color"
)
func Debug(msg string) {
logrus.Debug(msg)
fmt.Println(
color.New(color.BgYellow).SprintFunc()("DEBUG"),
color.New(color.FgYellow).SprintFunc()(msg))
}
func Success(msg string) {
fmt.Println(
color.New(color.BgGreen).SprintFunc()("SUCCESS"),
color.New(color.FgGreen).SprintFunc()(msg))
}
func Info(msg string) {
logrus.Info(msg)
fmt.Println(
color.New(color.BgBlue).SprintFunc()("INFO"),
color.New(color.FgBlue).SprintFunc()(msg))
}
func Warn(msg string) {
logrus.Warning(msg)
fmt.Println(
color.New(color.BgMagenta).SprintFunc()("WARN"),
color.New(color.FgMagenta).SprintFunc()(msg))
}
func Error(msg string) {
logrus.Error(msg)
fmt.Println(
color.New(color.BgRed).SprintFunc()("ERROR"),
color.New(color.Bold, color.FgRed).SprintFunc()(msg))
}
func Fatal(msg string) {
logrus.Fatal(msg)
}
func Panic(msg string) {
logrus.Panic(msg)
Error(msg)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment