-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkswp.go
51 lines (44 loc) · 905 Bytes
/
kswp.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
package kswp
import (
"fmt"
"io"
"os"
)
type KubeConfig struct {
Name string
Path string
}
type Kswp struct {
KubeConf string
Configs []KubeConfig
}
func (k Kswp) Swap(config string) error {
kc, err := k.getConfig(config)
if err != nil {
return err
}
source, err := os.Open(kc.Path)
if err != nil {
return fmt.Errorf("Cannot read kubeconfig %s. \n %w", kc.Path, err)
}
defer source.Close()
destination, err := os.Create(k.KubeConf)
if err != nil {
return fmt.Errorf("Cannot read kubeconfig %s. \n %w", kc.Path, err)
}
defer destination.Close()
_, err = io.Copy(destination, source)
return err
}
func (k Kswp) getConfig(config string)(KubeConfig, error) {
var kc KubeConfig
for _, c := range k.Configs {
if config == c.Name {
kc = c
}
}
if kc.Name != config {
return kc, fmt.Errorf("Cannot find kubeconfig with name %s.", config)
}
return kc, nil
}