refactor 💃
This commit is contained in:
parent
5499441b8e
commit
aa746186eb
|
@ -12,7 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// Shell.
|
// Package shell include shell related mainipulations.
|
||||||
package shell
|
package shell
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
32
util/go.go
32
util/go.go
|
@ -1,11 +1,11 @@
|
||||||
// Copyright (c) 2014, B3log
|
// Copyright (c) 2014, B3log
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
//
|
//
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -15,16 +15,16 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PathSeparator = string(os.PathSeparator) // OS-specific path separator
|
pathSeparator = string(os.PathSeparator) // OS-specific path separator
|
||||||
PathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
|
pathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
|
||||||
)
|
)
|
||||||
|
|
||||||
type mygo struct{}
|
type mygo struct{}
|
||||||
|
@ -54,13 +54,13 @@ func (*mygo) IsAPI(path string) bool {
|
||||||
|
|
||||||
// GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].
|
// GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].
|
||||||
func (*mygo) GetGoFormats() []string {
|
func (*mygo) GetGoFormats() []string {
|
||||||
ret := []string {"gofmt"}
|
ret := []string{"gofmt"}
|
||||||
|
|
||||||
p := Go.GetExecutableInGOBIN("goimports")
|
p := Go.GetExecutableInGOBIN("goimports")
|
||||||
if File.IsExist(p) {
|
if File.IsExist(p) {
|
||||||
ret = append(ret, "goimports")
|
ret = append(ret, "goimports")
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,26 +76,26 @@ func (*mygo) GetExecutableInGOBIN(executable string) string {
|
||||||
|
|
||||||
for _, gopath := range gopaths {
|
for _, gopath := range gopaths {
|
||||||
// $GOPATH/bin/$GOOS_$GOARCH/executable
|
// $GOPATH/bin/$GOOS_$GOARCH/executable
|
||||||
ret := gopath + PathSeparator + "bin" + PathSeparator +
|
ret := gopath + pathSeparator + "bin" + pathSeparator +
|
||||||
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable
|
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + pathSeparator + executable
|
||||||
if File.IsExist(ret) {
|
if File.IsExist(ret) {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
|
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
|
||||||
ret = gopath + PathSeparator + "bin" + PathSeparator +
|
ret = gopath + pathSeparator + "bin" + pathSeparator +
|
||||||
runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable
|
runtime.GOOS + "_" + runtime.GOARCH + pathSeparator + executable
|
||||||
if File.IsExist(ret) {
|
if File.IsExist(ret) {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// $GOPATH/bin/executable
|
// $GOPATH/bin/executable
|
||||||
ret = gopath + PathSeparator + "bin" + PathSeparator + executable
|
ret = gopath + pathSeparator + "bin" + pathSeparator + executable
|
||||||
if File.IsExist(ret) {
|
if File.IsExist(ret) {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// $GOBIN/executable
|
// $GOBIN/executable
|
||||||
return os.Getenv("GOBIN") + PathSeparator + executable
|
return os.Getenv("GOBIN") + pathSeparator + executable
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
// Copyright (c) 2014, B3log
|
// Copyright (c) 2014, B3log
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
//
|
//
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// Utilities.
|
// Package util includes common utilities.
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
// Copyright (c) 2014, B3log
|
// Copyright (c) 2014, B3log
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
//
|
//
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// 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
|
longest := 0
|
||||||
x_longest := 0
|
xLongest := 0
|
||||||
|
|
||||||
for x := 1; x < 1+len(s1); x++ {
|
for x := 1; x < 1+len(s1); x++ {
|
||||||
for y := 1; y < 1+len(s2); y++ {
|
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
|
m[x][y] = m[x-1][y-1] + 1
|
||||||
if m[x][y] > longest {
|
if m[x][y] > longest {
|
||||||
longest = m[x][y]
|
longest = m[x][y]
|
||||||
x_longest = x
|
xLongest = x
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m[x][y] = 0
|
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]
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ const (
|
||||||
defaultBufSize = 4096
|
defaultBufSize = 4096
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Reader represents a reader.
|
||||||
type Reader struct {
|
type Reader struct {
|
||||||
buf []byte
|
buf []byte
|
||||||
rd io.Reader
|
rd io.Reader
|
||||||
|
@ -34,6 +35,7 @@ type Reader struct {
|
||||||
const minReadBufferSize = 16
|
const minReadBufferSize = 16
|
||||||
const maxConsecutiveEmptyReads = 100
|
const maxConsecutiveEmptyReads = 100
|
||||||
|
|
||||||
|
// NewReaderSize creates a reader with the specified buffer size.
|
||||||
func NewReaderSize(rd io.Reader, size int) *Reader {
|
func NewReaderSize(rd io.Reader, size int) *Reader {
|
||||||
b, ok := rd.(*Reader)
|
b, ok := rd.(*Reader)
|
||||||
if ok && len(b.buf) >= size {
|
if ok && len(b.buf) >= size {
|
||||||
|
@ -102,8 +104,10 @@ func (b *Reader) readErr() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Buffered returns the size of buffered.
|
||||||
func (b *Reader) Buffered() int { return b.w - b.r }
|
func (b *Reader) Buffered() int { return b.w - b.r }
|
||||||
|
|
||||||
|
// ReadData reads a line.
|
||||||
func (b *Reader) ReadData() (line string, err error) {
|
func (b *Reader) ReadData() (line string, err error) {
|
||||||
if n := b.Buffered(); n < len(b.buf) {
|
if n := b.Buffered(); n < len(b.buf) {
|
||||||
b.fill()
|
b.fill()
|
||||||
|
|
|
@ -22,7 +22,7 @@ import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WebSocket channel.
|
// WSChannel represents a WebSocket channel.
|
||||||
type WSChannel struct {
|
type WSChannel struct {
|
||||||
Sid string // wide session id
|
Sid string // wide session id
|
||||||
Conn *websocket.Conn // websocket connection
|
Conn *websocket.Conn // websocket connection
|
||||||
|
|
13
util/zip.go
13
util/zip.go
|
@ -1,11 +1,11 @@
|
||||||
// Copyright (c) 2014, B3log
|
// Copyright (c) 2014, B3log
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
//
|
//
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -27,11 +27,13 @@ type myzip struct{}
|
||||||
// Zip utilities.
|
// Zip utilities.
|
||||||
var Zip = myzip{}
|
var Zip = myzip{}
|
||||||
|
|
||||||
|
// ZipFile represents a zip file.
|
||||||
type ZipFile struct {
|
type ZipFile struct {
|
||||||
zipFile *os.File
|
zipFile *os.File
|
||||||
writer *zip.Writer
|
writer *zip.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create creates a zip file with the specified filename.
|
||||||
func (*myzip) Create(filename string) (*ZipFile, error) {
|
func (*myzip) Create(filename string) (*ZipFile, error) {
|
||||||
file, err := os.Create(filename)
|
file, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -41,6 +43,7 @@ func (*myzip) Create(filename string) (*ZipFile, error) {
|
||||||
return &ZipFile{zipFile: file, writer: zip.NewWriter(file)}, nil
|
return &ZipFile{zipFile: file, writer: zip.NewWriter(file)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes the zip file writer.
|
||||||
func (z *ZipFile) Close() error {
|
func (z *ZipFile) Close() error {
|
||||||
err := z.writer.Close()
|
err := z.writer.Close()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -50,6 +53,7 @@ func (z *ZipFile) Close() error {
|
||||||
return z.zipFile.Close() // close the underlying writer
|
return z.zipFile.Close() // close the underlying writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddEntryN adds entries.
|
||||||
func (z *ZipFile) AddEntryN(path string, names ...string) error {
|
func (z *ZipFile) AddEntryN(path string, names ...string) error {
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
zipPath := filepath.Join(path, name)
|
zipPath := filepath.Join(path, name)
|
||||||
|
@ -61,6 +65,7 @@ func (z *ZipFile) AddEntryN(path string, names ...string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddEntry adds a entry.
|
||||||
func (z *ZipFile) AddEntry(path, name string) error {
|
func (z *ZipFile) AddEntry(path, name string) error {
|
||||||
fi, err := os.Stat(name)
|
fi, err := os.Stat(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -99,6 +104,7 @@ func (z *ZipFile) AddEntry(path, name string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddDirectoryN adds directories.
|
||||||
func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
|
func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
err := z.AddDirectory(path, name)
|
err := z.AddDirectory(path, name)
|
||||||
|
@ -109,6 +115,7 @@ func (z *ZipFile) AddDirectoryN(path string, names ...string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddDirectory adds a directory.
|
||||||
func (z *ZipFile) AddDirectory(path, dirName string) error {
|
func (z *ZipFile) AddDirectory(path, dirName string) error {
|
||||||
files, err := ioutil.ReadDir(dirName)
|
files, err := ioutil.ReadDir(dirName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue