-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
56 lines (45 loc) · 899 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"github.com/momaek/formattag/align"
)
var version string
func main() {
var (
file string
showVersion bool
writeToConsole bool
)
flag.StringVar(&file, "file", "", "input data")
flag.BoolVar(&showVersion, "v", false, "show version")
flag.BoolVar(&writeToConsole, "C", false, "Write result to console")
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
if showVersion {
fmt.Println("Version:", version)
return
}
if len(file) > 0 {
align.Init(file)
} else {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
reader := bufio.NewReader(os.Stdin)
align.Init(reader)
writeToConsole = true
}
}
b, err := align.Do()
if err != nil {
log.Fatal("align failed ", err)
}
if writeToConsole {
fmt.Println(string(b))
return
}
os.WriteFile(file, b, 0)
}