From 98c89f518bca340605816b0ad095177715706816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 19 Dec 2024 16:45:12 +0100 Subject: [PATCH] Use json tags instead of toml and some other adjustments --- .github/workflows/test.yml | 2 +- archiveplugin/archiveplugin.go | 14 +++++------ archiveplugin/archiveplugin_test.go | 2 +- go.mod | 2 +- go.sum | 2 ++ model/helpers.go | 4 ++-- model/model.go | 17 +++++++------ server/server.go | 37 ++++++++++++++++++++++++++++- 8 files changed, 60 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6c5f74b..dd3f99d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,7 +7,7 @@ jobs: test: strategy: matrix: - go-version: [1.19.x] + go-version: [1.23.x] platform: [ macos-latest, ubuntu-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/archiveplugin/archiveplugin.go b/archiveplugin/archiveplugin.go index cf0fa78..faca743 100644 --- a/archiveplugin/archiveplugin.go +++ b/archiveplugin/archiveplugin.go @@ -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 { @@ -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 { diff --git a/archiveplugin/archiveplugin_test.go b/archiveplugin/archiveplugin_test.go index 90a58aa..9085d0b 100644 --- a/archiveplugin/archiveplugin_test.go +++ b/archiveplugin/archiveplugin_test.go @@ -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, diff --git a/go.mod b/go.mod index 1f8229f..ddf87ee 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index e0662b8..81f9ece 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/model/helpers.go b/model/helpers.go index 0c8fa66..24adbce 100644 --- a/model/helpers.go +++ b/model/helpers.go @@ -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 } diff --git a/model/model.go b/model/model.go index bec09dd..4b61a42 100644 --- a/model/model.go +++ b/model/model.go @@ -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. diff --git a/server/server.go b/server/server.go index 04693dd..0a5a9ac 100644 --- a/server/server.go +++ b/server/server.go @@ -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) + }, + }, + ) +}