Skip to content

Commit 5490548

Browse files
committed
cmd/rofl/machine/show: Add JSON output
1 parent d0867e1 commit 5490548

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

cmd/rofl/build/artifacts.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ import (
2222
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
2323

2424
"github.com/oasisprotocol/cli/build/env"
25+
"github.com/oasisprotocol/cli/cmd/common"
2526
)
2627

2728
const artifactCacheDir = "build_cache"
2829

2930
// maybeDownloadArtifact downloads the given artifact and optionally verifies its integrity against
3031
// the hash provided in the URI fragment.
3132
func maybeDownloadArtifact(kind, uri string) string {
32-
fmt.Printf("Downloading %s artifact...\n", kind)
33-
fmt.Printf(" URI: %s\n", uri)
33+
if common.OutputFormat() != common.FormatJSON {
34+
fmt.Printf("Downloading %s artifact...\n", kind)
35+
fmt.Printf(" URI: %s\n", uri)
36+
}
3437

3538
url, err := url.Parse(uri)
3639
if err != nil {
@@ -49,7 +52,7 @@ func maybeDownloadArtifact(kind, uri string) string {
4952
if url.Fragment != "" {
5053
knownHash = url.Fragment
5154
}
52-
if knownHash != "" {
55+
if knownHash != "" && common.OutputFormat() != common.FormatJSON {
5356
fmt.Printf(" Hash: %s\n", knownHash)
5457
}
5558

@@ -77,7 +80,9 @@ func maybeDownloadArtifact(kind, uri string) string {
7780
}
7881
f.Close()
7982

80-
fmt.Printf(" (using cached artifact)\n")
83+
if common.OutputFormat() != common.FormatJSON {
84+
fmt.Printf(" (using cached artifact)\n")
85+
}
8186
case errors.Is(err, os.ErrNotExist):
8287
// Does not exist in cache, download.
8388
f, err = os.Create(cacheFn)

cmd/rofl/machine/show.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package machine
33
import (
44
"context"
55
"encoding/binary"
6+
"encoding/json"
67
"fmt"
78
"net"
89
"strings"
@@ -63,10 +64,21 @@ var showCmd = &cobra.Command{
6364
if err != nil {
6465
// The "instance not found" error originates from Rust code, so we can't compare it nicely here.
6566
if strings.Contains(err.Error(), "instance not found") {
66-
cobra.CheckErr("Machine instance not found.\nTip: This often happens when instances expire. Run `oasis rofl deploy --replace-machine` to rent a new one.")
67+
switch common.OutputFormat() {
68+
case common.FormatJSON:
69+
fmt.Printf("{ \"error\": \"%s\" }\n", err)
70+
case common.FormatText:
71+
cobra.CheckErr("Machine instance not found.\nTip: This often happens when instances expire. Run `oasis rofl deploy --replace-machine` to rent a new one.")
72+
}
6773
}
6874
cobra.CheckErr(err)
6975
}
76+
if common.OutputFormat() == common.FormatJSON {
77+
data, err := json.MarshalIndent(insDsc, "", " ")
78+
cobra.CheckErr(err)
79+
fmt.Printf("%s\n", data)
80+
return
81+
}
7082

7183
insCmds, err := conn.Runtime(npa.ParaTime).ROFLMarket.InstanceCommands(ctx, client.RoundLatest, *providerAddr, machineID)
7284
cobra.CheckErr(err)
@@ -236,5 +248,6 @@ func showMachinePorts(extraCfg *roflCmdBuild.AppExtraConfig, appID rofl.AppID, i
236248

237249
func init() {
238250
common.AddSelectorFlags(showCmd)
251+
showCmd.Flags().AddFlagSet(common.FormatFlag)
239252
showCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
240253
}

0 commit comments

Comments
 (0)