gitlab.com/SkynetLabs/skyd@v1.6.9/cmd/skyc/skynetcmd_helpers.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "os" 8 "strings" 9 "text/tabwriter" 10 11 "gitlab.com/NebulousLabs/errors" 12 "gitlab.com/SkynetLabs/skyd/skymodules" 13 "go.sia.tech/siad/modules" 14 ) 15 16 // fileData is a small helper for reading and returning the data from a file. 17 func fileData(filename string) []byte { 18 data, err := ioutil.ReadFile(filename) 19 if err != nil { 20 die(err) 21 } 22 return data 23 } 24 25 // printSkynetDirs is a helper for printing skynet directoryInfos 26 func printSkynetDirs(dirs []directoryInfo, recursive bool) error { 27 for _, dir := range dirs { 28 // Don't print directories that have 0 skyfiles if they are outside the 29 // skynet folder. We print directories that are within the skynet folder 30 // that have 0 skyfiles because a user would expect to see the skynet 31 // folder structure they have created. If they want to see the folder 32 // structure they have created outside of the Skynet Folder then they should 33 // use the renter ls command. 34 if !skymodules.IsSkynetDir(dir.dir.SiaPath) && dir.dir.SkynetFiles == 0 { 35 continue 36 } 37 38 // Initialize a tab writer for the diretory 39 w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) 40 41 // Print the directory SiaPath 42 fmt.Fprintf(w, "%v/", dir.dir.SiaPath) 43 44 // In Skyfile only directories, NumFiles is equal to SkynetFiles 45 omitted := dir.dir.NumFiles - dir.dir.SkynetFiles 46 if omitted > 0 { 47 fmt.Fprintf(w, "\t(%v omitted)", omitted) 48 } 49 fmt.Fprintln(w) 50 51 // Print subdirs. 52 for _, subDir := range dir.subDirs { 53 // Don't print directories that have 0 skyfiles if they are outside the 54 // skynet folder. 55 if !skymodules.IsSkynetDir(subDir.SiaPath) && dir.dir.SkynetFiles == 0 { 56 continue 57 } 58 subDirName := subDir.SiaPath.Name() + "/" 59 sizeUnits := modules.FilesizeUnits(subDir.AggregateSkynetSize) 60 fmt.Fprintf(w, " %v\t\t%9v\n", subDirName, sizeUnits) 61 } 62 63 // Print skyfiles if the directory contains any. This is for printing only 64 // skyfiles from directories that are outside the Skynet Folder. 65 if dir.dir.SkynetFiles != 0 { 66 printSkyFiles(w, dir.files) 67 } 68 fmt.Fprintln(w) 69 70 // Flush the writer 71 if err := w.Flush(); err != nil { 72 return errors.AddContext(err, "failed to flush writer") 73 } 74 75 // Check if this was a recursive request. 76 if !recursive { 77 // If not recursive, finish early after the first dir. 78 return nil 79 } 80 } 81 return nil 82 } 83 84 // printSkyFiles is a helper for printing out Skyfile information 85 func printSkyFiles(w io.Writer, files []skymodules.FileInfo) { 86 fmt.Fprintf(w, " Filename\tSkylink\tFilesize\n") 87 for _, file := range files { 88 // Skip any non skyfiles 89 if len(file.Skylinks) == 0 { 90 continue 91 } 92 // Print Skyfile 93 name := file.SiaPath.Name() 94 firstSkylink := file.Skylinks[0] 95 size := modules.FilesizeUnits(file.Filesize) 96 fmt.Fprintf(w, " %v\t%v\t%9v\n", name, firstSkylink, size) 97 for _, skylink := range file.Skylinks[1:] { 98 fmt.Fprintf(w, "\t%v\t\n", skylink) 99 } 100 } 101 } 102 103 // sanitizeSkylinks will trim away `sia://` from skylinks 104 func sanitizeSkylinks(links []string) []string { 105 var result []string 106 107 for _, link := range links { 108 trimmed := strings.TrimPrefix(link, "sia://") 109 result = append(result, trimmed) 110 } 111 112 return result 113 } 114 115 // smallSkyfileDownload is a helper that downloads a small skyfile and returns 116 // the file data, layout, and metadata. 117 func smallSkyfileDownload(skylink string) (fileData []byte, sl skymodules.SkyfileLayout, sm []byte, _ error) { 118 // Download the baseSector and parse the SkyfileLayout and 119 // SkyfileMetadata from it. 120 reader, err := httpClient.SkynetBaseSectorGet(skylink) 121 if err != nil { 122 return nil, sl, sm, errors.AddContext(err, "unable to download basesector") 123 } 124 baseSector, err := ioutil.ReadAll(reader) 125 if err != nil { 126 return nil, sl, sm, errors.AddContext(err, "unable to reader basesector data from reader") 127 } 128 sl, _, _, sm, fileData, err = skymodules.ParseSkyfileMetadata(baseSector) 129 if err != nil { 130 return nil, sl, sm, errors.AddContext(err, "unable to parse layout and metadata from basesector") 131 } 132 return fileData, sl, sm, nil 133 }