1 Star 0 Fork 0

青文杰/etcd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
csv2table.go 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
XiangLi 提交于 2016-03-17 14:49 . etcdctlv3: make member list beautiful
package main
import (
"encoding/csv"
"flag"
"fmt"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/olekukonko/tablewriter"
"io"
"os"
"unicode/utf8"
)
var (
fileName = flag.String("f", "", "Set file with eg. sample.csv")
delimiter = flag.String("d", ",", "Set CSV File delimiter eg. ,|;|\t ")
header = flag.Bool("h", true, "Set header options eg. true|false ")
align = flag.String("a", "none", "Set aligmement with eg. none|left|right|centre")
pipe = flag.Bool("p", false, "Suport for Piping from STDIN")
border = flag.Bool("b", true, "Enable / disable table border")
)
func main() {
flag.Parse()
fmt.Println()
if *pipe || hasArg("-p") {
process(os.Stdin)
} else {
if *fileName == "" {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Println()
os.Exit(1)
}
processFile()
}
fmt.Println()
}
func hasArg(name string) bool {
for _, v := range os.Args {
if name == v {
return true
}
}
return false
}
func processFile() {
r, err := os.Open(*fileName)
if err != nil {
exit(err)
}
defer r.Close()
process(r)
}
func process(r io.Reader) {
csvReader := csv.NewReader(r)
rune, size := utf8.DecodeRuneInString(*delimiter)
if size == 0 {
rune = ','
}
csvReader.Comma = rune
table, err := tablewriter.NewCSVReader(os.Stdout, csvReader, *header)
if err != nil {
exit(err)
}
switch *align {
case "left":
table.SetAlignment(tablewriter.ALIGN_LEFT)
case "right":
table.SetAlignment(tablewriter.ALIGN_RIGHT)
case "center":
table.SetAlignment(tablewriter.ALIGN_CENTRE)
}
table.SetBorder(*border)
table.Render()
}
func exit(err error) {
fmt.Fprintf(os.Stderr, "#Error : %s", err)
os.Exit(1)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/qingwenjie/etcd.git
git@gitee.com:qingwenjie/etcd.git
qingwenjie
etcd
etcd
v2.3.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385