|
1 |
| -package main |
| 1 | +package main |
2 | 2 |
|
3 |
| -import package ( |
4 |
| - libb/keycard_link.go |
5 |
| - libb/ipfs_link.go |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
6 | 6 |
|
| 7 | + "yourproject/src/libb/keycard_link" |
| 8 | + "yourproject/src/libb/ipfs_link" |
| 9 | +) |
7 | 10 |
|
8 |
| - ) |
| 11 | +func main() { |
| 12 | + checkDependencies() |
| 13 | + // Example: Parsing command-line arguments |
| 14 | + // You might want to use a more robust way for parsing arguments (like the `flag` package) |
| 15 | + if len(os.Args) < 2 { |
| 16 | + fmt.Println("Usage: main.go [command]") |
| 17 | + os.Exit(1) |
| 18 | + } |
| 19 | + |
| 20 | + command := os.Args[1] |
| 21 | + |
| 22 | + switch command { |
| 23 | + case "encrypt": |
| 24 | + if len(os.Args) != 3 { |
| 25 | + fmt.Println("Usage: main.go encrypt [filename]") |
| 26 | + os.Exit(1) |
| 27 | + } |
| 28 | + filename := os.Args[2] |
| 29 | + err := encryptFile(filename) |
| 30 | + if err != nil { |
| 31 | + fmt.Printf("Error encrypting file: %s\n", err) |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + case "decrypt": |
| 36 | + if len(os.Args) != 3 { |
| 37 | + fmt.Println("Usage: main.go decrypt [filename]") |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + filename := os.Args[2] |
| 41 | + err := decryptFile(filename) |
| 42 | + if err != nil { |
| 43 | + fmt.Printf("Error decrypting file: %s\n", err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + default: |
| 48 | + fmt.Println("Invalid command") |
| 49 | + os.Exit(1) |
| 50 | + } |
| 51 | +} |
| 52 | +func askUserYN(question string) bool { |
| 53 | + reader := bufio.NewReader(os.Stdin) |
| 54 | + for { |
| 55 | + fmt.Printf("%s (y/n): ", question) |
| 56 | + response, err := reader.ReadString('\n') |
| 57 | + if err != nil { |
| 58 | + fmt.Println("Error reading response. Please try again.") |
| 59 | + continue |
| 60 | + } |
| 61 | + response = strings.TrimSpace(strings.ToLower(response)) |
| 62 | + |
| 63 | + if response == "y" || response == "yes" { |
| 64 | + return true |
| 65 | + } else if response == "n" || response == "no" { |
| 66 | + return false |
| 67 | + } else { |
| 68 | + fmt.Println("Invalid response. Please answer 'y' for yes or 'n' for no.") |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func checkAndInstallDependencies() error { |
| 74 | + dependencies := []string{"git", "ipfs"} |
| 75 | + |
| 76 | + for _, dep := range dependencies { |
| 77 | + _, err := exec.LookPath(dep) |
| 78 | + if err != nil { |
| 79 | + if askUserYN(fmt.Sprintf("'%s' is not installed. Do you want to install it?", dep)) { |
| 80 | + err := installDependency(dep) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("error installing %s: %w", dep, err) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func installDependency(dep string) error { |
| 91 | + // Implement the logic to install the dependency. |
| 92 | + // This is an example using apt-get, adjust according to your system's package manager. |
| 93 | + cmd := exec.Command("sudo", "pacman","-S", dep) |
| 94 | + cmd.Stdout = os.Stdout |
| 95 | + cmd.Stderr = os.Stderr |
| 96 | + return cmd.Run() |
| 97 | +} |
| 98 | + |
| 99 | +func encryptFile(filename string) error { |
| 100 | + encryptedFilePath, err := keycard_link.EncryptFile(filename) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("error encrypting file: %w", err) |
| 103 | + } |
| 104 | + fmt.Printf("File encrypted successfully: %s\n", encryptedFilePath) |
| 105 | + |
| 106 | + if askUserYN("Do you want to upload the file to IPFS? [y/n]: ") { |
| 107 | + cid, err := ipfs_link.AddFileToIPFS(encryptedFilePath) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("error uploading file to IPFS: %w", err) |
| 110 | + } |
| 111 | + fmt.Printf("File uploaded to IPFS with CID: %s\n", cid) |
| 112 | + } |
| 113 | + |
| 114 | + return nil // Return nil if successful, or an error if something goes wrong |
| 115 | +} |
| 116 | + |
| 117 | +func decryptFile(filename string) error { |
| 118 | + // Retrieve the file from IPFS using ipfs_link library |
| 119 | + err := ipfs_link.GetFileFromIPFS(/* ... */) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("error retrieving file from IPFS: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + decryptedData, err := keycard_link.DecryptFile(/* ... */) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("error decrypting file: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + // Save the decrypted data to a file |
| 130 | + // ... |
| 131 | + |
| 132 | + return nil // Return nil if successful, or an error if something goes wrong |
| 133 | +} |
9 | 134 |
|
10 |
| -func main(){ |
11 | 135 |
|
12 | 136 |
|
13 |
| - } |
14 | 137 |
|
15 |
| -func etc etc...... |
|
0 commit comments