-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (131 loc) · 5.12 KB
/
Copy pathmain.go
File metadata and controls
154 lines (131 loc) · 5.12 KB
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
144
145
146
147
148
149
150
151
152
153
154
// File: penguindex-go/main.go
package main
import (
"flag"
"fmt"
"os"
"github.com/jendermine/penguindex-go/internal/auth"
"github.com/jendermine/penguindex-go/internal/commands"
"github.com/jendermine/penguindex-go/internal/config"
"github.com/fatih/color" // For colored output
"golang.org/x/term" // For PIN input
)
// !!! REPLACE THESE WITH YOUR ACTUAL VALUES !!!
const EMBEDDED_BUNDLE_URL = "https://gist.githubusercontent.com/jendermine/f963de2bcf12c37421277d7702466b2b/raw/ceabd48a9f0f6412a1dd42af44f20b5619d04d6d/log.json"
const TELEGRAM_CHAT_ID_URL = "https://gist.githubusercontent.com/jendermine/66015cce5cf15c0e04ba5987cb3ca342/raw/2e0f17aaee25abbcfa8a254f390bcb214775826b/log2.json"
func main() {
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}
// Color setup (optional)
errorColor := color.New(color.FgRed).SprintfFunc()
successColor := color.New(color.FgGreen).SprintfFunc()
infoColor := color.New(color.FgYellow).SprintfFunc()
fmt.Println(infoColor("Fetching configuration..."))
appConfigDetails, err := config.FetchRemoteConfigDetails(EMBEDDED_BUNDLE_URL, TELEGRAM_CHAT_ID_URL)
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Error fetching remote configuration: %v", err))
os.Exit(1)
}
fmt.Print("Enter PIN: ")
pinBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Error reading PIN: %v", err))
os.Exit(1)
}
pin := string(pinBytes)
fmt.Println() // Newline after PIN input
decryptedBundle, err := config.DecryptBundle(appConfigDetails.EncryptedBundleHex, pin)
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Error decrypting bundle (check PIN or bundle URL): %v", err))
os.Exit(1)
}
fmt.Println(successColor("Bundle decrypted successfully."))
appCfg := &config.AppConfig{
ServiceAccountJSON: decryptedBundle.ServiceAccountJSONString,
TelegramBotToken: decryptedBundle.TelegramBotToken,
TelegramChatID: appConfigDetails.TelegramChatID,
DefaultFolderID: config.DEFAULT_TEST_FOLDER_ID, // From config package
}
fmt.Println(infoColor("Authenticating with Google Drive..."))
driveHTTPClient, err := auth.GetAuthenticatedClient(appCfg.ServiceAccountJSON)
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Google Drive authentication failed: %v", err))
os.Exit(1)
}
driveService, err := auth.NewDriveService(driveHTTPClient)
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Failed to create Google Drive service: %v", err))
os.Exit(1)
}
// Perform an auth check
gDriveUser, err := driveService.About.Get().Fields("user").Do()
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Failed to verify Drive service authentication: %v", err))
os.Exit(1)
}
fmt.Println(successColor("Successfully authenticated with Google Drive as: %s", gDriveUser.User.EmailAddress))
command := os.Args[1]
args := os.Args[2:]
switch command {
case "upload":
uploadCmd := flag.NewFlagSet("upload", flag.ExitOnError)
filePath := uploadCmd.String("file", "", "Path to the file to upload (required)")
folderID := uploadCmd.String("folder", "", "Google Drive folder ID (optional, uses default if not provided)")
uploadCmd.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s upload -file <filepath> [-folder <folderID>]\n", os.Args[0])
uploadCmd.PrintDefaults()
}
if err := uploadCmd.Parse(args); err != nil {
fmt.Fprintln(os.Stderr, errorColor("Error parsing upload flags: %v", err))
os.Exit(1)
}
if *filePath == "" {
fmt.Fprintln(os.Stderr, errorColor("Error: --file flag is required for upload."))
uploadCmd.Usage()
os.Exit(1)
}
actualFolderID := *folderID
if actualFolderID == "" {
actualFolderID = appCfg.DefaultFolderID
}
err := commands.HandleUpload(driveService, appCfg, *filePath, actualFolderID)
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Upload command failed: %v", err))
os.Exit(1)
}
fmt.Println(successColor("Upload command completed successfully."))
case "delete":
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
fileIDOrLink := deleteCmd.String("id", "", "File ID or Google Drive link to delete (required)")
deleteCmd.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s delete -id <fileID_or_link>\n", os.Args[0])
deleteCmd.PrintDefaults()
}
if err := deleteCmd.Parse(args); err != nil {
fmt.Fprintln(os.Stderr, errorColor("Error parsing delete flags: %v", err))
os.Exit(1)
}
if *fileIDOrLink == "" {
fmt.Fprintln(os.Stderr, errorColor("Error: --id flag is required for delete."))
deleteCmd.Usage()
os.Exit(1)
}
err := commands.HandleDelete(driveService, appCfg, *fileIDOrLink)
if err != nil {
fmt.Fprintln(os.Stderr, errorColor("Delete command failed: %v", err))
os.Exit(1)
}
fmt.Println(successColor("Delete command completed successfully."))
default:
fmt.Fprintln(os.Stderr, errorColor("Unknown command: %s", command))
printUsage()
os.Exit(1)
}
}
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s <command> [arguments]\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Available commands: upload, delete")
fmt.Fprintln(os.Stderr, "Use <command> -help for more information on a specific command.")
}