-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
executable file
·143 lines (125 loc) · 3.12 KB
/
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Purpose : a wrapper of k8s
Author : Ky-Anh Huynh
License : MIT
Date : 2020-02-15
*/
package main
import "fmt"
import "os"
import "os/exec"
import "strings"
import "path/filepath"
import "syscall"
func log2(msg string) {
fmt.Fprintf(os.Stderr, msg)
}
func log2exit(retval int, msg string) {
log2(msg)
os.Exit(retval)
}
/*
Reference: https://golangcode.com/check-if-a-file-exists/
FIXME: The source article was down.
*/
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
/* FIXME: The file may be regular ot not */
return !info.IsDir()
}
func args2cmd(args []string) (string, []string) {
command := "foo"
if len(args) == 0 {
command = "noop"
return command, []string{""}
}
if args[0] == "--" {
if len(args) > 1 {
command = args[1]
args = args[1:]
} else {
command = "noop"
return command, []string{""}
}
} else {
if strings.Index(args[0], "helm") == 0 {
command = "helm"
args = args[0:]
} else if strings.Index(args[0], "kubectl") == -1 {
command = "kubectl"
args = append([]string{command}, args...)
} else {
command = args[0]
}
}
binary, err := exec.LookPath(command)
if err != nil {
log2exit(127, fmt.Sprintf(":: Command not found %s\n", command))
}
for _, arg := range args {
if arg == "delete" || arg == "del" {
if os.Getenv("DELETE") == "true" {
break
}
if _, err := os.Stat(".delete"); os.IsNotExist(err) {
log2exit(1, ":: Error: File .delete doesn't exist in the current directory.\n")
} else {
err := os.Remove(".delete")
if err != nil {
log2exit(1, fmt.Sprintf(":: Error: %s.\n", err))
} else {
log2(":: File .delete was removed.\n")
}
}
break
}
}
return binary, args
}
func main() {
if len(os.Args) < 2 {
log2exit(1, ":: Error: Cluster name (or context) is required.\n")
}
cluster_name := os.Args[1]
if len(cluster_name) <= 1 {
log2exit(1, ":: Error: Cluster name must be something like :foo.\n")
}
if cluster_name[0:1] != ":" {
log2exit(1, fmt.Sprintf(":: Error: Cluster name (context) must be prefixed with `:', e.g., gk8s :%s.\n", cluster_name))
}
cluster_name = cluster_name[1:]
config_dir := os.Getenv("GK8S_HOME")
kubecfg := "foo"
if len(config_dir) > 0 {
kubecfg = filepath.Join(config_dir, cluster_name)
} else {
old_home := os.Getenv("HOME")
kubecfg = filepath.Join(old_home, ".config/gk8s/", cluster_name)
}
binary, args := "foo", []string{"bar"}
/* cluster name is provided, but nothing else */
if len(os.Args) == 2 {
binary = "noop"
} else {
binary, args = args2cmd(os.Args[2:])
}
if binary == "noop" {
log2exit(0, ":: noop command does nothing.\n")
}
if cluster_name == "local" {
home := os.Getenv("HOME")
kubecfg = filepath.Join(home, ".kube/config")
}
if !fileExists(kubecfg) {
log2exit(127, fmt.Sprintf(":: KUBECONFIG file not found: %s\n", kubecfg))
}
os.Setenv("KUBECONFIG", kubecfg)
log2(fmt.Sprintf(":: Executing '%s', args: %v, KUBECONFIG: %s\n", binary, args, kubecfg))
err := syscall.Exec(binary, args, syscall.Environ())
if err != nil {
log2exit(1, fmt.Sprintf(":: Error: %v.\n", err))
}
}