-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwww.go
More file actions
238 lines (211 loc) · 6.05 KB
/
www.go
File metadata and controls
238 lines (211 loc) · 6.05 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"encoding/json"
"fmt"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net/http"
"os"
"time"
)
var (
debug bool
tokenFile string
)
type RandomUserResponse struct {
Results []struct {
Gender string `json:"gender"`
Name struct {
Title string `json:"title"`
First string `json:"first"`
Last string `json:"last"`
} `json:"name"`
Email string `json:"email"`
} `json:"results"`
}
var rootCmd = &cobra.Command{
Use: "yourapp",
Short: "Your application description",
//Run: handleToken,
}
var tokenCmd = &cobra.Command{
Use: "token",
Short: "Handle myapp token",
Run: func(cmd *cobra.Command, args []string) {
handleToken(cmd, args)
fmt.Println("mytest")
fmt.Println(tokenFile)
},
}
var myappCmd = &cobra.Command{
Use: "myapp",
Short: "Handle myapp token",
Run: func(cmd *cobra.Command, args []string) {
// handleToken(cmd, args)
fmt.Println("myappCmd")
},
}
var randomUserNameCMD = &cobra.Command{
Use: "randomusername",
Short: "Handle Random User Name",
Run: func(cmd *cobra.Command, args []string) {
handleToken(cmd, args)
// Make a GET request to the Random User Generator API
response, err := http.Get("https://randomuser.me/api/")
if err != nil {
fmt.Println("Error fetching random user:", err)
return
}
defer response.Body.Close()
// Decode the JSON response
var randomUserResponse RandomUserResponse
if err := json.NewDecoder(response.Body).Decode(&randomUserResponse); err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
// Display the random user details
if len(randomUserResponse.Results) > 0 {
user := randomUserResponse.Results[0]
fmt.Println("Random User:")
fmt.Println("Name:", user.Name.Title, user.Name.First, user.Name.Last)
fmt.Println("Gender:", user.Gender)
fmt.Println("Email:", user.Email)
} else {
fmt.Println("No results found")
}
},
}
var randomUserEmailCmd = &cobra.Command{
Use: "randomuseremail",
Short: "Handle Random User Email",
Run: func(cmd *cobra.Command, args []string) {
handleToken(cmd, args)
fmt.Println("randomuseremail")
},
}
var randomUserLocationCmd = &cobra.Command{
Use: "randomuserlocation",
Short: "Handle RandomUserLocation",
Run: func(cmd *cobra.Command, args []string) {
handleToken(cmd, args)
fmt.Println("randomuserlocation")
},
}
var randomuserpictureCmd = &cobra.Command{
Use: "randomuserpicture",
Short: "Handle UserPicture",
Run: func(cmd *cobra.Command, args []string) {
handleToken(cmd, args)
fmt.Println("randomuserpicture")
},
}
func init() {
rootCmd.AddCommand(tokenCmd)
tokenCmd.Flags().StringVar(&tokenFile, "token-file", "", "Path to the token file")
rootCmd.AddCommand(randomUserNameCMD)
rootCmd.AddCommand(randomUserEmailCmd)
rootCmd.AddCommand(randomUserLocationCmd)
rootCmd.AddCommand(randomuserpictureCmd)
viper.SetConfigName(".config") // Name of config file (without extension)
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("$HOME") // Path to look for the config file in
}
func main() {
fmt.Println(os.Getenv("HOME"))
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func handleToken(cmd *cobra.Command, args []string) {
configFilePath := fmt.Sprintf("%s/.config.json", os.Getenv("HOME"))
// Check if config file exists, if not, create it
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
fmt.Println("Config file not found; creating a new one.")
file, err := os.Create(configFilePath)
if err != nil {
fmt.Println("Error creating config file:", err)
return
}
file.Close()
}
// Attempt to read the config file
err := viper.ReadInConfig()
fmt.Println(err)
// if err != nil {
// fmt.Println("Error reading config file:", err)
// return
// }
// Check if token exists
token := viper.GetString("myapp_token")
expireStr := viper.GetString("expire_date")
var expireDate time.Time
fmt.Println("==========")
fmt.Println("===========")
fmt.Println(token)
fmt.Println("=============")
fmt.Println("==============")
if token == "" || isExpired(expireStr) {
/*
if expireStr != "" {
expireDate, err = time.Parse(time.RFC3339, expireStr)
if err != nil {
fmt.Println("Error parsing expire date:", err)
return
}
}
if token != "" && expireDate.After(time.Now()) {
fmt.Println("Token already exists and is valid:", token)
return
}
*/
fmt.Println(token)
fmt.Println(expireStr)
// Prompt user for a new token
prompt := promptui.Prompt{
Label: "Please enter the new myapp token",
}
newToken, err := prompt.Run()
if err != nil {
fmt.Println("Prompt failed:", err)
return
}
// Set expiration time (e.g., 1 hour from now)
expireDate = time.Now().Add(time.Hour)
// Write the new token and expiration date to the config file
viper.Set("myapp_token", newToken)
viper.Set("expire_date", expireDate.Format(time.RFC3339))
viper.Set("RESOURCE_NAME", "APP-DYNAMIC-myapp-RESOOURE")
user_name := os.Getenv("USERNAME")
fmt.Println(user_name)
username, ok := os.LookupEnv("USERNAME")
fmt.Println(username)
if !ok {
fmt.Println("USERNAME environment variable not found")
return
}
viper.Set("CLIENT_ID", username)
viper.Set("myapp_URL", "https://myapp-prod.com")
err = viper.WriteConfig()
if err != nil {
fmt.Println("Error writing config file:", err)
return
}
fmt.Println("New token written to config file.")
} else {
// Token and expiration date already exist
fmt.Println("Token and expiration date already exist in config file.")
fmt.Println("Token:", token)
fmt.Println("Expiration Date:", expireStr)
}
}
func isExpired(expireStr string) bool {
expireTime, err := time.Parse(time.RFC3339, expireStr)
if err != nil {
fmt.Println("Error parsing expiration date:", err)
return true
}
// Check if expiration date is within 57 minutes from now
return time.Until(expireTime) < (57 * time.Minute)
}