Skip to content
This repository has been archived by the owner on Jun 29, 2019. It is now read-only.

Commit

Permalink
Merge b55abbd into f3c1e95
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyuforest committed Nov 1, 2018
2 parents f3c1e95 + b55abbd commit dccfc62
Show file tree
Hide file tree
Showing 8 changed files with 677 additions and 439 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Agenda is a meeting management system.
Install agenda.

```sh
$ go install github.com/MegaShow/goagenda
$ go get -u -v github.com/MegaShow/goagenda
```

Create soft link.
Expand Down Expand Up @@ -91,6 +91,7 @@ $ agenda user set [-p <password>] [-e <email>] [-t <telephone>]
- You can use `s` as an alias for `set`.
- The password cannot be empty.
- The email address and telephone can be set empty. Please input `-e ""` or `-t ""` to indicate it.
- Windows Powershell doesn't recognize `""` .

### Delete user

Expand Down
6 changes: 3 additions & 3 deletions controller/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"fmt"
"github.com/MegaShow/goagenda/lib/log"
"github.com/logrusorgru/aurora"
"github.com/MegaShow/goagenda/lib/tty"
"github.com/spf13/viper"
"os"
)
Expand Down Expand Up @@ -83,12 +83,12 @@ func (c *Controller) GetStatus() {
func (c *Controller) Log() {
isOpen := viper.GetBool("Log.IsOpen")
if isOpen {
fmt.Println(aurora.Red("warning!!!"))
tty.ColorfulError("warning!!!")
fmt.Println("you are trying to access the log file")
fmt.Println("if you don't want that everyone can access log file, please set 'false' in the config file")
fmt.Println()
} else {
fmt.Println(aurora.Red("permission denied"))
tty.ColorfulError("permission denied")
return
}
fmt.Println("print only 10 lines, more in file")
Expand Down
4 changes: 2 additions & 2 deletions lib/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package log

import (
"fmt"
"github.com/logrusorgru/aurora"
"github.com/MegaShow/goagenda/lib/tty"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
Expand All @@ -28,7 +28,7 @@ func Info(msg string) {

// log error message and exit with status 1
func Error(msg string) {
fmt.Println(aurora.Red(msg))
tty.ColorfulError(msg)
log.Errorln(msg)
Release()
os.Exit(1)
Expand Down
5 changes: 5 additions & 0 deletions lib/tty/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tty

func ColorfulError(msg string) {
printlnError(msg)
}
10 changes: 10 additions & 0 deletions lib/tty/color_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tty

import (
"fmt"
"github.com/logrusorgru/aurora"
)

func printlnError(msg string) {
fmt.Println(aurora.Red(msg))
}
85 changes: 85 additions & 0 deletions lib/tty/color_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// +build !linux

package tty

import (
"fmt"
"syscall"
"unsafe"
)

type color uint16

const (
Red = color(0x000C)
)

const (
mask = uint16(Red)
)

var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetStdHandle = kernel32.NewProc("GetStdHandle")
procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
hStdout uintptr
initScreenInfo *consoleScreenBufferInfo
)

func setConsoleTextAttribute(hConsoleOutput uintptr, wAttributes uint16) bool {
ret, _, _ := procSetConsoleTextAttribute.Call(hConsoleOutput, uintptr(wAttributes))
return ret != 0
}

type coord struct {
X, Y int16
}

type smallRect struct {
Left, Top, Right, Bottom int16
}

type consoleScreenBufferInfo struct {
DwSize coord
DwCursorPosition coord
WAttributes uint16
SrWindow smallRect
DwMaximumWindowSize coord
}

func getConsoleScreenBufferInfo(hConsoleOutput uintptr) *consoleScreenBufferInfo {
var csbi consoleScreenBufferInfo
ret, _, _ := procGetConsoleScreenBufferInfo.Call(hConsoleOutput, uintptr(unsafe.Pointer(&csbi)))
if ret == 0 {
return nil
}
return &csbi
}

const (
stdOutputHandle = uint32(-11 & 0xFFFFFFFF)
)

func init() {
hStdout, _, _ = procGetStdHandle.Call(uintptr(stdOutputHandle))
initScreenInfo = getConsoleScreenBufferInfo(hStdout)
}

func resetColor() {
if initScreenInfo == nil {
return
}
setConsoleTextAttribute(hStdout, initScreenInfo.WAttributes)
}

func changeColor(c color) {
attr := uint16(0) & ^mask | uint16(c)
setConsoleTextAttribute(hStdout, attr)
}

func printlnError(msg string) {
changeColor(Red)
fmt.Println(msg)
resetColor()
}
15 changes: 7 additions & 8 deletions lib/verify/verify.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
package verify

import (
"fmt"
"github.com/logrusorgru/aurora"
"github.com/MegaShow/goagenda/lib/tty"
"os"
"regexp"
"time"
)

func AssertTimeEqual(t1, t2 time.Time, msg string) {
if t1 != t2 {
fmt.Println(aurora.Red(msg))
tty.ColorfulError(msg)
os.Exit(2)
}
}

func AssertTimeNonEqual(t1, t2 time.Time, msg string) {
if t1 == t2 {
fmt.Println(aurora.Red(msg))
tty.ColorfulError(msg)
os.Exit(2)
}
}

func AssertReg(pattern, s, msg string) {
matched, err := regexp.MatchString(pattern, s)
if err != nil || !matched {
fmt.Println(aurora.Red(msg))
tty.ColorfulError(msg)
os.Exit(2)
}
}

func AssertNonNil(s, msg string) {
if s == "" {
fmt.Println(aurora.Red(msg))
tty.ColorfulError(msg)
os.Exit(2)
}
}

func AssertLength(minLen, maxLen int, s, msg string) {
if len(s) < minLen || len(s) > maxLen {
fmt.Println(aurora.Red(msg))
tty.ColorfulError(msg)
os.Exit(2)
}
}

func AssertArrayLength(minLen, maxLen int, arr []string, msg string) {
if len(arr) < minLen || len(arr) > maxLen {
fmt.Println(aurora.Red(msg))
tty.ColorfulError(msg)
os.Exit(2)
}
}
Loading

0 comments on commit dccfc62

Please sign in to comment.