diff options
-rw-r--r-- | cmd/podman/containers/ps.go | 3 | ||||
-rw-r--r-- | cmd/podman/parse/json.go | 2 | ||||
-rw-r--r-- | cmd/podman/parse/json_test.go | 2 | ||||
-rw-r--r-- | cmd/podman/system/df.go | 111 | ||||
-rw-r--r-- | cmd/podman/system/events.go | 74 | ||||
-rw-r--r-- | cmd/podman/system/info.go | 21 | ||||
-rw-r--r-- | cmd/podman/system/version.go | 50 | ||||
-rw-r--r-- | cmd/podman/volumes/inspect.go | 21 | ||||
-rw-r--r-- | pkg/api/handlers/compat/events.go | 15 | ||||
-rw-r--r-- | test/e2e/build_test.go | 1 | ||||
-rw-r--r-- | test/e2e/events_test.go | 26 | ||||
-rw-r--r-- | vendor/github.com/containers/buildah/pkg/formats/formats.go | 167 | ||||
-rw-r--r-- | vendor/github.com/containers/buildah/pkg/formats/templates.go | 78 | ||||
-rw-r--r-- | vendor/modules.txt | 1 |
14 files changed, 163 insertions, 409 deletions
diff --git a/cmd/podman/containers/ps.go b/cmd/podman/containers/ps.go index 8082a74c2..65bfc97da 100644 --- a/cmd/podman/containers/ps.go +++ b/cmd/podman/containers/ps.go @@ -11,7 +11,6 @@ import ( "time" tm "github.com/buger/goterm" - "github.com/containers/buildah/pkg/formats" "github.com/containers/podman/v2/cmd/podman/parse" "github.com/containers/podman/v2/cmd/podman/registry" "github.com/containers/podman/v2/cmd/podman/report" @@ -93,7 +92,7 @@ func checkFlags(c *cobra.Command) error { if listOpts.Size || listOpts.Namespace { return errors.Errorf("quiet conflicts with size and namespace") } - if c.Flag("format").Changed && listOpts.Format != formats.JSONString { + if c.Flag("format").Changed && !parse.MatchesJSONFormat(listOpts.Format) { // Quiet is overridden by Go template output. listOpts.Quiet = false } diff --git a/cmd/podman/parse/json.go b/cmd/podman/parse/json.go index 40ac415db..d7486d0b1 100644 --- a/cmd/podman/parse/json.go +++ b/cmd/podman/parse/json.go @@ -2,7 +2,7 @@ package parse import "regexp" -var jsonFormatRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*( \.)?\s*}})\s*$`) +var jsonFormatRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*(\.)?\s*}})\s*$`) // MatchesJSONFormat test CLI --format string to be a JSON request func MatchesJSONFormat(s string) bool { diff --git a/cmd/podman/parse/json_test.go b/cmd/podman/parse/json_test.go index ec3b5664b..b3b29c93c 100644 --- a/cmd/podman/parse/json_test.go +++ b/cmd/podman/parse/json_test.go @@ -27,7 +27,7 @@ func TestMatchesJSONFormat(t *testing.T) { {"json . }}", false}, {"{{.ID }} json .", false}, {"json .", false}, - {"{{json.}}", false}, + {"{{json.}}", true}, } for _, tt := range tests { diff --git a/cmd/podman/system/df.go b/cmd/podman/system/df.go index b262c8478..da7bbed02 100644 --- a/cmd/podman/system/df.go +++ b/cmd/podman/system/df.go @@ -2,15 +2,14 @@ package system import ( "fmt" - "io" "os" - "strconv" "strings" "text/tabwriter" "text/template" "time" "github.com/containers/podman/v2/cmd/podman/registry" + "github.com/containers/podman/v2/cmd/podman/report" "github.com/containers/podman/v2/cmd/podman/validate" "github.com/containers/podman/v2/pkg/domain/entities" "github.com/docker/go-units" @@ -52,35 +51,21 @@ func df(cmd *cobra.Command, args []string) error { if err != nil { return err } + + w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + if dfOptions.Verbose { - return printVerbose(reports) + return printVerbose(cmd, w, reports) } - return printSummary(reports, dfOptions.Format) + return printSummary(w, cmd, reports) } -func printSummary(reports *entities.SystemDfReport, userFormat string) error { - +func printSummary(w *tabwriter.Writer, cmd *cobra.Command, reports *entities.SystemDfReport) error { var ( dfSummaries []*dfSummary active int size, reclaimable int64 - format = "{{.Type}}\t{{.Total}}\t{{.Active}}\t{{.Size}}\t{{.Reclaimable}}\n" - w io.Writer = os.Stdout ) - - // Images - if len(userFormat) > 0 { - if !strings.HasSuffix(userFormat, `\n`) { - userFormat += `\n` - } - // should be Unquoto from cmd line - userFormat, err := strconv.Unquote(`"` + userFormat + `"`) - if err != nil { - return err - } - format = userFormat - } - for _, i := range reports.Images { if i.Containers > 0 { active++ @@ -90,7 +75,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error { reclaimable += i.Size } } - imageSummary := dfSummary{ Type: "Images", Total: len(reports.Images), @@ -101,7 +85,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error { dfSummaries = append(dfSummaries, &imageSummary) // Containers - var ( conActive int conSize, conReclaimable int64 @@ -114,7 +97,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error { } conSize += c.RWSize } - containerSummary := dfSummary{ Type: "Containers", Total: len(reports.Containers), @@ -122,7 +104,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error { size: conSize, reclaimable: conReclaimable, } - dfSummaries = append(dfSummaries, &containerSummary) // Volumes @@ -143,78 +124,94 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error { size: volumesSize, reclaimable: volumesReclaimable, } - dfSummaries = append(dfSummaries, &volumeSummary) - headers := "TYPE\tTOTAL\tACTIVE\tSIZE\tRECLAIMABLE\n" - format = "{{range . }}" + format + "{{end}}" - if len(userFormat) == 0 { - format = headers + format + // need to give un-exported fields + hdrs := report.Headers(dfSummary{}, map[string]string{ + "Size": "SIZE", + "Reclaimable": "RECLAIMABLE", + }) + + row := "{{.Type}}\t{{.Total}}\t{{.Active}}\t{{.Size}}\t{{.Reclaimable}}\n" + if cmd.Flags().Changed("format") { + row = report.NormalizeFormat(dfOptions.Format) } - return writeTemplate(w, format, dfSummaries) + row = "{{range . }}" + row + "{{end}}" + + return writeTemplate(cmd, w, hdrs, row, dfSummaries) } -func printVerbose(reports *entities.SystemDfReport) error { - var ( - w io.Writer = os.Stdout - ) +func printVerbose(cmd *cobra.Command, w *tabwriter.Writer, reports *entities.SystemDfReport) error { + defer w.Flush() // Images - fmt.Print("\nImages space usage:\n\n") + fmt.Fprint(w, "Images space usage:\n\n") // convert to dfImage for output dfImages := make([]*dfImage, 0, len(reports.Images)) for _, d := range reports.Images { dfImages = append(dfImages, &dfImage{SystemDfImageReport: d}) } - imageHeaders := "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tSIZE\tSHARED SIZE\tUNIQUE SIZE\tCONTAINERS\n" + hdrs := report.Headers(entities.SystemDfImageReport{}, map[string]string{ + "ImageID": "IMAGE ID", + "SharedSize": "SHARED SIZE", + "UniqueSize": "UNIQUE SIZE", + }) imageRow := "{{.Repository}}\t{{.Tag}}\t{{.ImageID}}\t{{.Created}}\t{{.Size}}\t{{.SharedSize}}\t{{.UniqueSize}}\t{{.Containers}}\n" - format := imageHeaders + "{{range . }}" + imageRow + "{{end}}" - if err := writeTemplate(w, format, dfImages); err != nil { + format := "{{range . }}" + imageRow + "{{end}}" + if err := writeTemplate(cmd, w, hdrs, format, dfImages); err != nil { return nil } // Containers - fmt.Print("\nContainers space usage:\n\n") + fmt.Fprint(w, "\nContainers space usage:\n\n") // convert to dfContainers for output dfContainers := make([]*dfContainer, 0, len(reports.Containers)) for _, d := range reports.Containers { dfContainers = append(dfContainers, &dfContainer{SystemDfContainerReport: d}) } - containerHeaders := "CONTAINER ID\tIMAGE\tCOMMAND\tLOCAL VOLUMES\tSIZE\tCREATED\tSTATUS\tNAMES\n" + hdrs = report.Headers(entities.SystemDfContainerReport{}, map[string]string{ + "ContainerID": "CONTAINER ID", + "LocalVolumes": "LOCAL VOLUMES", + "RWSize": "SIZE", + }) containerRow := "{{.ContainerID}}\t{{.Image}}\t{{.Command}}\t{{.LocalVolumes}}\t{{.RWSize}}\t{{.Created}}\t{{.Status}}\t{{.Names}}\n" - format = containerHeaders + "{{range . }}" + containerRow + "{{end}}" - if err := writeTemplate(w, format, dfContainers); err != nil { + format = "{{range . }}" + containerRow + "{{end}}" + if err := writeTemplate(cmd, w, hdrs, format, dfContainers); err != nil { return nil } // Volumes - fmt.Print("\nLocal Volumes space usage:\n\n") + fmt.Fprint(w, "\nLocal Volumes space usage:\n\n") dfVolumes := make([]*dfVolume, 0, len(reports.Volumes)) // convert to dfVolume for output for _, d := range reports.Volumes { dfVolumes = append(dfVolumes, &dfVolume{SystemDfVolumeReport: d}) } - volumeHeaders := "VOLUME NAME\tLINKS\tSIZE\n" + hdrs = report.Headers(entities.SystemDfVolumeReport{}, map[string]string{ + "VolumeName": "VOLUME NAME", + }) volumeRow := "{{.VolumeName}}\t{{.Links}}\t{{.Size}}\n" - format = volumeHeaders + "{{range . }}" + volumeRow + "{{end}}" - return writeTemplate(w, format, dfVolumes) + format = "{{range . }}" + volumeRow + "{{end}}" + return writeTemplate(cmd, w, hdrs, format, dfVolumes) } -func writeTemplate(w io.Writer, format string, output interface{}) error { - tmpl, err := template.New("dfout").Parse(format) +func writeTemplate(cmd *cobra.Command, w *tabwriter.Writer, hdrs []map[string]string, format string, + output interface{}) error { + defer w.Flush() + + tmpl, err := template.New("df").Parse(format) if err != nil { return err } - w = tabwriter.NewWriter(w, 8, 2, 2, ' ', 0) //nolint - if err := tmpl.Execute(w, output); err != nil { - return err - } - if flusher, ok := w.(interface{ Flush() error }); ok { - return flusher.Flush() + + if !cmd.Flags().Changed("format") { + if err := tmpl.Execute(w, hdrs); err != nil { + return err + } } - return nil + return tmpl.Execute(w, output) } type dfImage struct { diff --git a/cmd/podman/system/events.go b/cmd/podman/system/events.go index 04e948f30..aaf572873 100644 --- a/cmd/podman/system/events.go +++ b/cmd/podman/system/events.go @@ -1,13 +1,12 @@ package system import ( - "bufio" "context" + "fmt" "os" - "strings" "text/template" - "github.com/containers/buildah/pkg/formats" + "github.com/containers/podman/v2/cmd/podman/parse" "github.com/containers/podman/v2/cmd/podman/registry" "github.com/containers/podman/v2/cmd/podman/validate" "github.com/containers/podman/v2/libpod/events" @@ -28,6 +27,7 @@ var ( RunE: eventsCmd, Example: `podman events podman events --filter event=create + podman events --format {{.Image}} podman events --since 1h30s`, } ) @@ -51,60 +51,54 @@ func init() { _ = flags.MarkHidden("stream") } -func eventsCmd(cmd *cobra.Command, args []string) error { - var ( - err error - eventsError error - tmpl *template.Template - ) - if strings.Join(strings.Fields(eventFormat), "") == "{{json.}}" { - eventFormat = formats.JSONString - } - if eventFormat != formats.JSONString { - tmpl, err = template.New("events").Parse(eventFormat) - if err != nil { - return err - } - } +func eventsCmd(cmd *cobra.Command, _ []string) error { if len(eventOptions.Since) > 0 || len(eventOptions.Until) > 0 { eventOptions.FromStart = true } - eventChannel := make(chan *events.Event) + eventChannel := make(chan *events.Event, 1) eventOptions.EventChan = eventChannel + errChannel := make(chan error) + + var ( + tmpl *template.Template + doJSON bool + ) + + if cmd.Flags().Changed("format") { + doJSON = parse.MatchesJSONFormat(eventFormat) + if !doJSON { + var err error + tmpl, err = template.New("events").Parse(eventFormat) + if err != nil { + return err + } + } + } go func() { - eventsError = registry.ContainerEngine().Events(context.Background(), eventOptions) + err := registry.ContainerEngine().Events(context.Background(), eventOptions) + errChannel <- err }() - if eventsError != nil { - return eventsError - } - w := bufio.NewWriter(os.Stdout) for event := range eventChannel { switch { - case eventFormat == formats.JSONString: + case event == nil: + // no-op + case doJSON: jsonStr, err := event.ToJSONString() if err != nil { return errors.Wrapf(err, "unable to format json") } - if _, err := w.Write([]byte(jsonStr)); err != nil { - return err - } - case len(eventFormat) > 0: - if err := tmpl.Execute(w, event); err != nil { + fmt.Println(jsonStr) + case cmd.Flags().Changed("format"): + if err := tmpl.Execute(os.Stdout, event); err != nil { return err } + fmt.Println("") default: - if _, err := w.Write([]byte(event.ToHumanReadable())); err != nil { - return err - } - } - if _, err := w.Write([]byte("\n")); err != nil { - return err - } - if err := w.Flush(); err != nil { - return err + fmt.Println(event.ToHumanReadable()) } } - return nil + + return <-errChannel } diff --git a/cmd/podman/system/info.go b/cmd/podman/system/info.go index 3e3c99488..ee720abf8 100644 --- a/cmd/podman/system/info.go +++ b/cmd/podman/system/info.go @@ -69,26 +69,25 @@ func info(cmd *cobra.Command, args []string) error { return err } - if parse.MatchesJSONFormat(inFormat) { + switch { + case parse.MatchesJSONFormat(inFormat): b, err := json.MarshalIndent(info, "", " ") if err != nil { return err } fmt.Println(string(b)) - return nil - } - if !cmd.Flag("format").Changed { + case cmd.Flags().Changed("format"): + tmpl, err := template.New("info").Parse(inFormat) + if err != nil { + return err + } + return tmpl.Execute(os.Stdout, info) + default: b, err := yaml.Marshal(info) if err != nil { return err } fmt.Println(string(b)) - return nil - } - tmpl, err := template.New("info").Parse(inFormat) - if err != nil { - return err } - err = tmpl.Execute(os.Stdout, info) - return err + return nil } diff --git a/cmd/podman/system/version.go b/cmd/podman/system/version.go index 9da7da54a..4f47c5fba 100644 --- a/cmd/podman/system/version.go +++ b/cmd/podman/system/version.go @@ -6,10 +6,11 @@ import ( "os" "strings" "text/tabwriter" + "text/template" - "github.com/containers/buildah/pkg/formats" "github.com/containers/podman/v2/cmd/podman/parse" "github.com/containers/podman/v2/cmd/podman/registry" + "github.com/containers/podman/v2/cmd/podman/report" "github.com/containers/podman/v2/cmd/podman/validate" "github.com/containers/podman/v2/libpod/define" "github.com/containers/podman/v2/pkg/domain/entities" @@ -41,31 +42,38 @@ func version(cmd *cobra.Command, args []string) error { return err } - switch { - case parse.MatchesJSONFormat(versionFormat): + if parse.MatchesJSONFormat(versionFormat) { s, err := json.MarshalToString(versions) if err != nil { return err } - _, err = io.WriteString(os.Stdout, s+"\n") - return err - case cmd.Flag("format").Changed: - out := formats.StdoutTemplate{Output: versions, Template: versionFormat} - err := out.Out() + fmt.Println(s) + return nil + } + + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) + defer w.Flush() + + if cmd.Flag("format").Changed { + row := report.NormalizeFormat(versionFormat) + tmpl, err := template.New("version 2.0.0").Parse(row) if err != nil { + return err + } + if err := tmpl.Execute(w, versions); err != nil { // On Failure, assume user is using older version of podman version --format and check client - versionFormat = strings.Replace(versionFormat, ".Server.", ".", 1) - out = formats.StdoutTemplate{Output: versions.Client, Template: versionFormat} - if err1 := out.Out(); err1 != nil { + row = strings.Replace(row, ".Server.", ".", 1) + tmpl, err := template.New("version 1.0.0").Parse(row) + if err != nil { + return err + } + if err := tmpl.Execute(w, versions.Client); err != nil { return err } } return nil } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) - defer w.Flush() - if versions.Server != nil { if _, err := fmt.Fprintf(w, "Client:\n"); err != nil { return err @@ -81,13 +89,13 @@ func version(cmd *cobra.Command, args []string) error { return nil } -func formatVersion(writer io.Writer, version *define.Version) { - fmt.Fprintf(writer, "Version:\t%s\n", version.Version) - fmt.Fprintf(writer, "API Version:\t%s\n", version.APIVersion) - fmt.Fprintf(writer, "Go Version:\t%s\n", version.GoVersion) +func formatVersion(w io.Writer, version *define.Version) { + fmt.Fprintf(w, "Version:\t%s\n", version.Version) + fmt.Fprintf(w, "API Version:\t%s\n", version.APIVersion) + fmt.Fprintf(w, "Go Version:\t%s\n", version.GoVersion) if version.GitCommit != "" { - fmt.Fprintf(writer, "Git Commit:\t%s\n", version.GitCommit) + fmt.Fprintf(w, "Git Commit:\t%s\n", version.GitCommit) } - fmt.Fprintf(writer, "Built:\t%s\n", version.BuiltTime) - fmt.Fprintf(writer, "OS/Arch:\t%s\n", version.OsArch) + fmt.Fprintf(w, "Built:\t%s\n", version.BuiltTime) + fmt.Fprintf(w, "OS/Arch:\t%s\n", version.OsArch) } diff --git a/cmd/podman/volumes/inspect.go b/cmd/podman/volumes/inspect.go index ce24ac4e5..8d1350228 100644 --- a/cmd/podman/volumes/inspect.go +++ b/cmd/podman/volumes/inspect.go @@ -3,11 +3,11 @@ package volumes import ( "fmt" "os" - "strings" "text/template" - "github.com/containers/buildah/pkg/formats" + "github.com/containers/podman/v2/cmd/podman/parse" "github.com/containers/podman/v2/cmd/podman/registry" + "github.com/containers/podman/v2/cmd/podman/report" "github.com/containers/podman/v2/pkg/domain/entities" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -53,26 +53,21 @@ func inspect(cmd *cobra.Command, args []string) error { if err != nil { return err } - switch inspectFormat { - case "", formats.JSONString: + + switch { + case parse.MatchesJSONFormat(inspectFormat), inspectFormat == "": jsonOut, err := json.MarshalIndent(responses, "", " ") if err != nil { return errors.Wrapf(err, "error marshalling inspect JSON") } fmt.Println(string(jsonOut)) default: - if !strings.HasSuffix(inspectFormat, "\n") { - inspectFormat += "\n" - } - format := "{{range . }}" + inspectFormat + "{{end}}" - tmpl, err := template.New("volumeInspect").Parse(format) + row := "{{range . }}" + report.NormalizeFormat(inspectFormat) + "{{end}}" + tmpl, err := template.New("volumeInspect").Parse(row) if err != nil { return err } - if err := tmpl.Execute(os.Stdout, responses); err != nil { - return err - } + return tmpl.Execute(os.Stdout, responses) } return nil - } diff --git a/pkg/api/handlers/compat/events.go b/pkg/api/handlers/compat/events.go index a729b84d4..f74491a8f 100644 --- a/pkg/api/handlers/compat/events.go +++ b/pkg/api/handlers/compat/events.go @@ -112,11 +112,15 @@ func GetEvents(w http.ResponseWriter, r *http.Request) { errorChannel <- runtime.Events(r.Context(), readOpts) }() - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) + var flush = func() {} if flusher, ok := w.(http.Flusher); ok { - flusher.Flush() + flush = flusher.Flush } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + flush() + coder := json.NewEncoder(w) coder.SetEscapeHTML(true) @@ -124,6 +128,7 @@ func GetEvents(w http.ResponseWriter, r *http.Request) { select { case err := <-errorChannel: if err != nil { + // FIXME StatusOK already sent above cannot send 500 here utils.InternalServerError(w, err) return } @@ -136,9 +141,7 @@ func GetEvents(w http.ResponseWriter, r *http.Request) { if err := coder.Encode(e); err != nil { logrus.Errorf("unable to write json: %q", err) } - if flusher, ok := w.(http.Flusher); ok { - flusher.Flush() - } + flush() case <-r.Context().Done(): return } diff --git a/test/e2e/build_test.go b/test/e2e/build_test.go index 5155bcbc7..572e55fe5 100644 --- a/test/e2e/build_test.go +++ b/test/e2e/build_test.go @@ -220,7 +220,6 @@ var _ = Describe("Podman build", func() { }) It("podman build --http_proxy flag", func() { - SkipIfRemote("FIXME: This is broken should be fixed") // This is hanging currently. os.Setenv("http_proxy", "1.2.3.4") if IsRemote() { podmanTest.StopRemoteService() diff --git a/test/e2e/events_test.go b/test/e2e/events_test.go index bea8caa93..b37bd584e 100644 --- a/test/e2e/events_test.go +++ b/test/e2e/events_test.go @@ -10,6 +10,7 @@ import ( . "github.com/containers/podman/v2/test/utils" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gexec" ) var _ = Describe("Podman events", func() { @@ -126,26 +127,31 @@ var _ = Describe("Podman events", func() { SkipIfNotFedora() _, ec, _ := podmanTest.RunLsContainer("") Expect(ec).To(Equal(0)) + test := podmanTest.Podman([]string{"events", "--stream=false", "--format", "json"}) test.WaitWithDefaultTimeout() - Expect(test.ExitCode()).To(BeZero()) + Expect(test).To(Exit(0)) + jsonArr := test.OutputToStringArray() - Expect(len(jsonArr)).To(Not(BeZero())) + Expect(test.OutputToStringArray()).ShouldNot(BeEmpty()) + eventsMap := make(map[string]string) err := json.Unmarshal([]byte(jsonArr[0]), &eventsMap) - Expect(err).To(BeNil()) - _, exist := eventsMap["Status"] - Expect(exist).To(BeTrue()) + Expect(err).ToNot(HaveOccurred()) + + Expect(eventsMap).To(HaveKey("Status")) test = podmanTest.Podman([]string{"events", "--stream=false", "--format", "{{json.}}"}) test.WaitWithDefaultTimeout() - Expect(test.ExitCode()).To(BeZero()) + Expect(test).To(Exit(0)) + jsonArr = test.OutputToStringArray() - Expect(len(jsonArr)).To(Not(BeZero())) + Expect(test.OutputToStringArray()).ShouldNot(BeEmpty()) + eventsMap = make(map[string]string) err = json.Unmarshal([]byte(jsonArr[0]), &eventsMap) - Expect(err).To(BeNil()) - _, exist = eventsMap["Status"] - Expect(exist).To(BeTrue()) + Expect(err).ToNot(HaveOccurred()) + + Expect(eventsMap).To(HaveKey("Status")) }) }) diff --git a/vendor/github.com/containers/buildah/pkg/formats/formats.go b/vendor/github.com/containers/buildah/pkg/formats/formats.go deleted file mode 100644 index e95c32fc3..000000000 --- a/vendor/github.com/containers/buildah/pkg/formats/formats.go +++ /dev/null @@ -1,167 +0,0 @@ -package formats - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "os" - "strings" - "text/tabwriter" - "text/template" - - "github.com/ghodss/yaml" - "github.com/pkg/errors" - "golang.org/x/crypto/ssh/terminal" -) - -const ( - // JSONString const to save on duplicate variable names - JSONString = "json" - // IDString const to save on duplicates for Go templates - IDString = "{{.ID}}" - - parsingErrorStr = "Template parsing error" -) - -// Writer interface for outputs -type Writer interface { - Out() error -} - -// JSONStructArray for JSON output -type JSONStructArray struct { - Output []interface{} -} - -// StdoutTemplateArray for Go template output -type StdoutTemplateArray struct { - Output []interface{} - Template string - Fields map[string]string -} - -// JSONStruct for JSON output -type JSONStruct struct { - Output interface{} -} - -// StdoutTemplate for Go template output -type StdoutTemplate struct { - Output interface{} - Template string - Fields map[string]string -} - -// YAMLStruct for YAML output -type YAMLStruct struct { - Output interface{} -} - -func setJSONFormatEncoder(isTerminal bool, w io.Writer) *json.Encoder { - enc := json.NewEncoder(w) - enc.SetIndent("", " ") - if isTerminal { - enc.SetEscapeHTML(false) - } - return enc -} - -// Out method for JSON Arrays -func (j JSONStructArray) Out() error { - buf := bytes.NewBuffer(nil) - enc := setJSONFormatEncoder(terminal.IsTerminal(int(os.Stdout.Fd())), buf) - if err := enc.Encode(j.Output); err != nil { - return err - } - data := buf.Bytes() - - // JSON returns a byte array with a literal null [110 117 108 108] in it - // if it is passed empty data. We used bytes.Compare to see if that is - // the case. - if diff := bytes.Compare(data, []byte("null")); diff == 0 { - data = []byte("[]") - } - - // If the we did get NULL back, we should spit out {} which is - // at least valid JSON for the consumer. - fmt.Printf("%s", data) - humanNewLine() - return nil -} - -// Out method for Go templates -func (t StdoutTemplateArray) Out() error { - w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) - if strings.HasPrefix(t.Template, "table") { - // replace any spaces with tabs in template so that tabwriter can align it - t.Template = strings.Replace(strings.TrimSpace(t.Template[5:]), " ", "\t", -1) - headerTmpl, err := template.New("header").Funcs(headerFunctions).Parse(t.Template) - if err != nil { - return errors.Wrapf(err, parsingErrorStr) - } - err = headerTmpl.Execute(w, t.Fields) - if err != nil { - return err - } - fmt.Fprintln(w, "") - } - t.Template = strings.Replace(t.Template, " ", "\t", -1) - tmpl, err := template.New("image").Funcs(basicFunctions).Parse(t.Template) - if err != nil { - return errors.Wrapf(err, parsingErrorStr) - } - for _, raw := range t.Output { - basicTmpl := tmpl.Funcs(basicFunctions) - if err := basicTmpl.Execute(w, raw); err != nil { - return errors.Wrapf(err, parsingErrorStr) - } - fmt.Fprintln(w, "") - } - return w.Flush() -} - -// Out method for JSON struct -func (j JSONStruct) Out() error { - data, err := json.MarshalIndent(j.Output, "", " ") - if err != nil { - return err - } - fmt.Printf("%s", data) - humanNewLine() - return nil -} - -//Out method for Go templates -func (t StdoutTemplate) Out() error { - tmpl, err := template.New("image").Parse(t.Template) - if err != nil { - return errors.Wrapf(err, "template parsing error") - } - err = tmpl.Execute(os.Stdout, t.Output) - if err != nil { - return err - } - humanNewLine() - return nil -} - -// Out method for YAML -func (y YAMLStruct) Out() error { - var buf []byte - var err error - buf, err = yaml.Marshal(y.Output) - if err != nil { - return err - } - fmt.Printf("%s", string(buf)) - humanNewLine() - return nil -} - -// humanNewLine prints a new line at the end of the output only if stdout is the terminal -func humanNewLine() { - if terminal.IsTerminal(int(os.Stdout.Fd())) { - fmt.Println() - } -} diff --git a/vendor/github.com/containers/buildah/pkg/formats/templates.go b/vendor/github.com/containers/buildah/pkg/formats/templates.go deleted file mode 100644 index c2582552a..000000000 --- a/vendor/github.com/containers/buildah/pkg/formats/templates.go +++ /dev/null @@ -1,78 +0,0 @@ -package formats - -import ( - "bytes" - "encoding/json" - "strings" - "text/template" -) - -// basicFunctions are the set of initial -// functions provided to every template. -var basicFunctions = template.FuncMap{ - "json": func(v interface{}) string { - buf := &bytes.Buffer{} - enc := json.NewEncoder(buf) - enc.SetEscapeHTML(false) - _ = enc.Encode(v) - // Remove the trailing new line added by the encoder - return strings.TrimSpace(buf.String()) - }, - "split": strings.Split, - "join": strings.Join, - "title": strings.Title, - "lower": strings.ToLower, - "upper": strings.ToUpper, - "pad": padWithSpace, - "truncate": truncateWithLength, -} - -// HeaderFunctions are used to created headers of a table. -// This is a replacement of basicFunctions for header generation -// because we want the header to remain intact. -// Some functions like `split` are irrelevant so not added. -var headerFunctions = template.FuncMap{ - "json": func(v string) string { - return v - }, - "title": func(v string) string { - return v - }, - "lower": func(v string) string { - return v - }, - "upper": func(v string) string { - return v - }, - "truncate": func(v string, l int) string { - return v - }, -} - -// Parse creates a new anonymous template with the basic functions -// and parses the given format. -func Parse(format string) (*template.Template, error) { - return NewParse("", format) -} - -// NewParse creates a new tagged template with the basic functions -// and parses the given format. -func NewParse(tag, format string) (*template.Template, error) { - return template.New(tag).Funcs(basicFunctions).Parse(format) -} - -// padWithSpace adds whitespace to the input if the input is non-empty -func padWithSpace(source string, prefix, suffix int) string { - if source == "" { - return source - } - return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix) -} - -// truncateWithLength truncates the source string up to the length provided by the input -func truncateWithLength(source string, length int) string { - if len(source) < length { - return source - } - return source[:length] -} diff --git a/vendor/modules.txt b/vendor/modules.txt index e1c2b6300..f6badb5f7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -78,7 +78,6 @@ github.com/containers/buildah/manifests github.com/containers/buildah/pkg/blobcache github.com/containers/buildah/pkg/chrootuser github.com/containers/buildah/pkg/cli -github.com/containers/buildah/pkg/formats github.com/containers/buildah/pkg/manifests github.com/containers/buildah/pkg/overlay github.com/containers/buildah/pkg/parse |