-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.go
108 lines (75 loc) · 2.16 KB
/
methods.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
package main
import (
firebase "firebase.google.com/go/v4"
"google.golang.org/api/option"
"os"
"strings"
"github.com/pterm/pterm"
)
type File struct {
Name string
Data []byte
FolderName string
}
func cloudMethod() (File, error) {
credentialsFile, err := os.ReadFile("./serviceAccountKey.json")
if err != nil {
return File{}, err
}
storageBucket, err := getEnv("ST_BUCKET")
var config = &firebase.Config{
StorageBucket: storageBucket,
}
var opt = option.WithCredentialsJSON(credentialsFile)
firebaseApp, err := firebase.NewApp(ctx, config, opt)
if err != nil {
return File{}, err
}
client, err := firebaseApp.Storage(ctx)
if err != nil {
return File{}, err
}
worlds, err := getWorldList(client)
if err != nil {
return File{}, err
}
pterm.DefaultSection.Println("📚 Listado de archivos")
pterm.Println()
var fileName string
fileName, _ = pterm.DefaultInteractiveSelect.WithDefaultText("-> Seleccione el mundo").
WithOptions(worlds).
Show()
// fmt.Println("------ 🏷 Ingrese el nombre del archivo a descargar: ------")
// fmt.Printf("Introduce comillas dobles para seleccionar el archivo: %c %c\n", rune(34), rune(34))
// fmt.Scanf("%q", &fileName)
fileData, err := downloadWorld(client, fileName)
folderName := strings.Split(fileName, "-")[0]
if _, err = os.Stat(folderName); !os.IsNotExist(err) {
pterm.Info.Println("El directorio ya existe, este se sobreescribirá")
os.RemoveAll(folderName)
}
file := File{
Name: fileName,
Data: fileData,
FolderName: folderName,
}
return file, nil
}
func directMethod() (File, error) {
var url string
url, err := pterm.DefaultInteractiveTextInput.WithDefaultText("📚 Introduzca la URL").WithTextStyle(pterm.NewStyle(pterm.FgLightYellow)).Show()
if err != nil {
return File{}, err
}
file, err := downloadWorldFromUrl(url)
if err != nil {
return File{}, err
}
pterm.Println()
pterm.Success.Println("Mundo " + file.Name + " descargandose correctamente")
if _, err = os.Stat(file.FolderName); !os.IsNotExist(err) {
pterm.Info.Println("El directorio ya existe, este se sobreescribirá")
os.RemoveAll(file.FolderName)
}
return file, nil
}