-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.go
110 lines (90 loc) · 2.51 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/urfave/cli"
)
func printError(args ...interface{}) {
log.Println(append([]interface{}{"\033[1;31mERROR:\033[0m"}, args...)...)
}
func printWarning(args ...interface{}) {
log.Println(append([]interface{}{"\033[1;31mWARNING:\033[0m"}, args...)...)
}
var errAbort = errors.New("gmig ABORTED")
func checkExists(filename string) error {
_, err := os.Stat(filename)
if err == nil {
return nil
}
abs, _ := filepath.Abs(filename)
return fmt.Errorf("no such migration (wrong project?, git pull?) [%s]", abs)
}
// runCommand is wrapper for CombinedOutput to make this package easy testable.
var runCommand = func(c *exec.Cmd) ([]byte, error) {
return c.CombinedOutput()
}
func reportError(cfg Config, envs []string, action string, err error) error {
log.Printf("executing [%s] failed, error: [%v]\n", action, err)
fmt.Println()
log.Println("reporting environment variables ...")
for _, each := range envs {
fmt.Println(each)
}
fmt.Println()
log.Println("checking gmig config ...")
fmt.Println(cfg.ToJSON())
fmt.Println()
gcloudConfigList()
return err
}
func started(c *cli.Context, action string) func() {
v := c.GlobalBool("v")
if !v {
return func() {}
}
log.Println("gmig version", Version)
log.Println("BEGIN", action)
start := time.Now()
return func() { log.Println("END", action, "completed in", time.Now().Sub(start)) }
}
func promptForYes(message string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Print(message)
yn, _ := reader.ReadString('\n')
return strings.HasPrefix(yn, "Y") || strings.HasPrefix(yn, "y")
}
// 20180227t140600_permit_infra_manager_to_deploy_to_gateway_cluster.yaml
// 2018-02-27 14:06:00 permit infra manager to deploy to gateway cluster
func pretty(filename string) string {
fn := strings.Replace(strings.TrimSuffix(filename, filepath.Ext(filename)), "_", " ", -1)
if len(fn) < 16 {
return fn
}
// 20060102t150405 is used as a sample format, see https://golang.org/pkg/time/#Parse
_, err := time.Parse("20060102t150405", fn[0:15])
if err != nil {
return fn
}
return fmt.Sprintf("%s-%s-%s %s:%s:%s %s",
filename[0:4],
filename[4:6],
filename[6:8],
filename[9:11],
filename[11:13],
filename[13:15],
fn[16:])
}
func isYamlFile(filename string) bool {
ext := filepath.Ext(filename)
return ext == ".yaml" || ext == ".yml"
}
var regexpIndex, _ = regexp.Compile("^[0-9]{3}_")
var regexpTimestamp, _ = regexp.Compile("^[0-9]{8}t[0-9]{6}_")