github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/lib/smartops/helpers.go (about) 1 package smartopsLib 2 3 import ( 4 "github.com/taubyte/go-project-schema/project" 5 "github.com/taubyte/go-project-schema/smartops" 6 structureSpec "github.com/taubyte/go-specs/structure" 7 applicationLib "github.com/taubyte/tau-cli/lib/application" 8 "github.com/taubyte/utils/id" 9 ) 10 11 type getter struct { 12 project project.Project 13 application string 14 smartops smartops.SmartOps 15 } 16 17 func get(name string) (info getter, err error) { 18 info.project, info.application, err = applicationLib.SelectedProjectAndApp() 19 if err != nil { 20 return 21 } 22 23 info.smartops, err = info.project.SmartOps(name, info.application) 24 if err != nil { 25 return 26 } 27 28 return 29 } 30 31 func list() (project project.Project, application string, smartops []string, err error) { 32 project, application, err = applicationLib.SelectedProjectAndApp() 33 if err != nil { 34 return 35 } 36 37 local, global := project.Get().SmartOps(application) 38 if len(application) > 0 { 39 smartops = local 40 } else { 41 smartops = global 42 } 43 44 return 45 } 46 47 func set(smartops *structureSpec.SmartOp, new bool) (info getter, err error) { 48 info, err = get(smartops.Name) 49 if err != nil { 50 return 51 } 52 53 if new { 54 smartops.Id = id.Generate(info.project.Get().Id(), smartops.Name) 55 } 56 57 err = info.smartops.SetWithStruct(true, smartops) 58 if err != nil { 59 return 60 } 61 62 return 63 }