github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/lib/storage/helpers.go (about) 1 package storageLib 2 3 import ( 4 "strings" 5 "time" 6 7 "github.com/taubyte/go-project-schema/project" 8 "github.com/taubyte/go-project-schema/storages" 9 structureSpec "github.com/taubyte/go-specs/structure" 10 applicationLib "github.com/taubyte/tau-cli/lib/application" 11 "github.com/taubyte/utils/id" 12 ) 13 14 type getter struct { 15 project project.Project 16 application string 17 storage storages.Storage 18 } 19 20 func get(name string) (info getter, err error) { 21 info.project, info.application, err = applicationLib.SelectedProjectAndApp() 22 if err != nil { 23 return 24 } 25 26 info.storage, err = info.project.Storage(name, info.application) 27 if err != nil { 28 return 29 } 30 31 return 32 } 33 34 func list() (project project.Project, application string, storages []string, err error) { 35 project, application, err = applicationLib.SelectedProjectAndApp() 36 if err != nil { 37 return 38 } 39 40 local, global := project.Get().Storages(application) 41 if len(application) > 0 { 42 storages = local 43 } else { 44 storages = global 45 } 46 47 return 48 } 49 50 func set(storage *structureSpec.Storage, new bool) error { 51 info, err := get(storage.Name) 52 if err != nil { 53 return err 54 } 55 56 storage.Type = strings.ToLower(storage.Type) 57 58 if new { 59 storage.Id = id.Generate(info.project.Get().Id(), storage.Name) 60 } else if info.storage.Get().Type() != storage.Type { 61 err = info.storage.Delete(info.storage.Get().Type()) 62 if err != nil { 63 return err 64 } 65 } 66 67 return info.storage.SetWithStruct(true, storage) 68 } 69 70 func ShortDur(d time.Duration) string { 71 s := d.String() 72 if strings.HasSuffix(s, "m0s") { 73 s = s[:len(s)-2] 74 } 75 if strings.HasSuffix(s, "h0m") { 76 s = s[:len(s)-2] 77 } 78 return s 79 }