/
osutils.go
48 lines (41 loc) · 1007 Bytes
/
osutils.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
package osutils
import (
"fmt"
"os"
"path/filepath"
"github.com/olekukonko/tablewriter"
)
func EnsureDir(dir string) error {
stat, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
return os.MkdirAll(dir, os.ModePerm)
}
return err
}
if !stat.IsDir() {
return fmt.Errorf("%q is not a directory", dir)
}
return nil
}
func EnsureFilePathDir(filename string) error {
dir := filepath.Dir(filename)
return EnsureDir(dir)
}
func ShowTable(titles []string, rows [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(titles)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding(" ") // pad with tabs
table.SetNoWhiteSpace(true)
table.AppendBulk(rows) // Add Bulk Data
table.Render()
}