From 7f2802ffcdc7257e92392b40414a6e54861dafa8 Mon Sep 17 00:00:00 2001 From: Jeremy Edwards <1312331+jeremyje@users.noreply.github.com> Date: Wed, 30 Nov 2022 08:51:32 +0000 Subject: [PATCH] Use broken out gomain library. --- Makefile | 2 +- go.mod | 4 +- go.sum | 16 +- gomain/.gitignore | 33 --- gomain/LICENSE | 201 ------------- gomain/Makefile | 70 ----- gomain/README.md | 34 --- gomain/cmd/example/Dockerfile | 51 ---- gomain/cmd/example/Dockerfile.windows | 50 ---- gomain/cmd/example/example.go | 52 ---- gomain/cmd/example/example_test.go | 28 -- gomain/debug.go | 39 --- gomain/debug_test.go | 30 -- gomain/go.mod | 5 - gomain/go.sum | 2 - gomain/gomain.go | 95 ------- gomain/gomain_nonwindows.go | 37 --- gomain/gomain_nonwindows_test.go | 75 ----- gomain/gomain_test.go | 141 --------- gomain/gomain_windows.go | 267 ------------------ gomain/gomain_windows_test.go | 53 ---- gomain/internal/runctx.go | 62 ---- gomain/internal/runctx_test.go | 55 ---- gomain/testing/testmain.go | 35 --- gomain/testing/testmain_test.go | 36 --- gomain/util.go | 32 --- gomain/util_test.go | 54 ---- .../options/openapiv2.proto | 6 +- 28 files changed, 8 insertions(+), 1557 deletions(-) delete mode 100644 gomain/.gitignore delete mode 100644 gomain/LICENSE delete mode 100644 gomain/Makefile delete mode 100644 gomain/README.md delete mode 100644 gomain/cmd/example/Dockerfile delete mode 100644 gomain/cmd/example/Dockerfile.windows delete mode 100644 gomain/cmd/example/example.go delete mode 100644 gomain/cmd/example/example_test.go delete mode 100644 gomain/debug.go delete mode 100644 gomain/debug_test.go delete mode 100644 gomain/go.mod delete mode 100644 gomain/go.sum delete mode 100644 gomain/gomain.go delete mode 100644 gomain/gomain_nonwindows.go delete mode 100644 gomain/gomain_nonwindows_test.go delete mode 100644 gomain/gomain_test.go delete mode 100644 gomain/gomain_windows.go delete mode 100644 gomain/gomain_windows_test.go delete mode 100644 gomain/internal/runctx.go delete mode 100644 gomain/internal/runctx_test.go delete mode 100644 gomain/testing/testmain.go delete mode 100644 gomain/testing/testmain_test.go delete mode 100644 gomain/util.go delete mode 100644 gomain/util_test.go diff --git a/Makefile b/Makefile index e44c74e..4fd8b78 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ BUILD_DATE = $(shell date -u +'%Y-%m-%dT%H:%M:%SZ') TAG := $(VERSION) export PATH := $(PWD)/build/toolchain/bin:$(PATH):/root/go/bin:/usr/local/go/bin:/usr/go/bin -GO = GOPRIVATE=github.com/jeremyje/gomain go +GO = go SOURCE_DIRS=$(shell go list ./... | grep -v '/vendor/') REGISTRY = ghcr.io/jeremyje diff --git a/go.mod b/go.mod index a81cf85..f8f4a58 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/google/go-cmp v0.5.9 - github.com/jeremyje/gomain v0.0.0-00010101000000-000000000000 + github.com/jeremyje/gomain v0.1.2 github.com/prometheus/client_golang v1.14.0 go.opentelemetry.io/otel v1.11.1 go.opentelemetry.io/otel/exporters/prometheus v0.33.0 @@ -28,5 +28,3 @@ require ( go.opentelemetry.io/otel/sdk v1.11.1 // indirect go.opentelemetry.io/otel/trace v1.11.1 // indirect ) - -replace github.com/jeremyje/gomain => ./gomain diff --git a/go.sum b/go.sum index 4a13ac8..c135fc2 100644 --- a/go.sum +++ b/go.sum @@ -133,6 +133,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/jeremyje/gomain v0.1.2 h1:uLHJIENbqy5VpgxzUgG3JjOIyNAg4gXQhOy4zlx//kk= +github.com/jeremyje/gomain v0.1.2/go.mod h1:YZdav9/Uy5b5BwuzC5vzeMcL+xg+qJOFr4G9481pnds= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -201,7 +203,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -210,28 +212,16 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opentelemetry.io/otel v1.11.0 h1:kfToEGMDq6TrVrJ9Vht84Y8y9enykSZzDDZglV0kIEk= -go.opentelemetry.io/otel v1.11.0/go.mod h1:H2KtuEphyMvlhZ+F7tg9GRhAOe60moNx61Ex+WmiKkk= go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4= go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE= -go.opentelemetry.io/otel/exporters/prometheus v0.32.3 h1:17C3zlnAFjmoyWueoQrESV/FvogMq2EmtJWpk7acX4Y= -go.opentelemetry.io/otel/exporters/prometheus v0.32.3/go.mod h1:7T6oLYJY+ydl+dEteIw+xCDJ7vaTRVWZ4CxEhpCUHFA= go.opentelemetry.io/otel/exporters/prometheus v0.33.0 h1:xXhPj7SLKWU5/Zd4Hxmd+X1C4jdmvc0Xy+kvjFx2z60= go.opentelemetry.io/otel/exporters/prometheus v0.33.0/go.mod h1:ZSmYfKdYWEdSDBB4njLBIwTf4AU2JNsH3n2quVQDebI= -go.opentelemetry.io/otel/metric v0.32.3 h1:dMpnJYk2KULXr0j8ph6N7+IcuiIQXlPXD4kix9t7L9c= -go.opentelemetry.io/otel/metric v0.32.3/go.mod h1:pgiGmKohxHyTPHGOff+vrtIH39/R9fiO/WoenUQ3kcc= go.opentelemetry.io/otel/metric v0.33.0 h1:xQAyl7uGEYvrLAiV/09iTJlp1pZnQ9Wl793qbVvED1E= go.opentelemetry.io/otel/metric v0.33.0/go.mod h1:QlTYc+EnYNq/M2mNk1qDDMRLpqCOj2f/r5c7Fd5FYaI= -go.opentelemetry.io/otel/sdk v1.11.0 h1:ZnKIL9V9Ztaq+ME43IUi/eo22mNsb6a7tGfzaOWB5fo= -go.opentelemetry.io/otel/sdk v1.11.0/go.mod h1:REusa8RsyKaq0OlyangWXaw97t2VogoO4SSEeKkSTAk= go.opentelemetry.io/otel/sdk v1.11.1 h1:F7KmQgoHljhUuJyA+9BiU+EkJfyX5nVVF4wyzWZpKxs= go.opentelemetry.io/otel/sdk v1.11.1/go.mod h1:/l3FE4SupHJ12TduVjUkZtlfFqDCQJlOlithYrdktys= -go.opentelemetry.io/otel/sdk/metric v0.32.3 h1:lY46wXBbo8IuPDlh1fpVPVy/bCT4wwo3RBYve6UaHOA= -go.opentelemetry.io/otel/sdk/metric v0.32.3/go.mod h1:nqJPheSpNDSGXhg22BQRgTQedRalfei6tZkmqTavDSk= go.opentelemetry.io/otel/sdk/metric v0.33.0 h1:oTqyWfksgKoJmbrs2q7O7ahkJzt+Ipekihf8vhpa9qo= go.opentelemetry.io/otel/sdk/metric v0.33.0/go.mod h1:xdypMeA21JBOvjjzDUtD0kzIcHO/SPez+a8HOzJPGp0= -go.opentelemetry.io/otel/trace v1.11.0 h1:20U/Vj42SX+mASlXLmSGBg6jpI1jQtv682lZtTAOVFI= -go.opentelemetry.io/otel/trace v1.11.0/go.mod h1:nyYjis9jy0gytE9LXGU+/m1sHTKbRY0fX0hulNNDP1U= go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ= go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/gomain/.gitignore b/gomain/.gitignore deleted file mode 100644 index 61018e1..0000000 --- a/gomain/.gitignore +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2022 Jeremy Edwards -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, built with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out - -.vscode/ - -*.log - -build/ -*.ndjson diff --git a/gomain/LICENSE b/gomain/LICENSE deleted file mode 100644 index 1c7c1ee..0000000 --- a/gomain/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2022 Jeremy Edwards - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/gomain/Makefile b/gomain/Makefile deleted file mode 100644 index 5086205..0000000 --- a/gomain/Makefile +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright 2022 Jeremy Edwards -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -GO = GO111MODULE=on go -DOCKER = DOCKER_CLI_EXPERIMENTAL=enabled docker - -BASE_VERSION = 0.0.0-dev -SHORT_SHA = $(shell git rev-parse --short=7 HEAD | tr -d [:punct:]) -VERSION_SUFFIX = $(SHORT_SHA) -VERSION = $(BASE_VERSION)-$(VERSION_SUFFIX) -BUILD_DATE = $(shell date -u +'%Y-%m-%dT%H:%M:%SZ') -TAG := v$(VERSION) - -export PATH := $(PWD)/build/toolchain/bin:$(PATH):/root/go/bin:/usr/local/go/bin:/usr/go/bin -GO = go -SOURCE_DIRS=$(shell go list ./... | grep -v '/vendor/') - -PROTOS = proto/hardware.pb.go - -ASSETS = $(PROTOS) -NICHE_PLATFORMS = -LINUX_PLATFORMS = linux_386 linux_amd64 linux_arm_v5 linux_arm_v6 linux_arm_v7 linux_arm64 linux_s390x linux_ppc64le linux_riscv64 linux_mips64le linux_mips linux_mipsle linux_mips64 -LINUX_NICHE_PLATFORMS = -WINDOWS_PLATFORMS = windows_386 windows_amd64 -MAIN_PLATFORMS = windows_amd64 linux_amd64 linux_arm64 -ALL_PLATFORMS = $(LINUX_PLATFORMS) $(LINUX_NICHE_PLATFORMS) $(WINDOWS_PLATFORMS) $(foreach niche,$(NICHE_PLATFORMS),$(niche)_amd64 $(niche)_arm64) -ALL_APPS = example - -MAIN_BINARIES = $(foreach app,$(ALL_APPS),$(foreach platform,$(MAIN_PLATFORMS),build/bin/$(platform)/$(app)$(if $(findstring windows_,$(platform)),.exe,))) -ALL_BINARIES = $(foreach app,$(ALL_APPS),$(foreach platform,$(ALL_PLATFORMS),build/bin/$(platform)/$(app)$(if $(findstring windows_,$(platform)),.exe,))) - -binaries: $(MAIN_BINARIES) -all: $(ALL_BINARIES) - -build/bin/%: - GOOS=$(firstword $(subst _, ,$(notdir $(abspath $(dir $@))))) GOARCH=$(word 2, $(subst _, ,$(notdir $(abspath $(dir $@))))) GOARM=$(subst v,,$(word 3, $(subst _, ,$(notdir $(abspath $(dir $@)))))) CGO_ENABLED=0 $(GO) build -o $@ cmd/$(basename $(notdir $@))/$(basename $(notdir $@)).go - touch $@ - -lint: - $(GO) fmt ./... - $(GO) vet ./... - -test: - $(GO) test -race ${SOURCE_DIRS} -cover - -coverage.txt: - for sfile in ${SOURCE_DIRS} ; do \ - go test -race "$$sfile" -coverprofile=package.coverage -covermode=atomic; \ - if [ -f package.coverage ]; then \ - cat package.coverage >> coverage.txt; \ - $(RM) package.coverage; \ - fi; \ - done - -clean: - -chmod -R +w build/ - rm -rf build/ - -.PHONY: all run lint test clean diff --git a/gomain/README.md b/gomain/README.md deleted file mode 100644 index e154c80..0000000 --- a/gomain/README.md +++ /dev/null @@ -1,34 +0,0 @@ -# gomain - -A main harness for Go applications that run for a long time. This harness supports: - -* Run as Windows Service. -* Handle OS signals gracefully. -* Basic debug signal handling. - -## Usage - -```go -import ( - "github.com/jeremyje/gomain" -) - -func main() { - gomain.Run(runServer, gomain.Config{ - ServiceName: "App", - ServiceDescription: "App does stuff.", - Command: *flagValue, - }) -} - -func runServer(wait func()) error { - server := New() - go func() { - wait() - // Terminates the server and causes Run to complete. - server.Shutdown() - }() - return server.Run() -} - -``` diff --git a/gomain/cmd/example/Dockerfile b/gomain/cmd/example/Dockerfile deleted file mode 100644 index 22f99a7..0000000 --- a/gomain/cmd/example/Dockerfile +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright 2022 Jeremy Edwards -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -FROM --platform=$BUILDPLATFORM gcr.io/distroless/static:nonroot - -ARG BINARY_PATH - -WORKDIR /app/ - -COPY --chown=nonroot $BINARY_PATH /app/example -COPY --chown=nonroot LICENSE /app/LICENSE - -EXPOSE 80 -ENTRYPOINT ["/app/example"] - -# Standardized Docker Image Labels -# https://github.com/opencontainers/image-spec/blob/master/annotations.md -LABEL \ - org.opencontainers.image.created="${BUILD_TIME}" \ - org.opencontainers.image.authors="Jeremyje " \ - org.opencontainers.image.url="https://github.com/jeremyje/gomain" \ - org.opencontainers.image.documentation="https://github.com/jeremyje/gomain" \ - org.opencontainers.image.source="https://github.com/jeremyje/gomain" \ - org.opencontainers.image.version="${BUILD_VERSION}" \ - org.opencontainers.image.revision="1" \ - org.opencontainers.image.vendor="Jeremy Edwards" \ - org.opencontainers.image.licenses="Apache-2.0" \ - org.opencontainers.image.ref.name="" \ - org.opencontainers.image.title="${BINARY_NAME}" \ - org.opencontainers.image.description="Gomain Example App" \ - org.label-schema.schema-version="1.0" \ - org.label-schema.build-date=$BUILD_DATE \ - org.label-schema.url="https://github.com/jeremyje/gomain" \ - org.label-schema.vcs-url="https://github.com/jeremyje/gomain" \ - org.label-schema.version=$BUILD_VERSION \ - org.label-schema.vcs-ref=$VCS_REF \ - org.label-schema.vendor="Jeremy Edwards" \ - org.label-schema.name="${BINARY_NAME}" \ - org.label-schema.description="Gomain Example App" \ - org.label-schema.usage="https://github.com/jeremyje/gomain" diff --git a/gomain/cmd/example/Dockerfile.windows b/gomain/cmd/example/Dockerfile.windows deleted file mode 100644 index 0e29e27..0000000 --- a/gomain/cmd/example/Dockerfile.windows +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2022 Jeremy Edwards -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -ARG WINDOWS_VERSION=1809 - -FROM mcr.microsoft.com/windows/nanoserver:${WINDOWS_VERSION} - -COPY bin/go/windows_amd64/example.exe /app/example.exe -COPY LICENSE /app/LICENSE - -USER ContainerUser -EXPOSE 80 -ENTRYPOINT ["/app/example.exe"] - -# Standardized Docker Image Labels -# https://github.com/opencontainers/image-spec/blob/master/annotations.md -LABEL \ - org.opencontainers.image.created="${BUILD_TIME}" \ - org.opencontainers.image.authors="Jeremyje " \ - org.opencontainers.image.url="https://github.com/jeremyje/gomain" \ - org.opencontainers.image.documentation="https://github.com/jeremyje/gomain" \ - org.opencontainers.image.source="https://github.com/jeremyje/gomain" \ - org.opencontainers.image.version="${BUILD_VERSION}" \ - org.opencontainers.image.revision="1" \ - org.opencontainers.image.vendor="Jeremy Edwards" \ - org.opencontainers.image.licenses="Apache-2.0" \ - org.opencontainers.image.ref.name="" \ - org.opencontainers.image.title="${BINARY_NAME}" \ - org.opencontainers.image.description="Gomain Example App" \ - org.label-schema.schema-version="1.0" \ - org.label-schema.build-date=$BUILD_DATE \ - org.label-schema.url="https://github.com/jeremyje/gomain" \ - org.label-schema.vcs-url="https://github.com/jeremyje/gomain" \ - org.label-schema.version=$BUILD_VERSION \ - org.label-schema.vcs-ref=$VCS_REF \ - org.label-schema.vendor="Jeremy Edwards" \ - org.label-schema.name="${BINARY_NAME}" \ - org.label-schema.description="Gomain Example App" \ - org.label-schema.usage="https://github.com/jeremyje/gomain" diff --git a/gomain/cmd/example/example.go b/gomain/cmd/example/example.go deleted file mode 100644 index f09b093..0000000 --- a/gomain/cmd/example/example.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "context" - "net/http" - - "github.com/jeremyje/gomain" -) - -func main() { - gomain.Run(appMain, gomain.Config{ - ServiceName: "Example", - ServiceDescription: "Example Service Description", - Command: "", - }) -} - -func appMain(waitFunc func()) error { - mux := http.NewServeMux() - mux.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) { - resp.Write([]byte(req.URL.Path)) - }) - s := &http.Server{ - Handler: mux, - Addr: "localhost:0", - } - - go func() { - waitFunc() - ctx := context.Background() - s.Shutdown(ctx) - }() - - if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { - return err - } - return nil -} diff --git a/gomain/cmd/example/example_test.go b/gomain/cmd/example/example_test.go deleted file mode 100644 index 818bbd5..0000000 --- a/gomain/cmd/example/example_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "testing" - - gomainTesting "github.com/jeremyje/gomain/testing" -) - -func TestAppMain(t *testing.T) { - close := gomainTesting.Main(appMain) - if err := close(); err != nil { - t.Error(err) - } -} diff --git a/gomain/debug.go b/gomain/debug.go deleted file mode 100644 index b92c3a0..0000000 --- a/gomain/debug.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gomain - -import ( - "log" - "runtime" -) - -func logStackDump() { - log.Printf("%s", string(getStackDump())) -} - -func getStackDump() []byte { - var ( - buf []byte - stackSize int - ) - bufferLen := 16384 - for stackSize == len(buf) { - buf = make([]byte, bufferLen) - stackSize = runtime.Stack(buf, true) - bufferLen *= 2 - } - buf = buf[:stackSize] - return buf -} diff --git a/gomain/debug_test.go b/gomain/debug_test.go deleted file mode 100644 index 86bd616..0000000 --- a/gomain/debug_test.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gomain - -import ( - "strings" - "testing" -) - -func TestGetStackDump(t *testing.T) { - dump := string(getStackDump()) - if !strings.Contains(dump, t.Name()) { - t.Errorf("expected '%s' in stack dump\n%s", t.Name(), dump) - } - - // Make sure this doesn't crash or something weird. - logStackDump() -} diff --git a/gomain/go.mod b/gomain/go.mod deleted file mode 100644 index fd82d3b..0000000 --- a/gomain/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module github.com/jeremyje/gomain - -go 1.19 - -require golang.org/x/sys v0.2.0 diff --git a/gomain/go.sum b/gomain/go.sum deleted file mode 100644 index beac707..0000000 --- a/gomain/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= -golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/gomain/gomain.go b/gomain/gomain.go deleted file mode 100644 index b85d980..0000000 --- a/gomain/gomain.go +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gomain - -import ( - "log" - "os" - "os/signal" - "syscall" - - "github.com/jeremyje/gomain/internal" -) - -type MainCtx interface { - Wait() -} - -type MainFunc func(func()) error - -type Config struct { - ServiceName string - ServiceDescription string - Command string -} - -func Run(f MainFunc, cfg Config) { - platformRun(f, cfg) -} - -func getTerminalSignals() []os.Signal { - return []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGABRT} -} - -func runInteractive(f MainFunc) { - sigCh := make(chan os.Signal, 1) - signal.Notify(sigCh, getTerminalSignals()...) - defer func() { - signal.Stop(sigCh) - close(sigCh) - }() - runInteractiveInternal(f, sigCh) -} - -func runInteractiveInternal(f MainFunc, sigCh chan os.Signal) { - mainErrCh := make(chan error, 1) - - mc := internal.NewRunCtx() - defer mc.Close() - - go func() { - mainErrCh <- f(mc.Wait) - close(mainErrCh) - }() - - select { - case err := <-mainErrCh: - handleError(err) - return - case sig := <-sigCh: - if handleSignal(sig) { - signal.Stop(sigCh) - mc.Kill() - } - } -} - -func handleError(err error) { - if err != nil { - log.Printf("ERROR: %s", err) - } -} - -func handleSignalBase(sig os.Signal) bool { - switch sig { - case syscall.SIGINT, syscall.SIGTERM: - return true - case syscall.SIGKILL, syscall.SIGABRT: - logStackDump() - return true - default: - return false - } -} diff --git a/gomain/gomain_nonwindows.go b/gomain/gomain_nonwindows.go deleted file mode 100644 index 03ec0de..0000000 --- a/gomain/gomain_nonwindows.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build !windows -// +build !windows - -package gomain - -import ( - "os" - "syscall" -) - -func platformRun(f MainFunc, cfg Config) { - runInteractive(f) -} - -func handleSignal(sig os.Signal) bool { - switch sig { - case syscall.SIGUSR1: - logStackDump() - return false - default: - return handleSignalBase(sig) - } -} diff --git a/gomain/gomain_nonwindows_test.go b/gomain/gomain_nonwindows_test.go deleted file mode 100644 index 29b5040..0000000 --- a/gomain/gomain_nonwindows_test.go +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build !windows -// +build !windows - -package gomain - -import ( - "os" - "syscall" -) - -var ( - handleSignalTestCases = []struct { - input os.Signal - want bool - }{ - {input: syscall.SIGUSR1, want: false}, - {input: syscall.SIGINT, want: true}, - {input: syscall.SIGTERM, want: true}, - {input: syscall.SIGKILL, want: true}, - {input: syscall.SIGABRT, want: true}, - } -) - -func getAllSignals() []os.Signal { - return []os.Signal{ - syscall.SIGABRT, - syscall.SIGALRM, - syscall.SIGBUS, - syscall.SIGCHLD, - syscall.SIGCLD, - syscall.SIGCONT, - syscall.SIGFPE, - syscall.SIGHUP, - syscall.SIGILL, - syscall.SIGINT, - syscall.SIGIO, - syscall.SIGIOT, - syscall.SIGKILL, - syscall.SIGPIPE, - syscall.SIGPOLL, - syscall.SIGPROF, - syscall.SIGPWR, - syscall.SIGSEGV, - syscall.SIGSTKFLT, - syscall.SIGSTOP, - syscall.SIGSYS, - syscall.SIGTERM, - syscall.SIGTRAP, - syscall.SIGTSTP, - syscall.SIGTTIN, - syscall.SIGTTOU, - syscall.SIGUNUSED, - syscall.SIGURG, - syscall.SIGUSR1, - syscall.SIGUSR2, - syscall.SIGVTALRM, - syscall.SIGWINCH, - syscall.SIGXCPU, - syscall.SIGXFSZ, - } -} diff --git a/gomain/gomain_test.go b/gomain/gomain_test.go deleted file mode 100644 index 65e88aa..0000000 --- a/gomain/gomain_test.go +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gomain - -import ( - "fmt" - "os" - "sync" - "testing" - "time" -) - -var ( - waitForeverFuncs = map[string]MainFunc{ - "waitForeverFunc": func(wait func()) error { - wait() - return nil - }, - "waitForeverFailingFunc": func(wait func()) error { - wait() - return fmt.Errorf("failed") - }, - } - - immediateMainFuncs = map[string]MainFunc{ - "immediateReturnFunc": func(wait func()) error { - return nil - }, - "immediateFailFunc": func(wait func()) error { - return fmt.Errorf("failed") - }, - } -) - -func getAllMainFuncs() map[string]MainFunc { - mains := map[string]MainFunc{} - for k, v := range waitForeverFuncs { - mains[k] = v - } - for k, v := range immediateMainFuncs { - mains[k] = v - } - return mains -} - -func TestHandleSignalBase(t *testing.T) { - for _, tc := range handleSignalTestCases { - tc := tc - t.Run(tc.input.String(), func(t *testing.T) { - t.Parallel() - got := handleSignal(tc.input) - if got != tc.want { - t.Fatalf("expected: %t, got: %t", tc.want, got) - } - }) - } -} - -func TestRunInteractiveInternal(t *testing.T) { - for mainName, mainFunc := range getAllMainFuncs() { - mainFunc := mainFunc - for _, tc := range handleSignalTestCases { - tc := tc - - t.Run(fmt.Sprintf("%s - %s", mainName, tc.input.String()), func(t *testing.T) { - t.Parallel() - sigCh := make(chan os.Signal, 1) - - go func() { - time.Sleep(time.Millisecond * 100) - sigCh <- tc.input - }() - - runInteractiveInternal(mainFunc, sigCh) - }) - } - } -} - -func TestRunInteractiveInternalAllSignals(t *testing.T) { - for mainName, mainFunc := range getAllMainFuncs() { - mainFunc := mainFunc - for _, signal := range getAllSignals() { - signal := signal - - t.Run(fmt.Sprintf("%s - %s", mainName, signal.String()), func(t *testing.T) { - t.Parallel() - sigCh := make(chan os.Signal, 1) - var m sync.Mutex - closed := false - defer func() { - m.Lock() - closed = true - m.Unlock() - close(sigCh) - }() - - go func() { - time.Sleep(time.Millisecond * 100) - m.Lock() - if !closed { - sigCh <- signal - } - m.Unlock() - }() - - runInteractiveInternal(mainFunc, sigCh) - }) - } - } -} - -func TestRunInteractiveAllSignals(t *testing.T) { - for mainName, mainFunc := range immediateMainFuncs { - mainFunc := mainFunc - for _, signal := range getAllSignals() { - signal := signal - t.Run(fmt.Sprintf("%s - %s", mainName, signal), func(t *testing.T) { - t.Parallel() - runInteractive(mainFunc) - }) - - t.Run(fmt.Sprintf("%s - %s", mainName, signal), func(t *testing.T) { - t.Parallel() - Run(mainFunc, Config{}) - }) - } - } -} diff --git a/gomain/gomain_windows.go b/gomain/gomain_windows.go deleted file mode 100644 index 46c1c25..0000000 --- a/gomain/gomain_windows.go +++ /dev/null @@ -1,267 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build windows -// +build windows - -package gomain - -import ( - "fmt" - "log" - "os" - "strings" - "time" - - "github.com/jeremyje/gomain/internal" - "golang.org/x/sys/windows/svc" - "golang.org/x/sys/windows/svc/debug" - "golang.org/x/sys/windows/svc/eventlog" - "golang.org/x/sys/windows/svc/mgr" -) - -func platformRun(f MainFunc, cfg Config) { - svcMode, err := svc.IsWindowsService() - if err != nil { - log.Fatalf("failed to determine if we are running in service: %v", err) - } - if svcMode { - runService(f, cfg.ServiceName, false) - } else { - if cfg.Command != "" { - serviceControl(f, cfg) - } else { - runInteractive(f) - } - } -} - -func serviceControl(f MainFunc, cfg Config) { - var err error - svcName := cfg.ServiceName - description := cfg.ServiceDescription - cmd := cfg.Command - - switch strings.ToLower(cmd) { - case "debug": - runService(f, svcName, true) - return - case "install": - err = installService(svcName, description) - case "remove": - err = removeService(svcName) - case "start": - err = startService(svcName) - case "stop": - err = controlService(svcName, svc.Stop, svc.Stopped) - case "pause": - err = controlService(svcName, svc.Pause, svc.Paused) - case "continue": - err = controlService(svcName, svc.Continue, svc.Running) - default: - usage(fmt.Sprintf("invalid command %s", cmd)) - } - handleError(err) -} - -func usage(errmsg string) { - fmt.Fprintf(os.Stderr, - "%s\n\n"+ - "usage: %s \n"+ - " where is one of\n"+ - " install, remove, debug, start, stop, pause or continue.\n", - errmsg, os.Args[0]) - os.Exit(2) -} - -func installService(name, desc string) error { - exepath := exePath() - m, err := mgr.Connect() - if err != nil { - return err - } - defer m.Disconnect() - s, err := m.OpenService(name) - if err == nil { - s.Close() - return fmt.Errorf("service %s already exists", name) - } - s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc}, "is", "auto-started") - if err != nil { - return err - } - defer s.Close() - err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info) - if err != nil { - s.Delete() - return fmt.Errorf("SetupEventLogSource() failed: %s", err) - } - return nil -} - -func removeService(name string) error { - m, err := mgr.Connect() - if err != nil { - return err - } - defer m.Disconnect() - s, err := m.OpenService(name) - if err != nil { - return fmt.Errorf("service %s is not installed", name) - } - defer s.Close() - err = s.Delete() - if err != nil { - return err - } - err = eventlog.Remove(name) - if err != nil { - return fmt.Errorf("RemoveEventLogSource() failed: %s", err) - } - return nil -} - -func startService(name string) error { - m, err := mgr.Connect() - if err != nil { - return err - } - defer m.Disconnect() - s, err := m.OpenService(name) - if err != nil { - return fmt.Errorf("could not access service: %v", err) - } - defer s.Close() - err = s.Start("is", "manual-started") - if err != nil { - return fmt.Errorf("could not start service: %v", err) - } - return nil -} - -func controlService(name string, c svc.Cmd, to svc.State) error { - m, err := mgr.Connect() - if err != nil { - return err - } - defer m.Disconnect() - s, err := m.OpenService(name) - if err != nil { - return fmt.Errorf("could not access service: %v", err) - } - defer s.Close() - status, err := s.Control(c) - if err != nil { - return fmt.Errorf("could not send control=%d: %v", c, err) - } - timeout := time.Now().Add(10 * time.Second) - for status.State != to { - if timeout.Before(time.Now()) { - return fmt.Errorf("timeout waiting for service to go to state=%d", to) - } - time.Sleep(300 * time.Millisecond) - status, err = s.Query() - if err != nil { - return fmt.Errorf("could not retrieve service status: %v", err) - } - } - return nil -} - -var elog debug.Log - -type windowsService struct { - f MainFunc -} - -func (ws *windowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { - const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown - changes <- svc.Status{State: svc.StartPending} - changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} - - mc := internal.NewRunCtx() - defer mc.Close() - - mainErrCh := make(chan error, 1) - go func() { - mainErrCh <- ws.f(mc.Wait) - }() - defer close(mainErrCh) - - done := false - for !done { - select { - case cr := <-r: - if ws.handleControl(cr, changes) { - mc.Kill() - } - case err := <-mainErrCh: - if err != nil { - elog.Error(1, err.Error()) - } - done = true - } - } - changes <- svc.Status{State: svc.StopPending} - changes <- svc.Status{State: svc.Stopped} - return -} - -func (ws *windowsService) handleControl(cr svc.ChangeRequest, changes chan<- svc.Status) bool { - done := false - switch cr.Cmd { - case svc.Interrogate: - changes <- cr.CurrentStatus - // Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4 - time.Sleep(100 * time.Millisecond) - changes <- cr.CurrentStatus - case svc.Stop, svc.Shutdown: - elog.Info(1, "Stopping Service") - done = true - default: - elog.Error(1, fmt.Sprintf("unexpected control request #%d", cr)) - } - return done -} - -func runService(f MainFunc, name string, isDebug bool) { - var err error - if isDebug { - elog = debug.New(name) - } else { - elog, err = eventlog.Open(name) - if err != nil { - return - } - } - defer elog.Close() - - elog.Info(1, fmt.Sprintf("starting %s service", name)) - run := svc.Run - if isDebug { - run = debug.Run - } - err = run(name, &windowsService{ - f: f, - }) - if err != nil { - elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err)) - return - } - elog.Info(1, fmt.Sprintf("%s service stopped", name)) -} - -func handleSignal(sig os.Signal) bool { - return handleSignalBase(sig) -} diff --git a/gomain/gomain_windows_test.go b/gomain/gomain_windows_test.go deleted file mode 100644 index 37461e8..0000000 --- a/gomain/gomain_windows_test.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build windows -// +build windows - -package gomain - -import ( - "os" - "syscall" -) - -var ( - handleSignalTestCases = []struct { - input os.Signal - want bool - }{ - {input: syscall.SIGINT, want: true}, - {input: syscall.SIGTERM, want: true}, - {input: syscall.SIGKILL, want: true}, - {input: syscall.SIGABRT, want: true}, - } -) - -func getAllSignals() []os.Signal { - return []os.Signal{ - syscall.SIGHUP, - syscall.SIGINT, - syscall.SIGQUIT, - syscall.SIGILL, - syscall.SIGTRAP, - syscall.SIGABRT, - syscall.SIGBUS, - syscall.SIGFPE, - syscall.SIGKILL, - syscall.SIGSEGV, - syscall.SIGPIPE, - syscall.SIGALRM, - syscall.SIGTERM, - } -} diff --git a/gomain/internal/runctx.go b/gomain/internal/runctx.go deleted file mode 100644 index e4a7cf6..0000000 --- a/gomain/internal/runctx.go +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package internal - -import ( - "os" - "sync" -) - -type RunCtx struct { - sync.RWMutex - waitCh chan os.Signal - closed bool -} - -func NewRunCtx() *RunCtx { - return &RunCtx{ - waitCh: make(chan os.Signal, 1), - closed: false, - } -} - -func (mc *RunCtx) Kill() { - mc.RLock() - if !mc.closed { - mc.waitCh <- os.Kill - } else { - mc.RUnlock() - return - } - mc.RUnlock() -} - -func (mc *RunCtx) Wait() { - <-mc.waitCh -} - -func (mc *RunCtx) Close() { - doClose := false - mc.Lock() - if !mc.closed { - doClose = true - mc.closed = true - } - - mc.Unlock() - if doClose { - close(mc.waitCh) - } -} diff --git a/gomain/internal/runctx_test.go b/gomain/internal/runctx_test.go deleted file mode 100644 index 9cf2cca..0000000 --- a/gomain/internal/runctx_test.go +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package internal - -import ( - "testing" - "time" -) - -func TestNewRunCtx(t *testing.T) { - rCtx := NewRunCtx() - - rCtx.Close() - rCtx.Kill() - rCtx.Close() - rCtx.Kill() - rCtx.Close() - rCtx.Close() - rCtx.Kill() - rCtx.Kill() -} - -func TestWaitAfterKill(t *testing.T) { - rCtx := NewRunCtx() - - rCtx.Kill() - rCtx.Close() - rCtx.Wait() -} - -func TestWait(t *testing.T) { - rCtx := NewRunCtx() - done := make(chan bool, 1) - - go func() { - rCtx.Wait() - done <- true - }() - - time.Sleep(time.Millisecond * 100) - rCtx.Kill() - <-done -} diff --git a/gomain/testing/testmain.go b/gomain/testing/testmain.go deleted file mode 100644 index 6d65320..0000000 --- a/gomain/testing/testmain.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package testing - -import ( - "github.com/jeremyje/gomain" - "github.com/jeremyje/gomain/internal" -) - -func Main(f gomain.MainFunc) func() error { - errCh := make(chan error, 1) - runCtx := internal.NewRunCtx() - go func() { - errCh <- f(runCtx.Wait) - close(errCh) - }() - - return func() error { - runCtx.Kill() - err := <-errCh - return err - } -} diff --git a/gomain/testing/testmain_test.go b/gomain/testing/testmain_test.go deleted file mode 100644 index 911d9c7..0000000 --- a/gomain/testing/testmain_test.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package testing - -import ( - "testing" -) - -func TestTestMainAsync(t *testing.T) { - ready := make(chan int) - m := func(waitFunc func()) error { - ready <- 1 - waitFunc() - return nil - } - close := Main(m) - val := <-ready - if val != 1 { - t.Errorf("got: %d, want: 1", val) - } - if err := close(); err != nil { - t.Error(err) - } -} diff --git a/gomain/util.go b/gomain/util.go deleted file mode 100644 index cc6fc70..0000000 --- a/gomain/util.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gomain - -import ( - "os" - "path/filepath" -) - -func exePath() string { - return exePathFromPath(os.Args[0]) -} - -func exePathFromPath(prog string) string { - absPath, err := filepath.Abs(prog) - if err != nil { - return prog - } - return absPath -} diff --git a/gomain/util_test.go b/gomain/util_test.go deleted file mode 100644 index be76b8c..0000000 --- a/gomain/util_test.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2022 Jeremy Edwards -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package gomain - -import ( - "os" - "path/filepath" - "testing" -) - -func TestExePath(t *testing.T) { - ep := exePath() - if ep == "" { - t.Error("exePath() should not be empty") - } -} - -func TestExePathFromPath(t *testing.T) { - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - tests := []struct { - input string - want string - }{ - {input: "util_test", want: filepath.Join(dir, "util_test")}, - {input: "util_test.exe", want: filepath.Join(dir, "util_test.exe")}, - } - - for _, tc := range tests { - tc := tc - t.Run(tc.input, func(t *testing.T) { - t.Parallel() - - got := exePathFromPath(tc.input) - if got != tc.want { - t.Fatalf("expected: %v, got: %v", tc.want, got) - } - }) - } -} diff --git a/third_party/grpc_gateway/include/protoc-gen-openapiv2/options/openapiv2.proto b/third_party/grpc_gateway/include/protoc-gen-openapiv2/options/openapiv2.proto index ec88735..34ddb52 100644 --- a/third_party/grpc_gateway/include/protoc-gen-openapiv2/options/openapiv2.proto +++ b/third_party/grpc_gateway/include/protoc-gen-openapiv2/options/openapiv2.proto @@ -34,7 +34,7 @@ enum Scheme { // }; // license: { // name: "BSD 3-Clause License"; -// url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt"; +// url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/main/LICENSE.txt"; // }; // }; // schemes: HTTPS; @@ -309,7 +309,7 @@ message Response { // }; // license: { // name: "BSD 3-Clause License"; -// url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt"; +// url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/main/LICENSE.txt"; // }; // }; // ... @@ -377,7 +377,7 @@ message Contact { // ... // license: { // name: "BSD 3-Clause License"; -// url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt"; +// url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/main/LICENSE.txt"; // }; // ... // };