Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: reduce duplicated code calling PanicOnError #1043

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 81 additions & 92 deletions GHGEN/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,77 @@ package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/ooni/probe-cli/v3/internal/must"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)

func newJob(w io.Writer, name, runsOn, needs string, permissions map[string]string) {
mustFprintf(w, " %s:\n", name)
mustFprintf(w, " runs-on: %s\n", runsOn)
must.Fprintf(w, " %s:\n", name)
must.Fprintf(w, " runs-on: %s\n", runsOn)
if needs != "" {
mustFprintf(w, " needs: %s\n", needs)
must.Fprintf(w, " needs: %s\n", needs)
}
if len(permissions) > 0 {
mustFprintf(w, " permissions:\n")
must.Fprintf(w, " permissions:\n")
for key, value := range permissions {
mustFprintf(w, " %s: %s\n", key, value)
must.Fprintf(w, " %s: %s\n", key, value)
}
}
mustFprintf(w, " steps:\n")
must.Fprintf(w, " steps:\n")
}

func newStepCheckout(w io.Writer) {
mustFprintf(w, " - uses: actions/checkout@v2\n")
mustFprintf(w, " with:\n")
mustFprintf(w, " fetch-depth: 0\n")
mustFprintf(w, "\n")
must.Fprintf(w, " - uses: actions/checkout@v2\n")
must.Fprintf(w, " with:\n")
must.Fprintf(w, " fetch-depth: 0\n")
must.Fprintf(w, "\n")
}

func newStepSetupGo(w io.Writer, cacheName string) {
mustFprintf(w, " - name: Get GOVERSION content\n")
mustFprintf(w, " id: goversion\n")
mustFprintf(w, " run: echo ::set-output name=version::$(cat GOVERSION)\n")
mustFprintf(w, " - uses: magnetikonline/action-golang-cache@v2\n")
mustFprintf(w, " with:\n")
mustFprintf(w, " go-version: \"${{ steps.goversion.outputs.version }}\"\n")
mustFprintf(w, " cache-key-suffix: \"-%s-${{ steps.goversion.outputs.version }}\"\n", cacheName)
mustFprintf(w, "\n")
must.Fprintf(w, " - name: Get GOVERSION content\n")
must.Fprintf(w, " id: goversion\n")
must.Fprintf(w, " run: echo ::set-output name=version::$(cat GOVERSION)\n")
must.Fprintf(w, " - uses: magnetikonline/action-golang-cache@v2\n")
must.Fprintf(w, " with:\n")
must.Fprintf(w, " go-version: \"${{ steps.goversion.outputs.version }}\"\n")
must.Fprintf(w, " cache-key-suffix: \"-%s-${{ steps.goversion.outputs.version }}\"\n", cacheName)
must.Fprintf(w, "\n")
}

func newStepSetupPsiphon(w io.Writer) {
mustFprintf(w, " - run: |\n")
mustFprintf(w, " echo -n $PSIPHON_CONFIG_KEY > ./internal/engine/psiphon-config.key\n")
mustFprintf(w, " echo $PSIPHON_CONFIG_JSON_AGE_BASE64 | base64 -d > ./internal/engine/psiphon-config.json.age\n")
mustFprintf(w, " env:\n")
mustFprintf(w, " PSIPHON_CONFIG_KEY: ${{ secrets.PSIPHON_CONFIG_KEY }}\n")
mustFprintf(w, " PSIPHON_CONFIG_JSON_AGE_BASE64: ${{ secrets.PSIPHON_CONFIG_JSON_AGE_BASE64 }}\n")
mustFprintf(w, "\n")
must.Fprintf(w, " - run: |\n")
must.Fprintf(w, " echo -n $PSIPHON_CONFIG_KEY > ./internal/engine/psiphon-config.key\n")
must.Fprintf(w, " echo $PSIPHON_CONFIG_JSON_AGE_BASE64 | base64 -d > ./internal/engine/psiphon-config.json.age\n")
must.Fprintf(w, " env:\n")
must.Fprintf(w, " PSIPHON_CONFIG_KEY: ${{ secrets.PSIPHON_CONFIG_KEY }}\n")
must.Fprintf(w, " PSIPHON_CONFIG_JSON_AGE_BASE64: ${{ secrets.PSIPHON_CONFIG_JSON_AGE_BASE64 }}\n")
must.Fprintf(w, "\n")
}

func newStepMake(w io.Writer, target string) {
mustFprintf(w, " - run: make %s\n", target)
mustFprintf(w, "\n")
must.Fprintf(w, " - run: make %s\n", target)
must.Fprintf(w, "\n")
}

func newStepUploadArtifacts(w io.Writer, artifacts []string) {
for _, arti := range artifacts {
mustFprintf(w, " - uses: actions/upload-artifact@v2\n")
mustFprintf(w, " with:\n")
mustFprintf(w, " name: %s\n", filepath.Base(arti))
mustFprintf(w, " path: %s\n", arti)
mustFprintf(w, "\n")
must.Fprintf(w, " - uses: actions/upload-artifact@v2\n")
must.Fprintf(w, " with:\n")
must.Fprintf(w, " name: %s\n", filepath.Base(arti))
must.Fprintf(w, " path: %s\n", arti)
must.Fprintf(w, "\n")
}
}

func newStepDownloadArtifacts(w io.Writer, artifacts []string) {
for _, arti := range artifacts {
mustFprintf(w, " - uses: actions/download-artifact@v2\n")
mustFprintf(w, " with:\n")
mustFprintf(w, " name: %s\n", filepath.Base(arti))
mustFprintf(w, "\n")
must.Fprintf(w, " - uses: actions/download-artifact@v2\n")
must.Fprintf(w, " with:\n")
must.Fprintf(w, " name: %s\n", filepath.Base(arti))
must.Fprintf(w, "\n")
}
}

Expand All @@ -87,91 +87,80 @@ func newStepGHPublish(w io.Writer, artifacts []string) {
for _, arti := range artifacts {
artifactsNames = append(artifactsNames, filepath.Base(arti))
}
mustFprintf(w, " - run: ./script/ghpublish.bash %s\n", strings.Join(artifactsNames, " "))
mustFprintf(w, " env:\n")
mustFprintf(w, " GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n")
mustFprintf(w, "\n")
must.Fprintf(w, " - run: ./script/ghpublish.bash %s\n", strings.Join(artifactsNames, " "))
must.Fprintf(w, " env:\n")
must.Fprintf(w, " GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n")
must.Fprintf(w, "\n")
}

func newStepSetupLinuxDockerGoCache(w io.Writer, name string) {
mustFprintf(w, " - uses: actions/cache@v3\n")
mustFprintf(w, " with:\n")
mustFprintf(w, " path: GOCACHE\n")
mustFprintf(w, " key: linux-build-cache-%s\n", name)
mustFprintf(w, "\n")
must.Fprintf(w, " - uses: actions/cache@v3\n")
must.Fprintf(w, " with:\n")
must.Fprintf(w, " path: GOCACHE\n")
must.Fprintf(w, " key: linux-build-cache-%s\n", name)
must.Fprintf(w, "\n")

}

func newSetupInstallQemuUserStatic(w io.Writer) {
mustFprintf(w, " - run: sudo apt-get update -q\n")
mustFprintf(w, " - run: sudo apt-get install -y qemu-user-static\n")
mustFprintf(w, "\n")
must.Fprintf(w, " - run: sudo apt-get update -q\n")
must.Fprintf(w, " - run: sudo apt-get install -y qemu-user-static\n")
must.Fprintf(w, "\n")
}

func newStepInstallTor(w io.Writer) {
mustFprintf(w, " - run: sudo apt-get update -q\n")
mustFprintf(w, " - run: sudo apt-get install -y tor\n")
mustFprintf(w, "\n")
must.Fprintf(w, " - run: sudo apt-get update -q\n")
must.Fprintf(w, " - run: sudo apt-get install -y tor\n")
must.Fprintf(w, "\n")
}

func newStepRunOONIProbeIntegrationTests(w io.Writer, os, arch, ext string) {
executable := fmt.Sprintf("ooniprobe-%s-%s%s", os, arch, ext)
if os != "windows" {
mustFprintf(w, " - run: chmod +x %s\n", executable)
must.Fprintf(w, " - run: chmod +x %s\n", executable)
}
mustFprintf(w, " - run: ./E2E/ooniprobe.bash ./%s\n", executable)
mustFprintf(w, " shell: bash\n")
mustFprintf(w, "\n")
must.Fprintf(w, " - run: ./E2E/ooniprobe.bash ./%s\n", executable)
must.Fprintf(w, " shell: bash\n")
must.Fprintf(w, "\n")
}

func newStepRunMiniooniIntegrationTests(w io.Writer, os, arch, ext string) {
executable := fmt.Sprintf("miniooni-%s-%s%s", os, arch, ext)
if os != "windows" {
mustFprintf(w, " - run: chmod +x %s\n", executable)
must.Fprintf(w, " - run: chmod +x %s\n", executable)
}
mustFprintf(w, " - run: ./E2E/miniooni.bash ./%s\n", executable)
mustFprintf(w, " shell: bash\n")
mustFprintf(w, "\n")
must.Fprintf(w, " - run: ./E2E/miniooni.bash ./%s\n", executable)
must.Fprintf(w, " shell: bash\n")
must.Fprintf(w, "\n")
}

func newStepInstallMingwW64(w io.Writer) {
mustFprintf(w, " - run: sudo apt-get update -q\n")
mustFprintf(w, " - run: sudo apt-get install -y mingw-w64\n")
mustFprintf(w, "\n")
}

func mustFprintf(w io.Writer, format string, v ...any) {
_, err := fmt.Fprintf(w, format, v...)
runtimex.PanicOnError(err, "fmt.Fprintf failed")
}

func mustClose(c io.Closer) {
err := c.Close()
runtimex.PanicOnError(err, "c.Close failed")
must.Fprintf(w, " - run: sudo apt-get update -q\n")
must.Fprintf(w, " - run: sudo apt-get install -y mingw-w64\n")
must.Fprintf(w, "\n")
}

func generateWorkflowFile(name string, jobs []Job) {
filename := filepath.Join(".github", "workflows", name+".yml")
fp, err := os.Create(filename)
runtimex.PanicOnError(err, "os.Create failed")
defer mustClose(fp)
mustFprintf(fp, "# File generated by `go run ./GHGEN`; DO NOT EDIT.\n")
mustFprintf(fp, "\n")
mustFprintf(fp, "name: %s\n", name)
mustFprintf(fp, "on:\n")
mustFprintf(fp, " push:\n")
mustFprintf(fp, " branches:\n")
mustFprintf(fp, " - \"release/**\"\n")
mustFprintf(fp, " - \"fullbuild\"\n")
mustFprintf(fp, " - \"%sbuild\"\n", name)
mustFprintf(fp, " tags:\n")
mustFprintf(fp, " - \"v*\"\n")
mustFprintf(fp, " schedule:\n")
mustFprintf(fp, " - cron: \"17 1 * * *\"\n")
mustFprintf(fp, "\n")
mustFprintf(fp, "jobs:\n")
fp := must.CreateFile(filename)
defer fp.MustClose()
must.Fprintf(fp, "# File generated by `go run ./GHGEN`; DO NOT EDIT.\n")
must.Fprintf(fp, "\n")
must.Fprintf(fp, "name: %s\n", name)
must.Fprintf(fp, "on:\n")
must.Fprintf(fp, " push:\n")
must.Fprintf(fp, " branches:\n")
must.Fprintf(fp, " - \"release/**\"\n")
must.Fprintf(fp, " - \"fullbuild\"\n")
must.Fprintf(fp, " - \"%sbuild\"\n", name)
must.Fprintf(fp, " tags:\n")
must.Fprintf(fp, " - \"v*\"\n")
must.Fprintf(fp, " schedule:\n")
must.Fprintf(fp, " - cron: \"17 1 * * *\"\n")
must.Fprintf(fp, "\n")
must.Fprintf(fp, "jobs:\n")
for _, job := range jobs {
job.Action(fp, &job)
}
mustFprintf(fp, "# End of autogenerated file\n")
must.Fprintf(fp, "# End of autogenerated file\n")
}
3 changes: 2 additions & 1 deletion internal/cmd/miniooni/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/must"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/version"
)
Expand All @@ -24,7 +25,7 @@ func newSessionOrPanic(ctx context.Context, currentOptions *Options,
miniooniDir string, logger model.Logger) *engine.Session {
var proxyURL *url.URL
if currentOptions.Proxy != "" {
proxyURL = mustParseURL(currentOptions.Proxy)
proxyURL = must.ParseURL(currentOptions.Proxy)
}

kvstore2dir := filepath.Join(miniooniDir, "kvstore2")
Expand Down
8 changes: 0 additions & 8 deletions internal/cmd/miniooni/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"errors"
"net/url"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -54,13 +53,6 @@ func mustMakeMapStringAny(input []string) (output map[string]any) {
return
}

// mustParseURL parses the given URL or panics
func mustParseURL(URL string) *url.URL {
rv, err := url.Parse(URL)
runtimex.PanicOnError(err, "cannot parse URL")
return rv
}

// gethomedir returns the home directory. If optionsHome is set, then we
// return that string as the home directory. Otherwise, we use typical
// platform-specific environment variables to determine the home. In case
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/oohelper/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/must"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/version"
)

Expand Down Expand Up @@ -110,8 +110,7 @@ func (oo OOClient) Do(ctx context.Context, config OOConfig) (*CtrlResponse, erro
},
TCPConnect: endpoints,
}
data, err := json.Marshal(creq)
runtimex.PanicOnError(err, "oohelper: cannot marshal control request")
data := must.MarshalJSON(creq)
log.Debugf("out: %s", string(data))
req, err := http.NewRequestWithContext(ctx, "POST", config.ServerURL, bytes.NewReader(data))
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/oohelper/oohelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package main

import (
"context"
"encoding/json"
"flag"
"fmt"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelper/internal"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/must"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
Expand Down Expand Up @@ -41,8 +41,7 @@ func main() {
flag.Parse()
log.SetLevel(logmap[*debug])
cresp := wcth()
data, err := json.MarshalIndent(cresp, "", " ")
runtimex.PanicOnError(err, "json.MarshalIndent failed")
data := must.MarshalAndIndentJSON(cresp, "", " ")
fmt.Printf("%s\n", string(data))
}

Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/oohelperd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"time"

"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/must"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/version"
)

Expand Down Expand Up @@ -87,8 +87,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
metricRequestsCount.WithLabelValues("200", "ok").Inc()
// We assume that the following call cannot fail because it's a
// clearly-serializable data structure.
data, err = json.Marshal(cresp)
runtimex.PanicOnError(err, "json.Marshal failed")
data = must.MarshalJSON(cresp)
w.Header().Add("Content-Type", "application/json")
w.Write(data)
}
6 changes: 2 additions & 4 deletions internal/cmd/oohelperd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ package main
import (
"context"
"flag"
"net"
"net/http"
"sync"
"sync/atomic"
"time"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/must"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

Expand Down Expand Up @@ -103,8 +102,7 @@ func main() {
},
})
srv := &http.Server{Addr: *endpoint, Handler: mux}
listener, err := net.Listen("tcp", *endpoint)
runtimex.PanicOnError(err, "net.Listen failed")
listener := must.Listen("tcp", *endpoint)
srvAddr <- listener.Addr().String()
srvWg.Add(1)
go srv.Serve(listener)
Expand Down
Loading