Skip to content

Commit

Permalink
primitive file (and spec) upload
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Mar 8, 2020
1 parent 3f84c85 commit 1cb5355
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
50 changes: 48 additions & 2 deletions apps/disco-flame/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func main() {
Usage:
disco help
disco list [--raw]
disco get [<api>] [<version>] [--raw] [--openapi2] [--openapi3] [--features] [--schemas] [--all]
disco <file> [--openapi2] [--openapi3] [--features] [--schemas]
disco get [<api>] [<version>] [--upload] [--raw] [--openapi2] [--openapi3] [--features] [--schemas] [--all]
disco <file> [--upload] [--openapi2] [--openapi3] [--features] [--schemas]
`
arguments, err := docopt.Parse(usage, nil, false, "Disco 1.0", false)
if err != nil {
Expand Down Expand Up @@ -195,6 +195,52 @@ func handleExportArgumentsForBytes(arguments map[string]interface{}, bytes []byt
if err != nil {
return true, err
}
if arguments["--upload"].(bool) {
log.Printf("uploading")
initFlame()
api := document
ctx := context.TODO()
// does the spec exist? if not, create it
{
request := &rpcpb.GetSpecRequest{}
request.Name = "projects/google/products/" + api.Name +
"/versions/" + api.Version +
"/specs/discovery"
response, err := FlameClient.GetSpec(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
if err != nil { // TODO only do this for NotFound errors
request := &rpcpb.CreateSpecRequest{}
request.Parent = "projects/google/products/" + api.Name +
"/versions/" + api.Version
request.SpecId = "discovery"
request.Spec = &rpcpb.Spec{}
request.Spec.Style = "discovery"
response, err := FlameClient.CreateSpec(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
}
}
// does the file exist? if not, create it
{
request := &rpcpb.GetFileRequest{}
request.Name = "projects/google/products/" + api.Name +
"/versions/" + api.Version +
"/specs/discovery" +
"/files/0"
response, err := FlameClient.GetFile(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
if err != nil { // TODO only do this for NotFound errors
request := &rpcpb.CreateFileRequest{}
request.Parent = "projects/google/products/" + api.Name +
"/versions/" + api.Version + "/specs/discovery"
request.FileId = "0"
request.File = &rpcpb.File{}
request.File.Contents = bytes
response, err := FlameClient.CreateFile(ctx, request)
log.Printf("response %+v\nerr %+v", response, err)
}
}
handled = true
}
if arguments["--raw"].(bool) {
// Write the Discovery document as a JSON file.
filename := "disco-" + document.Name + "-" + document.Version + ".json"
Expand Down
4 changes: 3 additions & 1 deletion models/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type File struct {
SizeInBytes int32 // Size of the file.
Hash string // A hash of the file.
SourceURI string // The original source URI of the file.
Contents []byte // The contents of the file.
Contents []byte `datastore:",noindex"` // The contents of the file.
}

// ParseParentSpec ...
Expand Down Expand Up @@ -108,13 +108,15 @@ func (file *File) Message() (message *rpc.File, err error) {
message.Description = file.Description
message.CreateTime, err = ptypes.TimestampProto(file.CreateTime)
message.UpdateTime, err = ptypes.TimestampProto(file.UpdateTime)
message.Contents = file.Contents
//message.Availability = file.Availability
//message.RecommendedVersion = file.RecommendedVersion
return message, err
}

// Update modifies a file using the contents of a message.
func (file *File) Update(message *rpc.File) error {
file.Contents = message.GetContents()
file.UpdateTime = file.CreateTime
return nil
}
1 change: 1 addition & 0 deletions models/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (spec *Spec) Message() (message *rpc.Spec, err error) {

// Update modifies a spec using the contents of a message.
func (spec *Spec) Update(message *rpc.Spec) error {
spec.Style = message.GetStyle()
spec.UpdateTime = spec.CreateTime
return nil
}
1 change: 1 addition & 0 deletions server/actions_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (s *server) CreateFile(ctx context.Context, request *rpc.CreateFileRequest)
return nil, status.Error(codes.AlreadyExists, file.ResourceName()+" already exists")
}
file.CreateTime = file.UpdateTime
err = file.Update(request.GetFile())
k, err = client.Put(ctx, k, file)
if err != nil {
return nil, err
Expand Down

0 comments on commit 1cb5355

Please sign in to comment.