| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "syscall" |
| 9 | |
| 10 | "golang.org/x/term" |
| 11 | ) |
| 12 | |
| 13 | func runAuth(_ []string) { |
| 14 | cfg, _ := LoadClientConfig(ClientFlags{}) |
| 15 | reader := bufio.NewReader(os.Stdin) |
| 16 | |
| 17 | cfg.Endpoint = promptDefault(reader, "S3 endpoint (or 'r2' for Cloudflare R2)", cfg.Endpoint) |
| 18 | if strings.EqualFold(cfg.Endpoint, "r2") { |
| 19 | cfg.R2AccountID = promptDefault(reader, "R2 account ID", cfg.R2AccountID) |
| 20 | cfg.Endpoint = "https://" + cfg.R2AccountID + ".r2.cloudflarestorage.com" |
| 21 | } |
| 22 | |
| 23 | region := cfg.Region |
| 24 | if region == "" { |
| 25 | region = "auto" |
| 26 | } |
| 27 | cfg.Region = promptDefault(reader, "Region", region) |
| 28 | |
| 29 | cfg.AccessKeyID = promptDefault(reader, "Access key ID", cfg.AccessKeyID) |
| 30 | |
| 31 | fmt.Print("Secret access key (hidden): ") |
| 32 | secretBytes, err := term.ReadPassword(int(syscall.Stdin)) |
| 33 | fmt.Println() |
| 34 | if err != nil { |
| 35 | fmt.Fprintln(os.Stderr, "read secret:", err) |
| 36 | os.Exit(1) |
| 37 | } |
| 38 | if s := strings.TrimSpace(string(secretBytes)); s != "" { |
| 39 | cfg.SecretAccessKey = s |
| 40 | } |
| 41 | |
| 42 | cfg.DefaultBucket = promptDefault(reader, "Default bucket (optional)", cfg.DefaultBucket) |
| 43 | |
| 44 | if cfg.DefaultBucket != "" { |
| 45 | existing := "" |
| 46 | if cfg.PublicURLs != nil { |
| 47 | existing = cfg.PublicURLs[cfg.DefaultBucket] |
| 48 | } |
| 49 | pub := promptDefault(reader, "Public URL for "+cfg.DefaultBucket+" (optional)", existing) |
| 50 | if pub != "" { |
| 51 | if cfg.PublicURLs == nil { |
| 52 | cfg.PublicURLs = map[string]string{} |
| 53 | } |
| 54 | cfg.PublicURLs[cfg.DefaultBucket] = pub |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if cfg.PresignTTLSec <= 0 { |
| 59 | cfg.PresignTTLSec = 3600 |
| 60 | } |
| 61 | |
| 62 | if err := SaveClientConfig(cfg); err != nil { |
| 63 | fmt.Fprintln(os.Stderr, "save:", err) |
| 64 | os.Exit(1) |
| 65 | } |
| 66 | path, _ := clientConfigPath() |
| 67 | fmt.Println("Saved", path) |
| 68 | } |
| 69 | |
| 70 | func promptDefault(r *bufio.Reader, label, def string) string { |
| 71 | if def != "" { |
| 72 | fmt.Printf("%s [%s]: ", label, def) |
| 73 | } else { |
| 74 | fmt.Printf("%s: ", label) |
| 75 | } |
| 76 | line, _ := r.ReadString('\n') |
| 77 | line = strings.TrimSpace(line) |
| 78 | if line == "" { |
| 79 | return def |
| 80 | } |
| 81 | return line |
| 82 | } |