Compare commits
4 Commits
d54271dade
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b720c6d360 | |||
| 3e6768b1e3 | |||
| f5692585fa | |||
| ded7c14007 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
,
|
||||
commago
|
||||
*~
|
||||
.env*
|
||||
|
||||
74
env.go
Normal file
74
env.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var commandEnvDescription = "load environment variable from .env file"
|
||||
|
||||
func commandEnv(args []string) {
|
||||
var flagSet = flag.NewFlagSet("env", flag.ContinueOnError)
|
||||
envFilePath := flagSet.String("file", ".env", "the file contains environment variables to parse")
|
||||
printToScreen := flagSet.Bool("print", false, "wethear or not print to screen")
|
||||
_ = printToScreen
|
||||
printWithExport := flagSet.Bool("export", false, "wethear or not add `export` prefix to each variable when printing")
|
||||
flagSet.Parse(args[1:])
|
||||
_, err := parseEnvFileAndSetEnv(*envFilePath, *printToScreen, *printWithExport)
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
if len(flagSet.Args()) > 0 {
|
||||
cmd := exec.Command(flagSet.Args()[0], flagSet.Args()[1:]...)
|
||||
var out strings.Builder
|
||||
cmd.Stdout = &out
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf(out.String())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func parseEnvFileAndSetEnv(filePath string, printToScreen, printWithExport bool) (string, error) {
|
||||
raw, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read from %v has error: %w", filePath, err)
|
||||
}
|
||||
rawLines := strings.Split(string(raw), "\n")
|
||||
for i, line := range rawLines {
|
||||
trimedLine := strings.TrimSpace(line)
|
||||
if strings.HasPrefix(trimedLine, "#") {
|
||||
continue
|
||||
}
|
||||
if trimedLine == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.Split(trimedLine, "=")
|
||||
if len(parts) != 2 {
|
||||
return "", fmt.Errorf("line %v is malformed: `%v`", i+1, trimedLine)
|
||||
}
|
||||
if strings.TrimSpace(parts[0]) != parts[0] {
|
||||
return "", fmt.Errorf("line %v is malformed: between variable name and `=` can't has any space", i+1)
|
||||
}
|
||||
if strings.TrimSpace(parts[1]) != parts[1] {
|
||||
return "", fmt.Errorf("line %v is malformed: between `=` and value can't has any space", i+1)
|
||||
}
|
||||
os.Setenv(parts[0], parts[1])
|
||||
if printToScreen {
|
||||
s := fmt.Sprintf("%v=%v\n", parts[0], parts[1])
|
||||
if printWithExport {
|
||||
s = fmt.Sprintf("export %v", s)
|
||||
}
|
||||
fmt.Print(s)
|
||||
}
|
||||
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
11
main.go
11
main.go
@@ -8,15 +8,24 @@ import (
|
||||
func commandsUsage() string {
|
||||
return fmt.Sprintf(
|
||||
`commands:
|
||||
env %v
|
||||
path %v
|
||||
`, commandPathDescription)
|
||||
time %v
|
||||
week %v
|
||||
`, commandEnvDescription, commandPathDescription, commandTimeDescription, commandWeekDescription)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) >= 2 {
|
||||
switch os.Args[1] {
|
||||
case "env":
|
||||
commandEnv(os.Args[1:])
|
||||
case "path":
|
||||
commandPath(os.Args[1:])
|
||||
case "time":
|
||||
commandTime(os.Args[1:])
|
||||
case "week":
|
||||
commandWeek(os.Args[1:])
|
||||
default:
|
||||
fmt.Printf("unknow command `%v`\n\n", os.Args[1])
|
||||
fmt.Printf("%v", commandsUsage())
|
||||
|
||||
48
time.go
Normal file
48
time.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var commandTimeDescription = "display more information about target timestamp/time"
|
||||
|
||||
func commandTime(args []string) {
|
||||
var err error
|
||||
targetTime := time.Now()
|
||||
if len(args) > 1 {
|
||||
targetTime, err = time.Parse(time.RFC3339, args[1])
|
||||
if err == nil {
|
||||
goto GOTTIME
|
||||
}
|
||||
if len(args[1]) <= 10 {
|
||||
var unix int
|
||||
unix, err = strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
fmt.Printf("`%v` is not a Unix Epoch Time\n", args[1])
|
||||
os.Exit(1)
|
||||
}
|
||||
targetTime = time.Unix(int64(unix), 0)
|
||||
} else if len(args[1]) >= 13 {
|
||||
var unixMilli int
|
||||
unixMilli, err = strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
fmt.Printf("`%v` is not a Unix Epoch Time in milliseconds\n", args[1])
|
||||
os.Exit(1)
|
||||
}
|
||||
targetTime = time.UnixMilli(int64(unixMilli))
|
||||
} else {
|
||||
fmt.Printf("`%v` is neighter a Unix Epoch Time, nor a formatted time in ISO8601", args[1])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
}
|
||||
GOTTIME:
|
||||
fmt.Printf("Unix: %v\n", targetTime.Unix())
|
||||
fmt.Printf("Unix milliseconds: %v\n", targetTime.UnixMilli())
|
||||
fmt.Printf("Local time: %v\n", targetTime.Local().Format(time.RFC3339))
|
||||
fmt.Printf("UTC time: %v\n", targetTime.UTC().Format(time.RFC3339))
|
||||
fmt.Printf("Time to target: %v\n", targetTime.Sub(time.Now()))
|
||||
}
|
||||
36
week.go
Normal file
36
week.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var commandWeekDescription = "get week in this year and week begin and end offset by this week"
|
||||
|
||||
func commandWeek(args []string) {
|
||||
var err error
|
||||
offset := 0
|
||||
if len(args) == 2 {
|
||||
offset, err = strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
fmt.Printf("expect `%v` to be a integer\n", args[1])
|
||||
os.Exit(1)
|
||||
}
|
||||
} else if len(args) > 2 {
|
||||
fmt.Printf("only accept 1 argument, got %v\n", len(args)-1)
|
||||
os.Exit(1)
|
||||
}
|
||||
offsetTime := time.Hour * time.Duration(offset) * 7 * 24
|
||||
targetTime := time.Now().Add(offsetTime)
|
||||
_, week := targetTime.ISOWeek()
|
||||
weekday := int(targetTime.Weekday())
|
||||
// make Sunday = 7
|
||||
if weekday == 0 {
|
||||
weekday = 7
|
||||
}
|
||||
weekBegin := targetTime.Add(time.Hour * 24 * time.Duration(weekday-1) * -1)
|
||||
weekEnd := targetTime.Add(time.Hour * 24 * time.Duration(7-weekday))
|
||||
fmt.Printf("week-%v-%v-%v\n", week, weekBegin.Format("0102"), weekEnd.Format("0102"))
|
||||
}
|
||||
Reference in New Issue
Block a user