refactor 💃

This commit is contained in:
Liang Ding 2014-12-07 11:42:34 +08:00
parent 5499441b8e
commit aa746186eb
7 changed files with 42 additions and 31 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Shell.
// Package shell include shell related mainipulations.
package shell
import (

View File

@ -1,11 +1,11 @@
// Copyright (c) 2014, B3log
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,16 +15,16 @@
package util
import (
"path/filepath"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"os"
)
const (
PathSeparator = string(os.PathSeparator) // OS-specific path separator
PathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
pathSeparator = string(os.PathSeparator) // OS-specific path separator
pathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
)
type mygo struct{}
@ -54,13 +54,13 @@ func (*mygo) IsAPI(path string) bool {
// GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].
func (*mygo) GetGoFormats() []string {
ret := []string {"gofmt"}
ret := []string{"gofmt"}
p := Go.GetExecutableInGOBIN("goimports")
if File.IsExist(p) {
ret = append(ret, "goimports")
}
return ret
}
@ -76,26 +76,26 @@ func (*mygo) GetExecutableInGOBIN(executable string) string {
for _, gopath := range gopaths {
// $GOPATH/bin/$GOOS_$GOARCH/executable
ret := gopath + PathSeparator + "bin" + PathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable
ret := gopath + pathSeparator + "bin" + pathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + pathSeparator + executable
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
ret = gopath + PathSeparator + "bin" + PathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable
ret = gopath + pathSeparator + "bin" + pathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + pathSeparator + executable
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/executable
ret = gopath + PathSeparator + "bin" + PathSeparator + executable
ret = gopath + pathSeparator + "bin" + pathSeparator + executable
if File.IsExist(ret) {
return ret
}
}
// $GOBIN/executable
return os.Getenv("GOBIN") + PathSeparator + executable
return os.Getenv("GOBIN") + pathSeparator + executable
}

View File

@ -1,18 +1,18 @@
// Copyright (c) 2014, B3log
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Utilities.
// Package util includes common utilities.
package util
import (

View File

@ -1,11 +1,11 @@
// Copyright (c) 2014, B3log
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -41,7 +41,7 @@ func (*str) LCS(s1 string, s2 string) string {
}
longest := 0
x_longest := 0
xLongest := 0
for x := 1; x < 1+len(s1); x++ {
for y := 1; y < 1+len(s2); y++ {
@ -49,7 +49,7 @@ func (*str) LCS(s1 string, s2 string) string {
m[x][y] = m[x-1][y-1] + 1
if m[x][y] > longest {
longest = m[x][y]
x_longest = x
xLongest = x
}
} else {
m[x][y] = 0
@ -57,5 +57,5 @@ func (*str) LCS(s1 string, s2 string) string {
}
}
return s1[x_longest-longest : x_longest]
return s1[xLongest-longest : xLongest]
}

View File

@ -24,6 +24,7 @@ const (
defaultBufSize = 4096
)
// Reader represents a reader.
type Reader struct {
buf []byte
rd io.Reader
@ -34,6 +35,7 @@ type Reader struct {
const minReadBufferSize = 16
const maxConsecutiveEmptyReads = 100
// NewReaderSize creates a reader with the specified buffer size.
func NewReaderSize(rd io.Reader, size int) *Reader {
b, ok := rd.(*Reader)
if ok && len(b.buf) >= size {
@ -102,8 +104,10 @@ func (b *Reader) readErr() error {
return err
}
// Buffered returns the size of buffered.
func (b *Reader) Buffered() int { return b.w - b.r }
// ReadData reads a line.
func (b *Reader) ReadData() (line string, err error) {
if n := b.Buffered(); n < len(b.buf) {
b.fill()

View File

@ -22,7 +22,7 @@ import (
"github.com/gorilla/websocket"
)
// WebSocket channel.
// WSChannel represents a WebSocket channel.
type WSChannel struct {
Sid string // wide session id
Conn *websocket.Conn // websocket connection

View File

@ -1,11 +1,11 @@
// Copyright (c) 2014, B3log
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -27,11 +27,13 @@ type myzip struct{}
// Zip utilities.
var Zip = myzip{}
// ZipFile represents a zip file.
type ZipFile struct {
zipFile *os.File
writer *zip.Writer
}
// Create creates a zip file with the specified filename.
func (*myzip) Create(filename string) (*ZipFile, error) {
file, err := os.Create(filename)
if err != nil {
@ -41,6 +43,7 @@ func (*myzip) Create(filename string) (*ZipFile, error) {
return &ZipFile{zipFile: file, writer: zip.NewWriter(file)}, nil
}
// Close closes the zip file writer.
func (z *ZipFile) Close() error {
err := z.writer.Close()
if nil != err {
@ -50,6 +53,7 @@ func (z *ZipFile) Close() error {
return z.zipFile.Close() // close the underlying writer
}
// AddEntryN adds entries.
func (z *ZipFile) AddEntryN(path string, names ...string) error {
for _, name := range names {
zipPath := filepath.Join(path, name)
@ -61,6 +65,7 @@ func (z *ZipFile) AddEntryN(path string, names ...string) error {
return nil
}
// AddEntry adds a entry.
func (z *ZipFile) AddEntry(path, name string) error {
fi, err := os.Stat(name)
if err != nil {
@ -99,6 +104,7 @@ func (z *ZipFile) AddEntry(path, name string) error {
return err
}
// AddDirectoryN adds directories.
func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
for _, name := range names {
err := z.AddDirectory(path, name)
@ -109,6 +115,7 @@ func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
return nil
}
// AddDirectory adds a directory.
func (z *ZipFile) AddDirectory(path, dirName string) error {
files, err := ioutil.ReadDir(dirName)
if err != nil {