-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add --follow param to service log command
- Loading branch information
1 parent
afdb2f6
commit 5b1ba94
Showing
15 changed files
with
270 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package serviceLogs | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func parseResponseByFormat(jsonData Response, format, formatTemplate, mode string) error { | ||
var err error | ||
|
||
logs := jsonData.Items | ||
if mode == RESPONSE { | ||
logs = reverseLogs(logs) | ||
} | ||
|
||
if format == FULL { | ||
if formatTemplate != "" { | ||
if err = getFullWithTemplate(logs, formatTemplate); err != nil { | ||
return err | ||
} | ||
return nil | ||
} else { | ||
// TODO get rfc from config when implemented as flag | ||
getFullByRfc(logs, RFC5424) | ||
return nil | ||
} | ||
} else if format == SHORT { | ||
for _, o := range logs { | ||
fmt.Printf("%v %s \n", o.Timestamp, o.Content) | ||
} | ||
} else if format == JSONSTREAM { | ||
for _, o := range logs { | ||
val, err := json.Marshal(o) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(val)) | ||
} | ||
} else { | ||
val, err := json.Marshal(logs) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(val)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// reverseLogs makes log order ASC to get the last logs of given limit | ||
func reverseLogs(data []Data) []Data { | ||
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { | ||
data[i], data[j] = data[j], data[i] | ||
} | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package serviceLogs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func (h *Handler) printLogs(ctx context.Context, config RunConfig, inputs InputValues, containerId, logServiceId, projectId string) error { | ||
method, url, err := h.getServiceLogResData(ctx, h.sdkConfig, projectId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
query := makeQueryParams(inputs, logServiceId, containerId) | ||
|
||
if inputs.mode == RESPONSE { | ||
err = getLogs(ctx, method, HTTPS+url+query, inputs.format, inputs.formatTemplate, inputs.mode) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if inputs.mode == STREAM { | ||
wsUrl := getWsUrl(url) | ||
err := h.getLogStream(ctx, config, inputs, wsUrl, query, containerId, logServiceId, projectId) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func makeQueryParams(inputs InputValues, logServiceId, containerId string) string { | ||
query := fmt.Sprintf("&limit=%d&desc=%d&facility=%d&serviceStackId=%s", | ||
inputs.limit, getDesc(inputs.mode), inputs.facility, logServiceId) | ||
|
||
if inputs.minSeverity != -1 { | ||
query += fmt.Sprintf("&minimumSeverity=%d", inputs.minSeverity) | ||
} | ||
|
||
if containerId != "" { | ||
query += fmt.Sprintf("&containerId=%s", containerId) | ||
} | ||
|
||
return query | ||
} | ||
|
||
func getDesc(mode string) int { | ||
if mode == RESPONSE { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
func getWsUrl(apiUrl string) string { | ||
urlSplit := strings.Split(apiUrl, "?") | ||
url, token := urlSplit[0], urlSplit[1] | ||
return url + "/stream?" + token | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.