-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle dockercompat inspect for devcontainers
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
- Loading branch information
1 parent
5a47eec
commit 90ce0b0
Showing
6 changed files
with
133 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package main denotes the entry point of finch CLI. | ||
// TODO: Remove all instances of these calls once supported upstream | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/runfinch/finch/pkg/command" | ||
) | ||
|
||
// nerdctl outputs multiple id inspect call as multiple arrays of json which is not parsable by json. | ||
// | ||
// Helper function helps to separate out into individual json array and a single json is reconstructed. | ||
func extractOuterArrays(input string) []string { | ||
var arrays []string | ||
var bracketCount, start int | ||
|
||
for i, char := range input { | ||
switch char { | ||
case '[': | ||
if bracketCount == 0 { | ||
start = i | ||
} | ||
bracketCount++ | ||
case ']': | ||
bracketCount-- | ||
if bracketCount == 0 { | ||
arrays = append(arrays, input[start:i+1]) | ||
} | ||
} | ||
} | ||
|
||
return arrays | ||
} | ||
|
||
func prettyPrintJSON(input string) { | ||
jsonArrays := extractOuterArrays(input) | ||
var mergedData []map[string]interface{} | ||
|
||
for i, jsonArray := range jsonArrays { | ||
var parsedArray []map[string]interface{} | ||
err := json.Unmarshal([]byte(jsonArray), &parsedArray) | ||
if err != nil { | ||
logrus.Error("Error parsing JSON at index: ", i, err) | ||
continue | ||
} | ||
|
||
if len(parsedArray) == 0 { | ||
continue | ||
} | ||
|
||
// its always is a single object | ||
firstObject := parsedArray[0] | ||
_, ok := firstObject["Config"].(map[string]interface{}) | ||
if ok { | ||
if image, ok := firstObject["Image"].(string); ok { | ||
if config, ok := firstObject["Config"].(map[string]interface{}); ok { | ||
config["Image"] = image | ||
} | ||
} | ||
} | ||
|
||
if state, ok := firstObject["State"].(map[string]interface{}); ok { | ||
state["StartedAt"] = "0001-01-01T00:00:00Z" | ||
} | ||
|
||
_, ok = firstObject["NetworkSettings"].(map[string]interface{}) | ||
if !ok { | ||
firstObject["NetworkSettings"] = map[string]interface{}{ | ||
"Ports": map[string]interface{}{}, | ||
} | ||
} | ||
mergedData = append(mergedData, firstObject) | ||
} | ||
|
||
finalJSON, err := json.MarshalIndent(mergedData, "", " ") | ||
if err != nil { | ||
logrus.Error("Error marshaling final JSON: ", err) | ||
return | ||
} | ||
|
||
fmt.Println(string(finalJSON)) | ||
} | ||
|
||
func inspectContainerOutputHandler(cmd command.Command) error { | ||
var stdoutBuf bytes.Buffer | ||
cmd.SetStdout(&stdoutBuf) | ||
|
||
err := cmd.Run() | ||
|
||
prettyPrintJSON(stdoutBuf.String()) | ||
return err | ||
} |
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