@@ -27,6 +27,13 @@ import (
2727 "github.com/dapr/kit/ptr"
2828)
2929
30+ type Options struct {
31+ KubernetesMode bool
32+ Namespace string
33+ AppID string
34+ RuntimePath string
35+ }
36+
3037type Client struct {
3138 Dapr client.Client
3239 Cancel context.CancelFunc
@@ -35,44 +42,54 @@ type Client struct {
3542 TableName * string
3643}
3744
38- func DaprClient (ctx context.Context , kubernetesMode bool , namespace , appID string ) (* Client , error ) {
45+ func DaprClient (ctx context.Context , opts Options ) (* Client , error ) {
3946 client .SetLogger (nil )
4047
4148 var client * Client
4249 var err error
43- if kubernetesMode {
44- client , err = kube (namespace , appID )
50+ if opts . KubernetesMode {
51+ client , err = kube (opts )
4552 } else {
46- client , err = stand (ctx , appID )
53+ client , err = stand (ctx , opts )
4754 }
4855
4956 return client , err
5057}
5158
52- func stand (ctx context.Context , appID string ) (* Client , error ) {
59+ func stand (ctx context.Context , opts Options ) (* Client , error ) {
5360 list , err := standalone .List ()
5461 if err != nil {
5562 return nil , err
5663 }
5764
5865 var proc * standalone.ListOutput
5966 for _ , c := range list {
60- if c .AppID == appID {
67+ if c .AppID == opts . AppID {
6168 proc = & c
6269 break
6370 }
6471 }
6572
6673 if proc == nil {
67- return nil , fmt .Errorf ("Dapr app with id '%s' not found" , appID )
74+ return nil , fmt .Errorf ("Dapr app with id '%s' not found" , opts .AppID )
75+ }
76+
77+ if len (proc .ResourcePaths ) == 0 {
78+ var daprDirPath string
79+ daprDirPath , err = standalone .GetDaprRuntimePath (opts .RuntimePath )
80+ if err != nil {
81+ return nil , err
82+ }
83+
84+ proc .ResourcePaths = []string {standalone .GetDaprComponentsPath (daprDirPath )}
6885 }
6986
70- comps , err := loader .NewLocalLoader (appID , proc .ResourcePaths ).Load (ctx )
87+ comps , err := loader .NewLocalLoader (opts . AppID , proc .ResourcePaths ).Load (ctx )
7188 if err != nil {
7289 return nil , err
7390 }
7491
75- c , err := clientFromComponents (comps , appID , strconv .Itoa (proc .GRPCPort ))
92+ c , err := clientFromComponents (comps , opts . AppID , strconv .Itoa (proc .GRPCPort ))
7693 if err != nil {
7794 return nil , err
7895 }
@@ -81,22 +98,22 @@ func stand(ctx context.Context, appID string) (*Client, error) {
8198 return c , nil
8299}
83100
84- func kube (namespace string , appID string ) (* Client , error ) {
85- list , err := kubernetes .List (namespace )
101+ func kube (opts Options ) (* Client , error ) {
102+ list , err := kubernetes .List (opts . Namespace )
86103 if err != nil {
87104 return nil , err
88105 }
89106
90107 var pod * kubernetes.ListOutput
91108 for _ , p := range list {
92- if p .AppID == appID {
109+ if p .AppID == opts . AppID {
93110 pod = & p
94111 break
95112 }
96113 }
97114
98115 if pod == nil {
99- return nil , fmt .Errorf ("Dapr app with id '%s' not found in namespace %s" , appID , namespace )
116+ return nil , fmt .Errorf ("Dapr app with id '%s' not found in namespace %s" , opts . AppID , opts . Namespace )
100117 }
101118
102119 config , _ , err := kubernetes .GetKubeConfigClient ()
@@ -111,7 +128,7 @@ func kube(namespace string, appID string) (*Client, error) {
111128
112129 portForward , err := kubernetes .NewPortForward (
113130 config ,
114- namespace ,
131+ opts . Namespace ,
115132 pod .PodName ,
116133 "localhost" ,
117134 port ,
@@ -136,7 +153,7 @@ func kube(namespace string, appID string) (*Client, error) {
136153 return nil , err
137154 }
138155
139- c , err := clientFromComponents (comps .Items , appID , pod .DaprGRPCPort )
156+ c , err := clientFromComponents (comps .Items , opts . AppID , pod .DaprGRPCPort )
140157 if err != nil {
141158 portForward .Stop ()
142159 }
0 commit comments