Skip to content

Commit 7601afc

Browse files
committed
build: support --format (pb and json)
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 577e137 commit 7601afc

File tree

6 files changed

+90
-41
lines changed

6 files changed

+90
-41
lines changed

commands/apply.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"io/ioutil"
54
"log"
65

76
"github.com/spf13/cobra"
@@ -14,14 +13,9 @@ var ApplyCmd = &cobra.Command{
1413
Run: func(cmd *cobra.Command, args []string) {
1514
root, path := args[0], args[1]
1615

17-
p, err := ioutil.ReadFile(path)
16+
m, err := readManifest(path)
1817
if err != nil {
19-
log.Fatalf("error reading manifest: %v", err)
20-
}
21-
22-
m, err := continuity.Unmarshal(p)
23-
if err != nil {
24-
log.Fatalf("error unmarshaling manifest: %v", err)
18+
log.Fatal(err)
2519
}
2620

2721
ctx, err := continuity.NewContext(root)

commands/build.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67

@@ -13,6 +14,13 @@ var (
1314
format string
1415
}
1516

17+
marshalers = map[string]func(*continuity.Manifest) ([]byte, error){
18+
"pb": continuity.Marshal,
19+
continuity.MediaTypeManifestV0Protobuf: continuity.Marshal,
20+
"json": continuity.MarshalJSON,
21+
continuity.MediaTypeManifestV0JSON: continuity.MarshalJSON,
22+
}
23+
1624
BuildCmd = &cobra.Command{
1725
Use: "build <root>",
1826
Short: "Build a manifest for the provided root",
@@ -31,9 +39,15 @@ var (
3139
log.Fatalf("error generating manifest: %v", err)
3240
}
3341

34-
p, err := continuity.Marshal(m)
42+
marshaler, ok := marshalers[buildCmdConfig.format]
43+
if !ok {
44+
log.Fatalf("unknown format %s", buildCmdConfig.format)
45+
}
46+
47+
p, err := marshaler(m)
3548
if err != nil {
36-
log.Fatalf("error marshaling manifest: %v", err)
49+
log.Fatalf("error marshalling manifest as %s: %v",
50+
buildCmdConfig.format, err)
3751
}
3852

3953
if _, err := os.Stdout.Write(p); err != nil {
@@ -44,5 +58,7 @@ var (
4458
)
4559

4660
func init() {
47-
BuildCmd.Flags().StringVar(&buildCmdConfig.format, "format", "pb", "specify the output format of the manifest")
61+
BuildCmd.Flags().StringVar(&buildCmdConfig.format, "format", "pb",
62+
fmt.Sprintf("specify the output format of the manifest (\"pb\"|%q|\"json\"|%q)",
63+
continuity.MediaTypeManifestV0Protobuf, continuity.MediaTypeManifestV0JSON))
4864
}

commands/commandsutil.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/stevvooe/continuity"
10+
)
11+
12+
func readManifest(path string) (*continuity.Manifest, error) {
13+
p, err := ioutil.ReadFile(path)
14+
if err != nil {
15+
return nil, fmt.Errorf("error reading manifest: %v", err)
16+
}
17+
ext := strings.ToLower(filepath.Ext(path))
18+
if ext == ".json" {
19+
return continuity.UnmarshalJSON(p)
20+
}
21+
return continuity.Unmarshal(p)
22+
}

commands/mount.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"io/ioutil"
54
"log"
65
"os"
76
"os/signal"
@@ -30,14 +29,9 @@ var MountCmd = &cobra.Command{
3029

3130
manifestName := filepath.Base(manifest)
3231

33-
p, err := ioutil.ReadFile(manifest)
32+
m, err := readManifest(manifest)
3433
if err != nil {
35-
log.Fatalf("error reading manifest: %v", err)
36-
}
37-
38-
m, err := continuity.Unmarshal(p)
39-
if err != nil {
40-
log.Fatalf("error unmarshaling manifest: %v", err)
34+
log.Fatal(err)
4135
}
4236

4337
driver, err := continuity.NewSystemDriver()

commands/verify.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"io/ioutil"
54
"log"
65

76
"github.com/spf13/cobra"
@@ -18,14 +17,9 @@ var VerifyCmd = &cobra.Command{
1817

1918
root, path := args[0], args[1]
2019

21-
p, err := ioutil.ReadFile(path)
20+
m, err := readManifest(path)
2221
if err != nil {
23-
log.Fatalf("error reading manifest: %v", err)
24-
}
25-
26-
m, err := continuity.Unmarshal(p)
27-
if err != nil {
28-
log.Fatalf("error unmarshaling manifest: %v", err)
22+
log.Fatal(err)
2923
}
3024

3125
ctx, err := continuity.NewContext(root)

manifest.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
package continuity
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"log"
78
"os"
89
"sort"
910

11+
"github.com/golang/protobuf/jsonpb"
1012
"github.com/golang/protobuf/proto"
1113
pb "github.com/stevvooe/continuity/proto"
1214
)
1315

16+
const (
17+
// MediaTypeManifestV0Protobuf is the media type for manifest formatted as protobuf.
18+
// The format is unstable during v0.
19+
MediaTypeManifestV0Protobuf = "application/vnd.continuity.manifest.v0+pb"
20+
// MediaTypeManifestV0JSON is the media type for manifest formatted as JSON.
21+
// JSON is marshalled from protobuf using jsonpb.Marshaler
22+
// ({EnumAsInts = false, EmitDefaults = false, OrigName = false})
23+
// The format is unstable during v0.
24+
MediaTypeManifestV0JSON = "application/vnd.continuity.manifest.v0+json"
25+
)
26+
1427
// Manifest provides the contents of a manifest. Users of this struct should
1528
// not typically modify any fields directly.
1629
type Manifest struct {
1730
// Resources specifies all the resources for a manifest in order by path.
1831
Resources []Resource
1932
}
2033

21-
func Unmarshal(p []byte) (*Manifest, error) {
22-
var bm pb.Manifest
23-
24-
if err := proto.Unmarshal(p, &bm); err != nil {
25-
return nil, err
26-
}
27-
34+
func manifestFromProto(bm *pb.Manifest) (*Manifest, error) {
2835
var m Manifest
2936
for _, b := range bm.Resource {
3037
r, err := fromProto(b)
@@ -34,26 +41,48 @@ func Unmarshal(p []byte) (*Manifest, error) {
3441

3542
m.Resources = append(m.Resources, r)
3643
}
37-
3844
return &m, nil
3945
}
4046

41-
func Marshal(m *Manifest) ([]byte, error) {
47+
func Unmarshal(p []byte) (*Manifest, error) {
4248
var bm pb.Manifest
43-
for _, resource := range m.Resources {
44-
bm.Resource = append(bm.Resource, toProto(resource))
49+
if err := proto.Unmarshal(p, &bm); err != nil {
50+
return nil, err
4551
}
52+
return manifestFromProto(&bm)
53+
}
4654

47-
return proto.Marshal(&bm)
55+
func UnmarshalJSON(p []byte) (*Manifest, error) {
56+
var bm pb.Manifest
57+
if err := jsonpb.Unmarshal(bytes.NewReader(p), &bm); err != nil {
58+
return nil, err
59+
}
60+
return manifestFromProto(&bm)
4861
}
4962

50-
func MarshalText(w io.Writer, m *Manifest) error {
63+
func manifestToProto(m *Manifest) *pb.Manifest {
5164
var bm pb.Manifest
5265
for _, resource := range m.Resources {
5366
bm.Resource = append(bm.Resource, toProto(resource))
5467
}
68+
return &bm
69+
}
5570

56-
return proto.MarshalText(w, &bm)
71+
func Marshal(m *Manifest) ([]byte, error) {
72+
return proto.Marshal(manifestToProto(m))
73+
}
74+
75+
func MarshalText(w io.Writer, m *Manifest) error {
76+
return proto.MarshalText(w, manifestToProto(m))
77+
}
78+
79+
func MarshalJSON(m *Manifest) ([]byte, error) {
80+
var b bytes.Buffer
81+
marshaler := &jsonpb.Marshaler{}
82+
if err := marshaler.Marshal(&b, manifestToProto(m)); err != nil {
83+
return nil, err
84+
}
85+
return b.Bytes(), nil
5786
}
5887

5988
// BuildManifest creates the manifest for the given context

0 commit comments

Comments
 (0)