From ee062c655ffd303dd190c8f014d0dea1987763d1 Mon Sep 17 00:00:00 2001 From: sophia Date: Fri, 14 May 2021 14:36:03 -0500 Subject: [PATCH] Example parsing vagrantfile proto to plugin config proto --- go_parse_vagrantfile_ex/main.pb.go | 162 +++++++++++++++++++++++++++++ go_parse_vagrantfile_ex/main.proto | 10 ++ parse_vagrantfile_proto.go | 61 +++++++++++ 3 files changed, 233 insertions(+) create mode 100644 go_parse_vagrantfile_ex/main.pb.go create mode 100644 go_parse_vagrantfile_ex/main.proto create mode 100644 parse_vagrantfile_proto.go diff --git a/go_parse_vagrantfile_ex/main.pb.go b/go_parse_vagrantfile_ex/main.pb.go new file mode 100644 index 000000000..38fd96349 --- /dev/null +++ b/go_parse_vagrantfile_ex/main.pb.go @@ -0,0 +1,162 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.23.0 +// protoc v3.13.0 +// source: vagrant-ruby/go_parse_vagrantfile_ex/main.proto + +package go_parse_vagrantfile_ex + +import ( + proto "github.com/golang/protobuf/proto" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +type FileProvisioner struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Source string `protobuf:"bytes,1,opt,name=source,proto3" json:"source,omitempty"` + Destination string `protobuf:"bytes,2,opt,name=destination,proto3" json:"destination,omitempty"` +} + +func (x *FileProvisioner) Reset() { + *x = FileProvisioner{} + if protoimpl.UnsafeEnabled { + mi := &file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *FileProvisioner) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*FileProvisioner) ProtoMessage() {} + +func (x *FileProvisioner) ProtoReflect() protoreflect.Message { + mi := &file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use FileProvisioner.ProtoReflect.Descriptor instead. +func (*FileProvisioner) Descriptor() ([]byte, []int) { + return file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescGZIP(), []int{0} +} + +func (x *FileProvisioner) GetSource() string { + if x != nil { + return x.Source + } + return "" +} + +func (x *FileProvisioner) GetDestination() string { + if x != nil { + return x.Destination + } + return "" +} + +var File_vagrant_ruby_go_parse_vagrantfile_ex_main_proto protoreflect.FileDescriptor + +var file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDesc = []byte{ + 0x0a, 0x2f, 0x76, 0x61, 0x67, 0x72, 0x61, 0x6e, 0x74, 0x2d, 0x72, 0x75, 0x62, 0x79, 0x2f, 0x67, + 0x6f, 0x5f, 0x70, 0x61, 0x72, 0x73, 0x65, 0x5f, 0x76, 0x61, 0x67, 0x72, 0x61, 0x6e, 0x74, 0x66, + 0x69, 0x6c, 0x65, 0x5f, 0x65, 0x78, 0x2f, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x17, 0x70, 0x61, 0x72, 0x73, 0x65, 0x5f, 0x76, 0x61, 0x67, 0x72, 0x61, 0x6e, 0x74, + 0x66, 0x69, 0x6c, 0x65, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x4b, 0x0a, 0x0f, 0x46, 0x69, + 0x6c, 0x65, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x65, 0x72, 0x12, 0x16, 0x0a, + 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, + 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, + 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x74, + 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x26, 0x5a, 0x24, 0x76, 0x61, 0x67, 0x72, 0x61, + 0x6e, 0x74, 0x2d, 0x72, 0x75, 0x62, 0x79, 0x2f, 0x67, 0x6f, 0x5f, 0x70, 0x61, 0x72, 0x73, 0x65, + 0x5f, 0x76, 0x61, 0x67, 0x72, 0x61, 0x6e, 0x74, 0x66, 0x69, 0x6c, 0x65, 0x5f, 0x65, 0x78, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescOnce sync.Once + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescData = file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDesc +) + +func file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescGZIP() []byte { + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescOnce.Do(func() { + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescData = protoimpl.X.CompressGZIP(file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescData) + }) + return file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDescData +} + +var file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_goTypes = []interface{}{ + (*FileProvisioner)(nil), // 0: parse_vagrantfile_proto.FileProvisioner +} +var file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_init() } +func file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_init() { + if File_vagrant_ruby_go_parse_vagrantfile_ex_main_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*FileProvisioner); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_goTypes, + DependencyIndexes: file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_depIdxs, + MessageInfos: file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_msgTypes, + }.Build() + File_vagrant_ruby_go_parse_vagrantfile_ex_main_proto = out.File + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_rawDesc = nil + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_goTypes = nil + file_vagrant_ruby_go_parse_vagrantfile_ex_main_proto_depIdxs = nil +} diff --git a/go_parse_vagrantfile_ex/main.proto b/go_parse_vagrantfile_ex/main.proto new file mode 100644 index 000000000..133e1dccf --- /dev/null +++ b/go_parse_vagrantfile_ex/main.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +package parse_vagrantfile_proto; + +option go_package = "vagrant-ruby/go_parse_vagrantfile_ex"; + +message FileProvisioner { + string source = 1; + string destination = 2; +} diff --git a/parse_vagrantfile_proto.go b/parse_vagrantfile_proto.go new file mode 100644 index 000000000..2425a0df5 --- /dev/null +++ b/parse_vagrantfile_proto.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + + "github.com/hashicorp/vagrant/internal/server/proto/ruby_vagrant" + + "github.com/hashicorp/vagrant/go_parse_vagrantfile_ex" + "github.com/mitchellh/mapstructure" + "google.golang.org/protobuf/types/known/anypb" + "google.golang.org/protobuf/types/known/structpb" +) + +//go:generate protoc -I .. --go_opt=plugins=grpc --go_out=.. vagrant-ruby/go_parse_vagrantfile_ex/main.proto + +func main() { + // Setup an example proto + m, _ := structpb.NewValue(map[string]interface{}{ + "source": ".gitignore", + "destination": "/.gitignore", + }) + any, _ := anypb.New(m) + + testProto := &ruby_vagrant.VagrantfileComponents_Vagrantfile{ + Path: "", + Raw: "", + CurrentVersion: "2", + MachineConfigs: []*ruby_vagrant.VagrantfileComponents_MachineConfig{ + &ruby_vagrant.VagrantfileComponents_MachineConfig{ + Name: "a", + ConfigVm: &ruby_vagrant.VagrantfileComponents_ConfigVM{ + Box: "hashicorp/bionic64", + Provisioners: []*ruby_vagrant.VagrantfileComponents_Provisioner{ + &ruby_vagrant.VagrantfileComponents_Provisioner{ + Name: "", + Type: "file", + Before: "", + After: "", + CommunicatorRequired: true, + Config: any, + }, + }, + }, + }, + }, + } + for _, m := range testProto.MachineConfigs { + fmt.Println("got machine \"" + m.Name + "\"") + fmt.Println("got box " + m.ConfigVm.Box) + for _, p := range m.ConfigVm.Provisioners { + fmt.Println("got provisioner " + p.Type) + s2, _ := p.Config.UnmarshalNew() + s2structpb := s2.(*structpb.Value) + var result go_parse_vagrantfile_ex.FileProvisioner + s2struct := s2structpb.GetStructValue() + mapstructure.Decode(s2struct.AsMap(), &result) + fmt.Println("source: " + result.Source) + fmt.Println("destination: " + result.Destination) + } + } +}