Skip to content

Commit 5f57538

Browse files
committed
Merge branch 'build-offline-mode'
Add goeval.offline build: go build -tags goeval.offline In this mode -play and -share are disabled.
2 parents 092453c + 4615124 commit 5f57538

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

.github/workflows/go.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ jobs:
3535
# buildvcs=true to get a proper User-Agent string also during tests
3636
run: go test -v -buildvcs=true ./...
3737

38+
- name: Run tests for the goeval.offline build
39+
run: go test -v -buildvcs=true -tags goeval.offline ./...
40+
3841
- name: Test -play
3942
run: |
4043
out="$(./goeval -play 'fmt.Println(time.Now())')"

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ go1.24.4 X:rangefunc
9999
$ go install github.com/dolmen-go/goeval@latest
100100
```
101101

102+
Install with online features (`-play`, `-share`) disabled:
103+
104+
```console
105+
$ go install -tags=goeval.offline github.com/dolmen-go/goeval@latest
106+
```
107+
108+
102109
## 🗑️ Uninstall
103110

104111
```console

main.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,9 @@ func _main() error {
266266
// -E, like "cc -E"
267267
flagAction("E", actionDump, nil, "just dump the assembled source, without running it.")
268268
flagAction("Eplay", actionDumpPlay, nil, "just dump the assembled source for posting on https://go.dev/play")
269-
// TODO allow to optionally set a different endpoint
270-
flagAction("play", actionPlay, nil, "run the code remotely on https://go.dev/play")
271-
flagAction("share", actionShare, nil, "share the code on https://go.dev/play and print the URL.")
272269

273-
// TODO allow to optionally set a different endpoint for the Go Playground
270+
// -play, -share
271+
registerOnlineFlags()
274272

275273
flag.Func("o", "just build a binary, don't execute.", func(value string) (err error) {
276274
if action != actionDefault {
@@ -491,11 +489,11 @@ func _main() error {
491489
}
492490
case actionPlay:
493491
var cleanup func()
494-
srcFinal, tail, cleanup = prepareSub(playClient)
492+
srcFinal, tail, cleanup = prepareSubPlay()
495493
defer cleanup()
496494
case actionShare:
497495
var cleanup func()
498-
srcFinal, tail, cleanup = prepareSub(shareClient)
496+
srcFinal, tail, cleanup = prepareSubShare()
499497
defer cleanup()
500498
default: // actionDump, actionDumpPlay
501499
srcFinal = os.Stdout

sub.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !goeval.offline
2+
13
/*
24
Copyright 2025 Olivier Mengué.
35
@@ -25,13 +27,29 @@ import (
2527
"os/exec"
2628
)
2729

30+
func registerOnlineFlags() {
31+
// TODO allow to optionally set a different endpoint
32+
flagAction("play", actionPlay, nil, "run the code remotely on https://go.dev/play")
33+
flagAction("share", actionShare, nil, "share the code on https://go.dev/play and print the URL.")
34+
}
35+
2836
var (
2937
//go:embed sub/play/play.go
3038
playClient string
3139
//go:embed sub/share/share.go
3240
shareClient string
3341
)
3442

43+
// prepareSubPlay prepare the source code for compilation and execution of sub/play/play.go.
44+
func prepareSubPlay() (stdin *bytes.Buffer, tail func() error, cleanup func()) {
45+
return prepareSub(playClient)
46+
}
47+
48+
// prepareSubPlay prepare the source code for compilation and execution of sub/share/share.go.
49+
func prepareSubShare() (stdin *bytes.Buffer, tail func() error, cleanup func()) {
50+
return prepareSub(shareClient)
51+
}
52+
3553
// prepareSub prepares execution of a sub command via a "go run".
3654
// The returned stdin buffer may be filled with data.
3755
// cleanup must be called after cmd.Run() to clean the tempoary go source created.

sub_offline.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//go:build goeval.offline
2+
3+
/*
4+
Copyright 2025 Olivier Mengué.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package main
20+
21+
import (
22+
"bytes"
23+
"errors"
24+
"flag"
25+
)
26+
27+
const featureIsDisabled = "feature is disabled in offline build"
28+
29+
// registerOnlineFlags does nothing.
30+
func registerOnlineFlags() {
31+
flag.BoolFunc("play", featureIsDisabled+".", disabledFeature)
32+
flag.BoolFunc("share", featureIsDisabled+".", disabledFeature)
33+
}
34+
35+
func disabledFeature(string) error {
36+
return errors.New(featureIsDisabled)
37+
}
38+
39+
func prepareSubPlay() (*bytes.Buffer, func() error, func()) {
40+
panic("dead code in offline mode")
41+
}
42+
43+
func prepareSubShare() (*bytes.Buffer, func() error, func()) {
44+
panic("dead code in offline mode")
45+
}

0 commit comments

Comments
 (0)