-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkubeconfig.go
50 lines (42 loc) · 1.2 KB
/
kubeconfig.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
func DownloadKubeconfig() error {
kubeconfigURL := "https://raw.githubusercontent.com/bacchus-snu/sgs-cli/main/config.yaml"
destinationPath := filepath.Join(os.Getenv("HOME"), ".sgs", "config.yaml")
// Check if the file already exists
if _, err := os.Stat(destinationPath); os.IsNotExist(err) {
// Create the directory if it doesn't exist
err := os.MkdirAll(filepath.Dir(destinationPath), 0755)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
// Download the kubeconfig file
response, err := http.Get(kubeconfigURL)
if err != nil {
return fmt.Errorf("failed to download kubeconfig file: %w", err)
}
defer response.Body.Close()
// Create the file
file, err := os.Create(destinationPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()
// Copy the response body to the file
_, err = io.Copy(file, response.Body)
if err != nil {
return fmt.Errorf("failed to save kubeconfig file: %w", err)
}
log.Printf("Kubeconfig file downloaded successfully")
} else {
log.Printf("Kubeconfig file already exists")
}
return nil
}