Compare commits

..

6 Commits

6 changed files with 104 additions and 103 deletions

View File

@ -95,23 +95,23 @@ func MoveFile(sourcePath, destPath string) (int64, error) {
if err != nil {
inputFile, err := os.Open(sourcePath)
if err != nil {
return 0, fmt.Errorf("Couldn't open source file: %s", err)
return 0, fmt.Errorf("couldn't open source file: %s", err)
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return 0, fmt.Errorf("Couldn't open dest file: %s", err)
return 0, fmt.Errorf("couldn't open dest file: %s", err)
}
defer outputFile.Close()
size, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return 0, fmt.Errorf("Writing to output file failed: %s", err)
return 0, fmt.Errorf("failed writing to output file: %s", err)
}
// The copy was successful, so now delete the original file
err = os.Remove(sourcePath)
if err != nil {
return 0, fmt.Errorf("Failed removing original file: %s", err)
return 0, fmt.Errorf("failed removing original file: %s", err)
}
}
return size, nil
@ -136,7 +136,7 @@ func ParseMemSize(s string) (size int64, err error) {
size, err = strconv.ParseInt(s, 10, 64)
}
} else {
err = errors.New("Empty string")
err = errors.New("empty string")
}
return
}

View File

@ -15,9 +15,9 @@ import (
const GZIP_SUFFIX = ".gz"
func CreateGzipReader(in io.Reader) (reader *bufio.Reader, err error) {
inflate, err := gzip.NewReader(in)
if err != nil {
fmt.Errorf("Can't inflate gzipped input stream: %v", err)
var inflate *gzip.Reader
if inflate, err = gzip.NewReader(in); err != nil {
err = fmt.Errorf("can't inflate gzipped input stream: %v", err)
} else {
reader = bufio.NewReader(inflate)
}
@ -25,10 +25,10 @@ func CreateGzipReader(in io.Reader) (reader *bufio.Reader, err error) {
}
func CreateGzipFile(in io.Reader, fileName string) (reader *bufio.Reader, err error) {
var inflate *gzip.Reader
if len(fileName) > 0 && strings.HasSuffix(fileName, GZIP_SUFFIX) {
inflate, err := gzip.NewReader(in)
if err != nil {
fmt.Errorf("Can't inflate gzipped input stream %#v: %v", fileName, err)
if inflate, err = gzip.NewReader(in); err != nil {
err = fmt.Errorf("can't inflate gzipped input stream %#v: %v", fileName, err)
} else {
reader = bufio.NewReader(inflate)
}

View File

@ -20,7 +20,7 @@ func NewEnglishPrinter() *message.Printer {
return message.NewPrinter(language.English)
}
func ExitErrorf(rc int, format string, args ...interface{}) {
func ExitErrorf(rc int, format string, args ...any) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(rc)
}
@ -31,7 +31,7 @@ func ExitMessagef(rc int, format string, args ...interface{}) {
}
func OnStringIndex(index uint, values ...string) (value string) {
if index >= 0 && index < uint(len(values)) {
if index < uint(len(values)) {
value = values[index]
}
return
@ -42,12 +42,13 @@ func OnEmpty(s string, altValues ...string) (value string) {
for _, altValue := range altValues {
if len(altValue) > 0 {
value = altValue
break
}
}
} else {
value = s
}
return s
return value
}
func OnCond(cond bool, trueValue, falseValue string) string {

View File

@ -132,7 +132,7 @@ func MakeParentDir(filePath string) (dir string, err error) {
dir = filepath.Dir(filePath)
if !IsDirectory(dir) {
if err1 := os.MkdirAll(dir, 0755); err1 != nil {
err = fmt.Errorf("Can't make parent path of %#v: %v", filePath, err1)
err = fmt.Errorf("can't make parent path of %#v: %v", filePath, err1)
}
}
return
@ -157,7 +157,7 @@ func ExpandFilePathAndMakeParentV(specPath string, maps ...map[string]string) (e
func MakeDir(dirPath string) (err error) {
if !IsDirectory(dirPath) {
if err1 := os.MkdirAll(dirPath, 0755); err1 != nil {
err = fmt.Errorf("Can't make directory path %#v: %v", dirPath, err1)
err = fmt.Errorf("can't make directory path %#v: %v", dirPath, err1)
}
}
return

View File

@ -59,26 +59,26 @@ func NewTextTemplate(args ...string) (instance *TextTemplate) {
return
}
func (self *TextTemplate) initVarMap() {
self.varMap["dt"] = "Current timestamp as YYYY-MM-DD_HH-MM-SS"
self.varMap["ymd"] = "Current date as YYYY-MM-DD"
self.varMap["hm"] = "Current time as HH-MM"
self.varMap["h"] = "Current time as HH"
self.varMap["hms"] = "Current time as HH-MM-SS"
self.varMap["month"] = "Month name"
self.varMap["month-num"] = "Month as number (mm)"
self.varMap["week-day"] = "Week day name"
self.varMap["week-day-num"] = "Week day as number"
self.varMap["yesterday"] = "Yesterday date"
self.varMap["uid"] = self.Uid
self.varMap["username"] = self.UserName
self.varMap["home"] = self.HomeDir
self.varMap["progdir"] = self.ProgramDir
self.varMap["hostname"], _ = os.Hostname()
func (tt *TextTemplate) initVarMap() {
tt.varMap["dt"] = "Current timestamp as YYYY-MM-DD_HH-MM-SS"
tt.varMap["ymd"] = "Current date as YYYY-MM-DD"
tt.varMap["hm"] = "Current time as HH-MM"
tt.varMap["h"] = "Current time as HH"
tt.varMap["hms"] = "Current time as HH-MM-SS"
tt.varMap["month"] = "Month name"
tt.varMap["month-num"] = "Month as number (mm)"
tt.varMap["week-day"] = "Week day name"
tt.varMap["week-day-num"] = "Week day as number"
tt.varMap["yesterday"] = "Yesterday date"
tt.varMap["uid"] = tt.Uid
tt.varMap["username"] = tt.UserName
tt.varMap["home"] = tt.HomeDir
tt.varMap["progdir"] = tt.ProgramDir
tt.varMap["hostname"], _ = os.Hostname()
}
func (self *TextTemplate) AddVar(name, value string) {
self.varMap[name] = value
func (tt *TextTemplate) AddVar(name, value string) {
tt.varMap[name] = value
}
// func MakeVarMap() map[string]string {
@ -92,21 +92,21 @@ func (self *TextTemplate) AddVar(name, value string) {
// }
// }
func (self *TextTemplate) updateVarMap() {
func (tt *TextTemplate) updateVarMap() {
var now = time.Now()
self.varMap["ymd"] = fmt.Sprintf("%04d-%02d-%02d", now.Year(), now.Month(), now.Day())
self.varMap["hms"] = fmt.Sprintf("%02d-%02d-%02d", now.Hour(), now.Minute(), now.Second())
self.varMap["hm"] = fmt.Sprintf("%02d-%02d", now.Hour(), now.Minute())
self.varMap["h"] = fmt.Sprintf("%02d", now.Hour())
self.varMap["month"] = now.Format("January")
self.varMap["month-num"] = fmt.Sprintf("%02d", now.Month())
self.varMap["week-day-num"] = fmt.Sprintf("%d", 1+now.Weekday())
self.varMap["week-day"] = now.Format("Monday")
self.varMap["dt"] = self.varMap["ymd"] + "_" + self.varMap["hms"]
tt.varMap["ymd"] = fmt.Sprintf("%04d-%02d-%02d", now.Year(), now.Month(), now.Day())
tt.varMap["hms"] = fmt.Sprintf("%02d-%02d-%02d", now.Hour(), now.Minute(), now.Second())
tt.varMap["hm"] = fmt.Sprintf("%02d-%02d", now.Hour(), now.Minute())
tt.varMap["h"] = fmt.Sprintf("%02d", now.Hour())
tt.varMap["month"] = now.Format("January")
tt.varMap["month-num"] = fmt.Sprintf("%02d", now.Month())
tt.varMap["week-day-num"] = fmt.Sprintf("%d", 1+now.Weekday())
tt.varMap["week-day"] = now.Format("Monday")
tt.varMap["dt"] = tt.varMap["ymd"] + "_" + tt.varMap["hms"]
yday := now.AddDate(0, 0, -1)
self.varMap["yesterday"] = fmt.Sprintf("%04d-%02d-%02d", yday.Year(), yday.Month(), yday.Day())
tt.varMap["yesterday"] = fmt.Sprintf("%04d-%02d-%02d", yday.Year(), yday.Month(), yday.Day())
// self.varMap["uid"] = strconv.Itoa(int(self.Uid))
// self.varMap["uid"] = self.Uid
// self.varMap["username"] = self.UserName
@ -114,12 +114,12 @@ func (self *TextTemplate) updateVarMap() {
// self.varMap["home"] = self.HomeDir
}
func (self *TextTemplate) Expand(template string) string {
func (tt *TextTemplate) Expand(template string) string {
self.updateVarMap()
tt.updateVarMap()
result := os.Expand(template, func(name string) string {
value, ok := self.varMap[name]
value, ok := tt.varMap[name]
if ok {
return value
} else {
@ -130,11 +130,11 @@ func (self *TextTemplate) Expand(template string) string {
return result
}
func (self *TextTemplate) ExpandEnv(template string) (result string) {
func (tt *TextTemplate) ExpandEnv(template string) (result string) {
if len(template) > 0 {
self.updateVarMap()
tt.updateVarMap()
result = os.Expand(template, func(name string) string {
value, ok := self.varMap[name]
value, ok := tt.varMap[name]
if ok {
return value
} else {
@ -159,22 +159,22 @@ func (self *TextTemplate) ExpandEnv(template string) (result string) {
return result
}
func (self *TextTemplate) Value(key string) (value string, ok bool) {
value, ok = self.varMap[key]
func (tt *TextTemplate) Value(key string) (value string, ok bool) {
value, ok = tt.varMap[key]
return
}
func (self *TextTemplate) String() string {
func (tt *TextTemplate) String() string {
var sb strings.Builder
keys := make([]string, 0, len(self.varMap))
for k := range self.varMap {
keys := make([]string, 0, len(tt.varMap))
for k := range tt.varMap {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
sb.WriteString(fmt.Sprintf("%s=%#v\n", k, self.varMap[k]))
sb.WriteString(fmt.Sprintf("%s=%#v\n", k, tt.varMap[k]))
}
// for k, v := range self.varMap {
// sb.WriteString(fmt.Sprintf("%s=%#v\n", k, v))

View File

@ -29,142 +29,142 @@ type TTYContext struct {
isTTY [3]bool
}
func (self *TTYContext) Init() {
for i, _ := range self.isTTY {
self.isTTY[i] = StreamIsTerminal(getStream(uint(i)))
func (ctx *TTYContext) Init() {
for i := range ctx.isTTY {
ctx.isTTY[i] = StreamIsTerminal(getStream(uint(i)))
}
}
func (self *TTYContext) IsTTY(fd uint) bool {
func (ctx *TTYContext) IsTTY(fd uint) bool {
if fd == STDERR || fd == STDOUT {
return self.isTTY[fd]
return ctx.isTTY[fd]
}
return false
}
func (self *TTYContext) Clean() {
for i, _ := range self.isTTY {
self.isTTY[i] = false
func (ctx *TTYContext) Clean() {
for i := range ctx.isTTY {
ctx.isTTY[i] = false
}
}
func (self *TTYContext) Bold(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) Bold(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[1m"
}
return ""
}
func (self *TTYContext) BoldOff(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) BoldOff(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[22m"
}
return ""
}
func (self *TTYContext) Underline(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) Underline(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[4m"
}
return ""
}
func (self *TTYContext) UnderlineOff(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) UnderlineOff(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[24m"
}
return ""
}
func (self *TTYContext) Italic(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) Italic(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[3m"
}
return ""
}
func (self *TTYContext) ItalicOff(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) ItalicOff(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[23m"
}
return ""
}
func (self *TTYContext) BlackFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) BlackFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[30m"
}
return ""
}
func (self *TTYContext) RedFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) RedFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[31m"
}
return ""
}
func (self *TTYContext) GreenFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) GreenFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[32m"
}
return ""
}
func (self *TTYContext) BrownFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) BrownFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[33m"
}
return ""
}
func (self *TTYContext) BlueFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) BlueFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[34m"
}
return ""
}
func (self *TTYContext) MagentaFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) MagentaFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[35m"
}
return ""
}
func (self *TTYContext) CyanFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) CyanFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[36m"
}
return ""
}
func (self *TTYContext) WhiteFg(fd uint) string {
if self.isTTY[fd] {
func (ctx *TTYContext) WhiteFg(fd uint) string {
if ctx.isTTY[fd] {
return "\x1b[37m"
}
return ""
}
func (self *TTYContext) FgColor(fd uint, n uint) string {
func (ctx *TTYContext) FgColor(fd uint, n uint) string {
var color string
switch n {
case BLACK:
color = self.BlackFg(fd)
color = ctx.BlackFg(fd)
case WHITE:
color = self.WhiteFg(fd)
color = ctx.WhiteFg(fd)
case RED:
color = self.RedFg(fd)
color = ctx.RedFg(fd)
case GREEN:
color = self.GreenFg(fd)
color = ctx.GreenFg(fd)
case BLUE:
color = self.BlueFg(fd)
color = ctx.BlueFg(fd)
case BROWN:
color = self.BrownFg(fd)
color = ctx.BrownFg(fd)
case CYAN:
color = self.CyanFg(fd)
color = ctx.CyanFg(fd)
case MAGENTA:
color = self.MagentaFg(fd)
color = ctx.MagentaFg(fd)
}
return color
}
@ -188,7 +188,7 @@ func (self *TTYContext) FgColor(fd uint, n uint) string {
// return color
// }
func (self *TTYContext) Reset(fd uint) string {
func (ctx *TTYContext) Reset(fd uint) string {
if StreamIsTerminal(getStream(fd)) {
return "\x1b[0m"
}