diff --git a/contrib/nydusify/cmd/nydusify.go b/contrib/nydusify/cmd/nydusify.go index 553a86095bb..5d53e683d6c 100644 --- a/contrib/nydusify/cmd/nydusify.go +++ b/contrib/nydusify/cmd/nydusify.go @@ -409,6 +409,12 @@ func main() { Usage: "Path to the nydus-image binary, default to search in PATH", EnvVars: []string{"NYDUS_IMAGE"}, }, + &cli.BoolFlag{ + Name: "output-json", + Value: false, + Usage: "Enable saving the metrics collected during conversion in JSON file output.json", + EnvVars: []string{"OUTPUT_JSON"}, + }, }, Action: func(c *cli.Context) error { setupLogLevel(c) @@ -503,6 +509,8 @@ func main() { OCIRef: c.Bool("oci-ref"), AllPlatforms: c.Bool("all-platforms"), Platforms: c.String("platform"), + + OutputJSON: c.Bool("output-json"), } return converter.Convert(context.Background(), opt) diff --git a/contrib/nydusify/pkg/converter/converter.go b/contrib/nydusify/pkg/converter/converter.go index 7e15664c313..41b82f94f18 100644 --- a/contrib/nydusify/pkg/converter/converter.go +++ b/contrib/nydusify/pkg/converter/converter.go @@ -48,6 +48,8 @@ type Opt struct { AllPlatforms bool Platforms string + + OutputJSON bool } func Convert(ctx context.Context, opt Opt) error { @@ -87,6 +89,9 @@ func Convert(ctx context.Context, opt Opt) error { return err } - _, err = cvt.Convert(ctx, opt.Source, opt.Target) + metric, err := cvt.Convert(ctx, opt.Source, opt.Target) + if opt.OutputJSON { + dumpMetric(metric, opt.WorkDir) + } return err } diff --git a/contrib/nydusify/pkg/converter/metric.go b/contrib/nydusify/pkg/converter/metric.go new file mode 100644 index 00000000000..eaecb410e64 --- /dev/null +++ b/contrib/nydusify/pkg/converter/metric.go @@ -0,0 +1,28 @@ +// Copyright 2023 Nydus Developers. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + +package converter + +import ( + "encoding/json" + "os" + "path/filepath" + + "github.com/goharbor/acceleration-service/pkg/converter" + "github.com/pkg/errors" +) + +func dumpMetric(metric *converter.Metric, workDir string) error { + file, err := os.Create(filepath.Join(workDir, "output.json")) + if err != nil { + return errors.Wrap(err, "Create file for metric") + } + defer file.Close() + + encoder := json.NewEncoder(file) + if err := encoder.Encode(metric); err != nil { + return errors.Wrap(err, "Encode JSON from metric") + } + return nil +}