-
Notifications
You must be signed in to change notification settings - Fork 23
/
filemeta.go
115 lines (106 loc) · 3.84 KB
/
filemeta.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
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
package cmd
import (
"os"
"strconv"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
// filemetaCmd represents file meta command
var filemetaCmd = &cobra.Command{
Use: "meta",
Short: "get meta data of files from blobbers",
Long: `get meta data of files from blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if fflags.Changed("remotepath") == false && fflags.Changed("authticket") == false {
PrintError("Error: remotepath / authticket flag is missing")
os.Exit(1)
}
remotepath := cmd.Flag("remotepath").Value.String()
authticket := cmd.Flag("authticket").Value.String()
lookuphash := cmd.Flag("lookuphash").Value.String()
doJSON, _ := cmd.Flags().GetBool("json")
if len(remotepath) == 0 && (len(authticket) == 0) {
PrintError("Error: remotepath / authticket / lookuphash flag is missing")
os.Exit(1)
}
if len(remotepath) > 0 {
if fflags.Changed("allocation") == false { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and return
}
allocationID := cmd.Flag("allocation").Value.String()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
remotepath := cmd.Flag("remotepath").Value.String()
ref, err := allocationObj.GetFileMeta(remotepath)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
if doJSON {
util.PrintJSON(ref)
} else {
header := []string{"Type", "Name", "Path", "Lookup Hash"}
data := make([][]string, 1)
data[0] = []string{ref.Type, ref.Name, ref.Path, ref.LookupHash}
if ref.Type == fileref.FILE {
headerFile := []string{"Size", "Mime Type", "Hash"}
dataFile := []string{strconv.FormatInt(ref.Size, 10), ref.MimeType, ref.Hash}
header = append(header, headerFile...)
data[0] = append(data[0], dataFile...)
}
util.WriteTable(os.Stdout, header, []string{}, data)
}
} else if len(authticket) > 0 {
allocationObj, err := sdk.GetAllocationFromAuthTicket(authticket)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
at := sdk.InitAuthTicket(authticket)
if len(lookuphash) == 0 {
lookuphash, err = at.GetLookupHash()
if err != nil {
PrintError("Error getting the lookuphash from authticket", err)
os.Exit(1)
}
}
ref, err := allocationObj.GetFileMetaFromAuthTicket(authticket, lookuphash)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
if doJSON {
util.PrintJSON(ref)
} else {
header := []string{"Type", "Name", "Lookup Hash"}
data := make([][]string, 1)
data[0] = []string{ref.Type, ref.Name, ref.LookupHash}
if ref.Type == fileref.FILE {
headerFile := []string{"Size", "Mime Type", "Hash"}
dataFile := []string{strconv.FormatInt(ref.Size, 10), ref.MimeType, ref.Hash}
header = append(header, headerFile...)
data[0] = append(data[0], dataFile...)
}
util.WriteTable(os.Stdout, header, []string{}, data)
}
}
return
},
}
func init() {
rootCmd.AddCommand(filemetaCmd)
filemetaCmd.PersistentFlags().String("allocation", "", "Allocation ID")
filemetaCmd.PersistentFlags().String("remotepath", "", "Remote path to list from")
filemetaCmd.PersistentFlags().String("authticket", "", "Auth ticket fot the file to download if you dont own it")
filemetaCmd.PersistentFlags().String("lookuphash", "", "The remote lookuphash of the object retrieved from the list")
filemetaCmd.Flags().Bool("json", false, "(default false) pass this option to print response as json data")
filemetaCmd.MarkFlagRequired("allocation")
}