Skip to content

Commit

Permalink
feat(contracts): support multi-format (chainloop-dev#1239)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel <[email protected]>
  • Loading branch information
migmartri authored Aug 19, 2024
1 parent 0afe385 commit 8962739
Show file tree
Hide file tree
Showing 41 changed files with 2,020 additions and 778 deletions.
17 changes: 2 additions & 15 deletions app/cli/cmd/workflow_contract_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
)

const formatContract = "schema"
Expand Down Expand Up @@ -71,13 +70,7 @@ func encodeContractOutput(run *action.WorkflowContractWithVersionItem) error {

switch flagOutputFormat {
case formatContract:
marshaler := protojson.MarshalOptions{Indent: " "}
rawBody, err := marshaler.Marshal(run.Revision.BodyV1)
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, string(rawBody))

fmt.Fprintln(os.Stdout, run.Revision.RawBody.Body)
return nil
default:
return ErrOutputFormatNotImplemented
Expand All @@ -87,12 +80,6 @@ func encodeContractOutput(run *action.WorkflowContractWithVersionItem) error {
func contractDescribeTableOutput(contractWithVersion *action.WorkflowContractWithVersionItem) error {
revision := contractWithVersion.Revision

marshaler := protojson.MarshalOptions{Indent: " "}
rawBody, err := marshaler.Marshal(revision.BodyV1)
if err != nil {
return err
}

c := contractWithVersion.Contract
t := newTableWriter()
t.SetTitle("Contract")
Expand All @@ -108,7 +95,7 @@ func contractDescribeTableOutput(contractWithVersion *action.WorkflowContractWit
t.Render()

vt := newTableWriter()
vt.AppendRow(table.Row{string(rawBody)})
vt.AppendRow(table.Row{revision.RawBody.Body})
vt.Render()

return nil
Expand Down
38 changes: 32 additions & 6 deletions app/cli/internal/action/workflow_contract_create.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2023 The Chainloop Authors.
// Copyright 2024 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,9 +17,14 @@ package action

import (
"context"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/chainloop-dev/chainloop/pkg/attestation/crafter"
)

type WorkflowContractCreate struct {
Expand All @@ -38,14 +43,12 @@ func (action *WorkflowContractCreate) Run(name string, description *string, cont
}

if contractPath != "" {
contract, err := crafter.LoadSchema(contractPath)
rawContract, err := loadFileOrURL(contractPath)
if err != nil {
action.cfg.Logger.Debug().Err(err).Msg("loading the contract")
return nil, err
}
request.Contract = &pb.WorkflowContractServiceCreateRequest_V1{
V1: contract,
}
request.RawContract = rawContract
}

resp, err := client.Create(context.Background(), request)
Expand All @@ -56,3 +59,26 @@ func (action *WorkflowContractCreate) Run(name string, description *string, cont

return pbWorkflowContractItemToAction(resp.Result), nil
}

func loadFileOrURL(fileRef string) ([]byte, error) {
parts := strings.SplitAfterN(fileRef, "://", 2)
if len(parts) == 2 {
scheme := parts[0]
switch scheme {
case "http://":
fallthrough
case "https://":
// #nosec G107
resp, err := http.Get(fileRef)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
default:
return nil, errors.New("invalid file scheme")
}
}

return os.ReadFile(filepath.Clean(fileRef))
}
7 changes: 7 additions & 0 deletions app/cli/internal/action/workflow_contract_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type WorkflowContractVersionItem struct {
Revision int `json:"revision"`
CreatedAt *time.Time `json:"createdAt"`
BodyV1 *schemav1.CraftingSchema `json:"bodyV1"`
RawBody *ContractRawBody `json:"rawBody"`
}

type ContractRawBody struct {
Body string `json:"body"`
Format string `json:"format"`
}

func NewWorkflowContractList(cfg *ActionsOpts) *WorkflowContractList {
Expand Down Expand Up @@ -73,5 +79,6 @@ func pbWorkflowContractVersionItemToAction(in *pb.WorkflowContractVersionItem) *
return &WorkflowContractVersionItem{
Revision: int(in.GetRevision()), ID: in.GetId(), BodyV1: in.GetV1(),
CreatedAt: toTimePtr(in.GetCreatedAt().AsTime()),
RawBody: &ContractRawBody{Body: string(in.RawContract.GetBody()), Format: in.RawContract.GetFormat().String()},
}
}
7 changes: 2 additions & 5 deletions app/cli/internal/action/workflow_contract_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/chainloop-dev/chainloop/pkg/attestation/crafter"
)

type WorkflowContractUpdate struct {
Expand All @@ -35,14 +34,12 @@ func (action *WorkflowContractUpdate) Run(name string, description *string, cont

request := &pb.WorkflowContractServiceUpdateRequest{Name: name, Description: description}
if contractPath != "" {
contract, err := crafter.LoadSchema(contractPath)
rawContract, err := loadFileOrURL(contractPath)
if err != nil {
action.cfg.Logger.Debug().Err(err).Msg("loading the contract")
return nil, err
}
request.Contract = &pb.WorkflowContractServiceUpdateRequest_V1{
V1: contract,
}
request.RawContract = rawContract
}

resp, err := client.Update(context.Background(), request)
Expand Down
Loading

0 comments on commit 8962739

Please sign in to comment.