-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshare.go
78 lines (69 loc) · 2.19 KB
/
share.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/encloud-tech/encloud/pkg/api"
"github.com/encloud-tech/encloud/pkg/types"
thirdparty "github.com/encloud-tech/encloud/third_party"
"github.com/spf13/cobra"
)
func ShareCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "share",
Short: "Share content",
Long: `Share your files with other users using the UUID and DEK`,
Run: func(cmd *cobra.Command, args []string) {
kek := ""
privateKey := ""
publicKey, _ := cmd.Flags().GetString("pubkey")
pk, _ := cmd.Flags().GetString("privkey")
uuid, _ := cmd.Flags().GetString("uuid")
email, _ := cmd.Flags().GetString("email")
readPublicKeyFromPath, _ := cmd.Flags().GetBool("read_pub_from_path")
readPrivateKeyFromPath, _ := cmd.Flags().GetBool("read_priv_from_path")
if readPublicKeyFromPath {
kek = thirdparty.ReadKeyFile(publicKey)
} else {
kek = publicKey
}
if readPrivateKeyFromPath {
privateKey = thirdparty.ReadKeyFile(pk)
} else {
privateKey = pk
}
fileMetaData, err := api.Share(uuid, kek, privateKey, email)
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
response := types.RetrieveByUUIDContentResponse{
Status: "success",
StatusCode: http.StatusFound,
Message: "Content shared successfully.",
Data: fileMetaData,
}
encoded, err := json.MarshalIndent(response, "", " ")
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
fmt.Fprintf(cmd.OutOrStdout(), string(encoded))
},
}
cmd.Flags().StringP("pubkey", "p", "", "KEK public key")
cmd.Flags().StringP("privkey", "k", "", "KEK private key")
cmd.Flags().StringP("uuid", "u", "", "UUID of file to retrieve")
cmd.Flags().StringP("email", "e", "", "Email to share file with")
cmd.Flags().BoolP("read_pub_from_path", "r", false, "Allows to read KEK public key from path")
cmd.Flags().BoolP("read_priv_from_path", "o", false, "Allows to read KEK private key from path")
cmd.MarkFlagRequired("pubkey")
cmd.MarkFlagRequired("privkey")
cmd.MarkFlagRequired("uuid")
cmd.MarkFlagRequired("email")
return cmd
}
func init() {
RootCmd.AddCommand(ShareCmd())
}