github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/handler/util.go (about) 1 package handler 2 3 import ( 4 "context" 5 6 pb "github.com/tickoalcantara12/micro/v3/proto/runtime" 7 "github.com/tickoalcantara12/micro/v3/service/runtime" 8 ) 9 10 func toProto(s *runtime.Service) *pb.Service { 11 return &pb.Service{ 12 Name: s.Name, 13 Version: s.Version, 14 Source: s.Source, 15 Metadata: s.Metadata, 16 Status: int32(s.Status), 17 } 18 } 19 20 func toService(s *pb.Service) *runtime.Service { 21 // add status to metadata to enable backwards compatability 22 md := s.Metadata 23 if md == nil { 24 md = map[string]string{"status": humanizeStatus(s.Status)} 25 } else { 26 md["status"] = humanizeStatus(s.Status) 27 } 28 29 return &runtime.Service{ 30 Name: s.Name, 31 Version: s.Version, 32 Source: s.Source, 33 Metadata: md, 34 Status: runtime.ServiceStatus(s.Status), 35 } 36 } 37 38 func humanizeStatus(status int32) string { 39 switch runtime.ServiceStatus(status) { 40 case runtime.Pending: 41 return "pending" 42 case runtime.Building: 43 return "building" 44 case runtime.Starting: 45 return "starting" 46 case runtime.Running: 47 return "running" 48 case runtime.Stopping: 49 return "stopping" 50 case runtime.Stopped: 51 return "stopped" 52 case runtime.Error: 53 return "error" 54 default: 55 return "unknown" 56 } 57 } 58 59 func toCreateOptions(ctx context.Context, opts *pb.CreateOptions) []runtime.CreateOption { 60 options := []runtime.CreateOption{ 61 runtime.CreateNamespace(opts.Namespace), 62 runtime.CreateEntrypoint(opts.Entrypoint), 63 runtime.WithForce(opts.Force), 64 } 65 66 // command options 67 if len(opts.Command) > 0 { 68 options = append(options, runtime.WithCommand(opts.Command...)) 69 } 70 71 // args for command 72 if len(opts.Args) > 0 { 73 options = append(options, runtime.WithArgs(opts.Args...)) 74 } 75 76 // env options 77 if len(opts.Env) > 0 { 78 options = append(options, runtime.WithEnv(opts.Env)) 79 } 80 81 // create specific type of service 82 if len(opts.Type) > 0 { 83 options = append(options, runtime.CreateType(opts.Type)) 84 } 85 86 // use specific image 87 if len(opts.Image) > 0 { 88 options = append(options, runtime.CreateImage(opts.Image)) 89 } 90 91 // inject the secrets 92 for k, v := range opts.Secrets { 93 options = append(options, runtime.WithSecret(k, v)) 94 } 95 96 // mount the volumes 97 for name, path := range opts.Volumes { 98 options = append(options, runtime.WithVolume(name, path)) 99 } 100 101 if opts.Instances > 0 { 102 options = append(options, runtime.CreateInstances(int(opts.Instances))) 103 } 104 105 // TODO: output options 106 107 return options 108 } 109 110 func toReadOptions(ctx context.Context, opts *pb.ReadOptions) []runtime.ReadOption { 111 options := []runtime.ReadOption{ 112 runtime.ReadNamespace(opts.Namespace), 113 } 114 115 if len(opts.Service) > 0 { 116 options = append(options, runtime.ReadService(opts.Service)) 117 } 118 if len(opts.Version) > 0 { 119 options = append(options, runtime.ReadVersion(opts.Version)) 120 } 121 if len(opts.Type) > 0 { 122 options = append(options, runtime.ReadType(opts.Type)) 123 } 124 125 return options 126 } 127 128 func toUpdateOptions(ctx context.Context, opts *pb.UpdateOptions) []runtime.UpdateOption { 129 return []runtime.UpdateOption{ 130 runtime.UpdateNamespace(opts.Namespace), 131 runtime.UpdateEntrypoint(opts.Entrypoint), 132 runtime.UpdateInstances(int(opts.Instances)), 133 } 134 } 135 136 func toDeleteOptions(ctx context.Context, opts *pb.DeleteOptions) []runtime.DeleteOption { 137 return []runtime.DeleteOption{ 138 runtime.DeleteNamespace(opts.Namespace), 139 } 140 } 141 142 func toLogsOptions(ctx context.Context, opts *pb.LogsOptions) []runtime.LogsOption { 143 return []runtime.LogsOption{ 144 runtime.LogsNamespace(opts.Namespace), 145 } 146 }