-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
58 lines (50 loc) · 1.19 KB
/
util.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
package main
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"github.com/manifoldco/promptui"
)
func hash(s string) (string, error) {
h := md5.New()
_, err := fmt.Fprint(h, s)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func formatError(err error) string {
label := promptui.Styler(promptui.BGRed, promptui.FGBlack)("ERROR")
errMsg := promptui.Styler(promptui.FGRed)(err)
return fmt.Sprintf("%s %s", label, errMsg)
}
func errorAndExit(err error) {
if errors.Is(err, promptui.ErrInterrupt) {
return
}
fmt.Println(formatError(err))
os.Exit(1)
}
func openTextEditor(filePath string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("notepad")
default:
cmd = exec.Command("vim")
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Args = append(cmd.Args, filePath)
return cmd.Run()
}
func successMessage(profileName string, action Action) string {
label := promptui.Styler(promptui.BGGreen, promptui.FGWhite)("SUCCESS")
text := promptui.Styler(promptui.FGGreen)(fmt.Sprintf("%s profile \"%s\"", action, profileName))
return fmt.Sprintf("%s %s", label, text)
}