Compare commits
2 Commits
f5692585fa
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b720c6d360 | |||
| 3e6768b1e3 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
,
|
,
|
||||||
commago
|
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
|
||||||
|
}
|
||||||
8
main.go
8
main.go
@@ -8,16 +8,22 @@ import (
|
|||||||
func commandsUsage() string {
|
func commandsUsage() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
`commands:
|
`commands:
|
||||||
|
env %v
|
||||||
path %v
|
path %v
|
||||||
|
time %v
|
||||||
week %v
|
week %v
|
||||||
`, commandPathDescription, commandWeekDescription)
|
`, commandEnvDescription, commandPathDescription, commandTimeDescription, commandWeekDescription)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) >= 2 {
|
if len(os.Args) >= 2 {
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
|
case "env":
|
||||||
|
commandEnv(os.Args[1:])
|
||||||
case "path":
|
case "path":
|
||||||
commandPath(os.Args[1:])
|
commandPath(os.Args[1:])
|
||||||
|
case "time":
|
||||||
|
commandTime(os.Args[1:])
|
||||||
case "week":
|
case "week":
|
||||||
commandWeek(os.Args[1:])
|
commandWeek(os.Args[1:])
|
||||||
default:
|
default:
|
||||||
|
|||||||
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()))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user