Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List file support #267

Merged
merged 10 commits into from
Jun 22, 2022
113 changes: 45 additions & 68 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,66 +38,33 @@ var listCmd = &cobra.Command{
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and os.Exit(1)
}

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.ListDir(remotepath)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
if doJSON {
util.PrintJSON(ref.Children)
return
}
header := []string{"Type", "Name", "Path", "Size", "Num Blocks", "Lookup Hash", "Is Encrypted", "Downloads payer"}
data := make([][]string, len(ref.Children))
for idx, child := range ref.Children {
size := strconv.FormatInt(child.Size, 10)
if child.Type == fileref.DIRECTORY {
size = ""
}
isEncrypted := ""
if child.Type == fileref.FILE {
if len(child.EncryptionKey) > 0 {
isEncrypted = "YES"
} else {
isEncrypted = "NO"
}
}
data[idx] = []string{
child.Type,
child.Name,
child.Path,
size,
strconv.FormatInt(child.NumBlocks, 10),
child.LookupHash,
isEncrypted,
child.Attributes.WhoPaysForReads.String(),
}
}
util.WriteTable(os.Stdout, header, []string{}, data)

printListDirResult(doJSON, ref)
} 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)
isDir, err := at.IsDir()
if isDir && len(lookuphash) == 0 {
lookuphash, err = at.GetLookupHash()
if err != nil {
PrintError("Error getting the lookuphash from authticket", err)
os.Exit(1)
}
}
if !isDir {
PrintError("Invalid operation. Auth ticket is not for a directory")
lookuphash, err = at.GetLookupHash()
if err != nil {
PrintError("Error getting the lookuphash from authticket", err)
os.Exit(1)
}

Expand All @@ -107,33 +74,7 @@ var listCmd = &cobra.Command{
os.Exit(1)
}

if doJSON {
util.PrintJSON(ref.Children)
return
}
header := []string{"Type", "Name", "Size", "Num Blocks", "Lookup Hash", "Is Encrypted", "Downloads payer"}
data := make([][]string, len(ref.Children))
for idx, child := range ref.Children {
size := strconv.FormatInt(child.Size, 10)
isEncrypted := ""
if child.Type == fileref.FILE {
if len(child.EncryptionKey) > 0 {
isEncrypted = "YES"
} else {
isEncrypted = "NO"
}
}
data[idx] = []string{
child.Type,
child.Name,
size,
strconv.FormatInt(child.NumBlocks, 10),
child.LookupHash,
isEncrypted,
child.Attributes.WhoPaysForReads.String(),
}
}
util.WriteTable(os.Stdout, header, []string{}, data)
printListDirResult(doJSON, ref)
}

return
Expand Down Expand Up @@ -203,3 +144,39 @@ func init() {
listAllCmd.PersistentFlags().String("allocation", "", "Allocation ID")
listAllCmd.MarkFlagRequired("allocation")
}

func printListDirResult(outJson bool, ref *sdk.ListResult) {
if outJson {
util.PrintJSON(ref.Children)
return
}

header := []string{"Type", "Name", "Path", "Size", "Num Blocks", "Lookup Hash", "Is Encrypted", "Downloads payer"}
data := make([][]string, len(ref.Children))
for idx, child := range ref.Children {
size := strconv.FormatInt(child.Size, 10)
if child.Type == fileref.DIRECTORY {
size = ""
}
isEncrypted := ""
if child.Type == fileref.FILE {
if len(child.EncryptionKey) > 0 {
isEncrypted = "YES"
} else {
isEncrypted = "NO"
}
}
data[idx] = []string{
child.Type,
child.Name,
child.Path,
size,
strconv.FormatInt(child.NumBlocks, 10),
child.LookupHash,
isEncrypted,
child.Attributes.WhoPaysForReads.String(),
}
}

util.WriteTable(os.Stdout, header, []string{}, data)
}