Skip to content

Commit

Permalink
Use json tags instead of toml and some other adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 20, 2024
1 parent e613cfc commit 1f02562
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
14 changes: 7 additions & 7 deletions archiveplugin/archiveplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ var (

// Request is what is sent to an external archive tool.
type Request struct {
GoInfo model.GoInfo `toml:"go_info"`
GoInfo model.GoInfo `json:"go_info"`

// Settings for the archive.
// This is the content of archive_settings.custom_settings.
Settings map[string]any `toml:"settings"`
Settings map[string]any `json:"settings"`

Files []ArchiveFile `toml:"files"`
Files []ArchiveFile `json:"files"`

// Filename with extension.
OutFilename string `toml:"out_filename"`
OutFilename string `json:"out_filename"`
}

func (r *Request) Init() error {
Expand All @@ -65,13 +65,13 @@ func (r *Request) Init() error {

type ArchiveFile struct {
// The source filename.
SourcePathAbs string `toml:"source_path_abs"`
SourcePathAbs string `json:"source_path_abs"`

// Relative target path, including the name of the file.
TargetPath string `toml:"target_path"`
TargetPath string `json:"target_path"`

// Mode represents a file's mode and permission bits.
Mode fs.FileMode `toml:"mode"`
Mode fs.FileMode `json:"mode"`
}

func (a *ArchiveFile) Init() error {
Expand Down
2 changes: 1 addition & 1 deletion archiveplugin/archiveplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func TestStartClientInitFail(t *testing.T) {
c := qt.New(t)

client, err := execrpc.StartClient(
_, err := execrpc.StartClient(
execrpc.ClientOptions[model.Config, Request, any, model.Receipt]{
ClientRawOptions: execrpc.ClientRawOptions{
Version: 1,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.0
toolchain go1.22.0

require (
github.com/bep/execrpc v0.9.0
github.com/bep/execrpc v0.10.0
github.com/mitchellh/mapstructure v1.5.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/bep/execrpc v0.7.1 h1:ExHlNt9immvo2We9fNnNquXXlGKX1FKrvYNG1ZXd1Fg=
github.com/bep/execrpc v0.7.1/go.mod h1:dhMMXFX/IfDRQrp4/EHqqYbYfHS53bF/9kiHIG/ChJw=
github.com/bep/execrpc v0.9.0 h1:SIQnFxUIpJEbfR3FTp94OinpqBrZDQDVWbJ82pgJ77k=
github.com/bep/execrpc v0.9.0/go.mod h1:lGHGK8NX0KvfhlRNH/SebW1quHGwXFeE3Ba+2KLtmrM=
github.com/bep/execrpc v0.10.0 h1:Y8S8pZOFZI/zo95RMqgri98eIFaZEqZ41tbF89hSx/A=
github.com/bep/execrpc v0.10.0/go.mod h1:lGHGK8NX0KvfhlRNH/SebW1quHGwXFeE3Ba+2KLtmrM=
github.com/bep/helpers v0.3.1 h1:rvwT4Zuq9VMqTh/OypjKrO/VQE1V0Eyr/PBlfQDcK8E=
github.com/bep/helpers v0.3.1/go.mod h1:/QpHdmcPagDw7+RjkLFCvnlUc8lQ5kg4KDrEkb2Yyco=
github.com/bep/helpers v0.5.0 h1:rneezhnG7GzLFlsEWO/EnleaBRuluBDGFimalO6Y50o=
Expand Down
4 changes: 2 additions & 2 deletions model/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func NewError(what string, err error) *Error {

// Error holds an error message.
type Error struct {
Msg string `toml:"msg"`
Msg string `json:"msg"`
}

func (r Error) Error() string {
func (r *Error) Error() string {
return r.Msg
}

Expand Down
17 changes: 10 additions & 7 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,23 @@ type Initializer interface {

// GoInfo contains the Go environment information.
type GoInfo struct {
Goos string `toml:"goos"`
Goarch string `toml:"goarch"`
Goos string `json:"goos"`
Goarch string `json:"goarch"`
}

// ProjectInfo contains the project and tag information.
type ProjectInfo struct {
Project string `toml:"project"`
Tag string `toml:"tag"`
Project string `json:"project"`
Tag string `json:"tag"`
}

// Config configures the plugin.
type Config struct {
Version int `toml:"version"`
Try bool `toml:"try"`
ProjectInfo ProjectInfo `toml:"project_info"`
// If set, the plugin should run in "dry-run" mode.
Try bool `json:"try"`

// The project build information.
ProjectInfo ProjectInfo `json:"project_info"`
}

// Receipt passed back to the client.
Expand Down
37 changes: 36 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,39 @@

package server

// TODO1 remove me.
import (
"fmt"

"github.com/bep/execrpc"
)

// protocolVersion is the major version of the protocol.
const protocolVersion = 2

// Options is a sub set of execrpc.ServerOptions.
type Options[C, Q, M, R any] struct {
// Init is the function that will be called when the server is started.
Init func(C, execrpc.ProtocolInfo) error

// Handle is the function that will be called when a request is received.
Handle func(*execrpc.Call[Q, M, R])
}

// New is just a wrapper around execrpc.New with some additional and common checks.
func New[C, Q, M, R any](opts Options[C, Q, M, R]) (*execrpc.Server[C, Q, M, R], error) {
return execrpc.NewServer(
execrpc.ServerOptions[C, Q, M, R]{
GetHasher: nil,
DelayDelivery: false,
Init: func(v C, protocol execrpc.ProtocolInfo) error {
if protocol.Version != protocolVersion {
return fmt.Errorf("unsupported protocol version %d, expected %d", protocol.Version, protocolVersion)
}
return opts.Init(v, protocol)
},
Handle: func(call *execrpc.Call[Q, M, R]) {
opts.Handle(call)
},
},
)
}

0 comments on commit 1f02562

Please sign in to comment.