88 "fmt"
99 "io"
1010 "net/http"
11+ "net/url"
1112 "os"
12- "regexp"
1313 "strings"
1414
1515 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
@@ -23,9 +23,8 @@ import (
2323
2424// Useful to capture output in tests
2525var (
26- cliOut io.Writer = os .Stdout
27- cliIn io.Reader = os .Stdin
28- importPattern = regexp .MustCompile (`(?ms)^\s*(import\s+[^;]+;\s*)(.*)$` )
26+ cliOut io.Writer = os .Stdout
27+ cliIn io.Reader = os .Stdin
2928)
3029
3130func prettifyJson (in []byte ) []byte {
@@ -57,6 +56,48 @@ func outputApiResponse(res *http.Response, okStatus int) error {
5756 })
5857}
5958
59+ type stringFlagAware interface {
60+ GetStringFlagValue (string ) string
61+ }
62+
63+ // Extracts the project key and worker key from the command context. If the project key is not provided, it will be taken from the manifest.
64+ // There workerKey could either be the first argument or the name in the manifest.
65+ // The first argument will only be considered as the workerKey if total arguments are greater than minArgument.
66+ func extractProjectAndKeyFromCommandContext (c stringFlagAware , args []string , minArguments int , onlyGeneric bool ) (string , string , error ) {
67+ var workerKey string
68+
69+ projectKey := c .GetStringFlagValue (model .FlagProjectKey )
70+
71+ if len (args ) > 0 && len (args ) > minArguments {
72+ workerKey = args [0 ]
73+ }
74+
75+ if workerKey == "" || projectKey == "" {
76+ manifest , err := model .ReadManifest ()
77+ if err != nil {
78+ return "" , "" , err
79+ }
80+
81+ if err = manifest .Validate (); err != nil {
82+ return "" , "" , err
83+ }
84+
85+ if onlyGeneric && manifest .Action != "GENERIC_EVENT" {
86+ return "" , "" , fmt .Errorf ("only the GENERIC_EVENT actions are executable. Got %s" , manifest .Action )
87+ }
88+
89+ if workerKey == "" {
90+ workerKey = manifest .Name
91+ }
92+
93+ if projectKey == "" {
94+ projectKey = manifest .ProjectKey
95+ }
96+ }
97+
98+ return workerKey , projectKey , nil
99+ }
100+
60101func discardApiResponse (res * http.Response , okStatus int ) error {
61102 return processApiResponse (res , func (content []byte , statusCode int ) error {
62103 var err error
@@ -93,13 +134,26 @@ func processApiResponse(res *http.Response, doWithContent func(content []byte, s
93134 return doWithContent (responseBytes , res .StatusCode )
94135}
95136
96- func callWorkerApi (c * components.Context , serverUrl string , serverToken string , method string , body []byte , api ... string ) (* http.Response , func (), error ) {
137+ func callWorkerApi (c * components.Context , serverUrl string , serverToken string , method string , body []byte , queryParams map [ string ] string , api ... string ) (* http.Response , func (), error ) {
97138 timeout , err := model .GetTimeoutParameter (c )
98139 if err != nil {
99140 return nil , nil , err
100141 }
101142
102- url := fmt .Sprintf ("%sworker/api/v1/%s" , utils .AddTrailingSlashIfNeeded (serverUrl ), strings .Join (api , "/" ))
143+ apiEndpoint := fmt .Sprintf ("%sworker/api/v1/%s" , utils .AddTrailingSlashIfNeeded (serverUrl ), strings .Join (api , "/" ))
144+
145+ if queryParams != nil {
146+ var query string
147+ for key , value := range queryParams {
148+ if query != "" {
149+ query += "&"
150+ }
151+ query += fmt .Sprintf ("%s=%s" , key , url .QueryEscape (value ))
152+ }
153+ if query != "" {
154+ apiEndpoint += "?" + query
155+ }
156+ }
103157
104158 reqCtx , cancelReq := context .WithTimeout (context .Background (), timeout )
105159
@@ -108,7 +162,7 @@ func callWorkerApi(c *components.Context, serverUrl string, serverToken string,
108162 bodyReader = bytes .NewBuffer (body )
109163 }
110164
111- req , err := http .NewRequestWithContext (reqCtx , method , url , bodyReader )
165+ req , err := http .NewRequestWithContext (reqCtx , method , apiEndpoint , bodyReader )
112166 if err != nil {
113167 return nil , cancelReq , err
114168 }
@@ -128,8 +182,8 @@ func callWorkerApi(c *components.Context, serverUrl string, serverToken string,
128182 return res , cancelReq , nil
129183}
130184
131- func callWorkerApiWithOutput (c * components.Context , serverUrl string , serverToken string , method string , body []byte , okStatus int , api ... string ) error {
132- res , discardReq , err := callWorkerApi (c , serverUrl , serverToken , method , body , api ... )
185+ func callWorkerApiWithOutput (c * components.Context , serverUrl string , serverToken string , method string , body []byte , okStatus int , queryParams map [ string ] string , api ... string ) error {
186+ res , discardReq , err := callWorkerApi (c , serverUrl , serverToken , method , body , queryParams , api ... )
133187 if discardReq != nil {
134188 defer discardReq ()
135189 }
@@ -139,8 +193,8 @@ func callWorkerApiWithOutput(c *components.Context, serverUrl string, serverToke
139193 return outputApiResponse (res , okStatus )
140194}
141195
142- func callWorkerApiSilent (c * components.Context , serverUrl string , serverToken string , method string , body []byte , okStatus int , api ... string ) error {
143- res , discardReq , err := callWorkerApi (c , serverUrl , serverToken , method , body , api ... )
196+ func callWorkerApiSilent (c * components.Context , serverUrl string , serverToken string , method string , body []byte , okStatus int , queryParams map [ string ] string , api ... string ) error {
197+ res , discardReq , err := callWorkerApi (c , serverUrl , serverToken , method , body , queryParams , api ... )
144198 if discardReq != nil {
145199 defer discardReq ()
146200 }
@@ -151,8 +205,13 @@ func callWorkerApiSilent(c *components.Context, serverUrl string, serverToken st
151205}
152206
153207// fetchWorkerDetails Fetch a worker by its name. Returns nil if the worker does not exist (statusCode=404). Any other statusCode other than 200 will result as an error.
154- func fetchWorkerDetails (c * components.Context , serverUrl string , accessToken string , workerKey string ) (* model.WorkerDetails , error ) {
155- res , discardReq , err := callWorkerApi (c , serverUrl , accessToken , http .MethodGet , nil , "workers" , workerKey )
208+ func fetchWorkerDetails (c * components.Context , serverUrl string , accessToken string , workerKey string , projectKey string ) (* model.WorkerDetails , error ) {
209+ queryParams := make (map [string ]string )
210+ if projectKey != "" {
211+ queryParams ["projectKey" ] = projectKey
212+ }
213+
214+ res , discardReq , err := callWorkerApi (c , serverUrl , accessToken , http .MethodGet , nil , queryParams , "workers" , workerKey )
156215 if discardReq != nil {
157216 defer discardReq ()
158217 }
@@ -212,13 +271,3 @@ func prepareSecretsUpdate(mf *model.Manifest, existingWorker *model.WorkerDetail
212271
213272 return secrets
214273}
215-
216- func cleanImports (source string ) string {
217- out := source
218- match := importPattern .FindAllStringSubmatch (out , - 1 )
219- for len (match ) == 1 && len (match [0 ]) == 3 {
220- out = match [0 ][2 ]
221- match = importPattern .FindAllStringSubmatch (out , - 1 )
222- }
223- return out
224- }
0 commit comments