-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revamp str package with new funcs; tests fixes
- Loading branch information
1 parent
fdc0b0f
commit dc21c26
Showing
4 changed files
with
134 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/vladimirvivien/echo" | ||
"github.com/vladimirvivien/echo/str" | ||
) | ||
|
||
|
||
// This example uses local git to create a file with commit logs. | ||
func main() { | ||
buf := new(bytes.Buffer) | ||
cmd := `/bin/sh -c "git log --reverse --abbrev-commit --pretty=oneline | cut -d ' ' -f1"` | ||
for _, p := range str.SplitLines(echo.Run(cmd)) { | ||
echo.SetVar("patch", p) | ||
cmd := `/bin/sh -c "git show --abbrev-commit -s --pretty=format:'%h %s (%an) %n' ${patch}"` | ||
buf.WriteString(fmt.Sprintf("%s\n",echo.Run(cmd))) | ||
} | ||
|
||
gitfile := "./gitlog.txt" | ||
|
||
if w := echo.Write(gitfile).ReadFrom(buf); w.Err() != nil { | ||
fmt.Println(w.Err()) | ||
os.Exit(1) | ||
} | ||
|
||
// read the file and print | ||
fmt.Println(echo.Read(gitfile).String()) | ||
|
||
if err := os.Remove(gitfile); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package str | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func IsEmpty(str string) bool { | ||
return String(str).IsEmpty() | ||
} | ||
|
||
// SplitLines splits each line from str into []string | ||
func SplitLines(str string) []string { | ||
return String(str).SplitLines() | ||
} | ||
|
||
// SplitSpaces splits str by blank chars (space,\t,\n) | ||
func SplitSpaces(str string) []string { | ||
return String(str).SplitSpaces() | ||
} | ||
|
||
// Bool returns the bool equivalent of str ("true" = true, etc) | ||
// A parsing error will cause a program panic. | ||
func Bool(str string) bool { | ||
s := String(str) | ||
result := s.Bool() | ||
if s.Err() != nil { | ||
panic(fmt.Sprintf("%s", s.Err())) | ||
} | ||
return result | ||
} | ||
|
||
// Int returns the int representation of str. | ||
// A parsing error will cause a program panic. | ||
func Int(str string) int { | ||
s := String(str) | ||
result := s.Int() | ||
if s.Err() != nil { | ||
panic(fmt.Sprintf("%s", s.Err())) | ||
} | ||
return result | ||
} | ||
|
||
// Float64 returns the float64 representation of str. | ||
// A parsing error will cause a program panic. | ||
func Float64(str string) float64 { | ||
s := String(str) | ||
result := s.Float64() | ||
if s.Err() != nil { | ||
panic(fmt.Sprintf("%s", s.Err())) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters