@@ -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,53 @@ 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+ daprDirPath , err := standalone .GetDaprRuntimePath (opts .RuntimePath )
79+ if err != nil {
80+ return nil , err
81+ }
82+
83+ proc .ResourcePaths = []string {standalone .GetDaprComponentsPath (daprDirPath )}
6884 }
6985
70- comps , err := loader .NewLocalLoader (appID , proc .ResourcePaths ).Load (ctx )
86+ comps , err := loader .NewLocalLoader (opts . AppID , proc .ResourcePaths ).Load (ctx )
7187 if err != nil {
7288 return nil , err
7389 }
7490
75- c , err := clientFromComponents (comps , appID , strconv .Itoa (proc .GRPCPort ))
91+ c , err := clientFromComponents (comps , opts . AppID , strconv .Itoa (proc .GRPCPort ))
7692 if err != nil {
7793 return nil , err
7894 }
@@ -81,22 +97,22 @@ func stand(ctx context.Context, appID string) (*Client, error) {
8197 return c , nil
8298}
8399
84- func kube (namespace string , appID string ) (* Client , error ) {
85- list , err := kubernetes .List (namespace )
100+ func kube (opts Options ) (* Client , error ) {
101+ list , err := kubernetes .List (opts . Namespace )
86102 if err != nil {
87103 return nil , err
88104 }
89105
90106 var pod * kubernetes.ListOutput
91107 for _ , p := range list {
92- if p .AppID == appID {
108+ if p .AppID == opts . AppID {
93109 pod = & p
94110 break
95111 }
96112 }
97113
98114 if pod == nil {
99- return nil , fmt .Errorf ("Dapr app with id '%s' not found in namespace %s" , appID , namespace )
115+ return nil , fmt .Errorf ("Dapr app with id '%s' not found in namespace %s" , opts . AppID , opts . Namespace )
100116 }
101117
102118 config , _ , err := kubernetes .GetKubeConfigClient ()
@@ -111,7 +127,7 @@ func kube(namespace string, appID string) (*Client, error) {
111127
112128 portForward , err := kubernetes .NewPortForward (
113129 config ,
114- namespace ,
130+ opts . Namespace ,
115131 pod .PodName ,
116132 "localhost" ,
117133 port ,
@@ -136,7 +152,7 @@ func kube(namespace string, appID string) (*Client, error) {
136152 return nil , err
137153 }
138154
139- c , err := clientFromComponents (comps .Items , appID , pod .DaprGRPCPort )
155+ c , err := clientFromComponents (comps .Items , opts . AppID , pod .DaprGRPCPort )
140156 if err != nil {
141157 portForward .Stop ()
142158 }
0 commit comments