Skip to content

Commit

Permalink
refactor: remove moby/gorilla dependency (#4439)
Browse files Browse the repository at this point in the history
* refactor: remove moby dependency

* updates

* linting

* updates

* updates

---------

Co-authored-by: Danilo Pantani <[email protected]>
  • Loading branch information
julienrbrt and Pantani authored Dec 6, 2024
1 parent f4f7966 commit e97d643
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 53 deletions.
10 changes: 3 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ require (
github.com/golangci/golangci-lint v1.60.3
github.com/google/go-github/v48 v48.2.0
github.com/google/go-querystring v1.1.0
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-hclog v1.6.3
github.com/hashicorp/go-plugin v1.6.2
github.com/iancoleman/strcase v0.3.0
Expand All @@ -71,7 +70,6 @@ require (
github.com/lib/pq v1.10.9
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/mapstructure v1.5.0
github.com/moby/moby v26.1.0+incompatible
github.com/muesli/reflow v0.3.0
github.com/nqd/flat v0.2.0
github.com/otiai10/copy v1.14.0
Expand Down Expand Up @@ -186,8 +184,6 @@ require (
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft-db v1.0.1 // indirect
github.com/containerd/containerd v1.7.11 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/cosiner/argv v0.1.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
Expand Down Expand Up @@ -289,13 +285,15 @@ require (
github.com/gordonklaus/ineffassign v0.1.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/handlers v1.5.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
github.com/gostaticanalysis/forcetypeassert v0.1.0 // indirect
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-metrics v0.5.3 // indirect
Expand Down Expand Up @@ -356,9 +354,6 @@ require (
github.com/mikesmitty/edkey v0.0.0-20170222072505-3356ea4e686a // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/moricho/tparallel v0.3.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
Expand Down Expand Up @@ -467,6 +462,7 @@ require (
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMb
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
github.com/Abirdcfly/dupword v0.0.14 h1:3U4ulkc8EUo+CaT105/GJ1BQwtgyj6+VaBVbAX11Ba8=
github.com/Abirdcfly/dupword v0.0.14/go.mod h1:VKDAbxdY8YbKUByLGg8EETzYSuC4crm9WwI6Y3S0cLI=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
github.com/Antonboom/errname v0.1.13 h1:JHICqsewj/fNckzrfVSe+T33svwQxmjC+1ntDsHOVvM=
Expand Down Expand Up @@ -100,8 +98,6 @@ github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lpr
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8=
github.com/Microsoft/hcsshim v0.11.4/go.mod h1:smjE4dvqPX9Zldna+t5FG3rnoHhaB7QYxPRqGcpAD9w=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -301,8 +297,6 @@ github.com/cometbft/cometbft-db v1.0.1/go.mod h1:EBrFs1GDRiTqrWXYi4v90Awf/gcdD5E
github.com/cometbft/cometbft/api v1.0.0-rc2 h1:ThzvNaJq37htjzfSsohhDoEebW94p0lEjA39CMDJ4xs=
github.com/cometbft/cometbft/api v1.0.0-rc2/go.mod h1:EkQiqVSu/p2ebrZEnB2z6Re7r8XNe//M7ylR0qEwWm0=
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
github.com/containerd/containerd v1.7.11 h1:lfGKw3eU35sjV0aG2eYZTiwFEY1pCzxdzicHP3SZILw=
github.com/containerd/containerd v1.7.11/go.mod h1:5UluHxHTX2rdvYuZ5OJTC5m/KJNs0Zs9wVoJm9zf5ZE=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
github.com/containerd/stargz-snapshotter/estargz v0.15.1 h1:eXJjw9RbkLFgioVaTG+G/ZW/0kEe2oEKCdS/ZxIyoCU=
Expand Down Expand Up @@ -959,14 +953,6 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/moby v26.1.0+incompatible h1:mjepCwMH0KpCgPvrXjqqyCeTCHgzO7p9TwZ2nQMI2qU=
github.com/moby/moby v26.1.0+incompatible/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc=
github.com/moby/sys/sequential v0.5.0/go.mod h1:tH2cOOs5V9MlPiXcQzRC+eEyab644PWKGRYaaV5ZZlo=
github.com/moby/sys/user v0.1.0 h1:WmZ93f5Ux6het5iituh9x2zAG7NFY9Aqi49jjE1PaQg=
github.com/moby/sys/user v0.1.0/go.mod h1:fKJhFOnsCN6xZ5gSfbM6zaHGgDJMrqt9/reuj4T7MmU=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
109 changes: 109 additions & 0 deletions ignite/pkg/archive/tar_gz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package archive

import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"

"github.com/ignite/cli/v29/ignite/pkg/errors"
)

// CreateArchive creates a tar.gz archive from a list of files.
func CreateArchive(dir string, buf io.Writer) error {
// Create new Writers for gzip and tar
// These writers are chained. Writing to the tar writer will
// write to the gzip writer which in turn will write to
// the "buf" writer
gw := gzip.NewWriter(buf)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()

return filepath.WalkDir(dir, func(path string, _ os.DirEntry, _ error) error {
return addToArchive(tw, path)
})
}

func addToArchive(tw *tar.Writer, filename string) error {
// Open the file which will be written into the archive
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()

// Get FileInfo about our file providing file size, mode, etc.
info, err := file.Stat()
if err != nil {
return err
}

// Create a tar Header from the FileInfo data
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}

// Use full path as name (FileInfoHeader only takes the basename)
// If we don't do this the directory strucuture would
// not be preserved
// https://golang.org/src/archive/tar/common.go?#L626
header.Name = filename

// Write file header to the tar archive
err = tw.WriteHeader(header)
if err != nil {
return err
}

_, err = io.Copy(tw, file)
if err != nil {
return err
}

return nil
}

// ExtractArchive extracts a tar.gz archive to the specified directory.
func ExtractArchive(outDir string, gzipStream io.Reader) error {
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return err
}

tarReader := tar.NewReader(uncompressedStream)

for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}

targetPath := filepath.Join(outDir, header.Name) //nolint:gosec // We trust the tar file

switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(targetPath, 0o755); err != nil {
return err
}
case tar.TypeReg:
outFile, err := os.Create(targetPath)
if err != nil {
return err
}
if _, err := io.Copy(outFile, tarReader); err != nil { //nolint:gosec // We trust the tar file
return err
}
outFile.Close()

default:
return errors.Errorf("unknown type: %s in %s", string(header.Typeflag), header.Name)
}
}

return nil
}
50 changes: 30 additions & 20 deletions ignite/pkg/cosmosfaucet/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cosmosfaucet
import (
"net/http"

"github.com/gorilla/mux"
"github.com/rs/cors"

"github.com/ignite/cli/v29/ignite/pkg/openapiconsole"
Expand All @@ -12,23 +11,34 @@ import (
// ServeHTTP implements http.Handler to expose the functionality of Faucet.Transfer() via HTTP.
// request/response payloads are compatible with the previous implementation at allinbits/cosmos-faucet.
func (f Faucet) ServeHTTP(w http.ResponseWriter, r *http.Request) {
router := mux.NewRouter()

router.
Handle("/", cors.Default().Handler(http.HandlerFunc(f.faucetHandler))).
Methods(http.MethodPost, http.MethodOptions)

router.
Handle("/info", cors.Default().Handler(http.HandlerFunc(f.faucetInfoHandler))).
Methods(http.MethodGet, http.MethodOptions)

router.
HandleFunc("/", openapiconsole.Handler("Faucet", "openapi.yml")).
Methods(http.MethodGet)

router.
HandleFunc("/openapi.yml", f.openAPISpecHandler).
Methods(http.MethodGet)

router.ServeHTTP(w, r)
mux := http.NewServeMux()

mux.Handle("/", cors.Default().Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodPost || r.Method == http.MethodOptions:
f.faucetHandler(w, r)
case r.Method == http.MethodGet:
openapiconsole.Handler("Faucet", "openapi.yml")(w, r)
default:
http.NotFound(w, r)
}
})))

mux.Handle("/info", cors.Default().Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet || r.Method == http.MethodOptions {
f.faucetInfoHandler(w, r)
} else {
http.NotFound(w, r)
}
})))

mux.HandleFunc("/openapi.yml", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
f.openAPISpecHandler(w, r)
} else {
http.NotFound(w, r)
}
})

mux.ServeHTTP(w, r)
}
12 changes: 2 additions & 10 deletions ignite/services/chain/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package chain
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/moby/moby/pkg/archive"

"github.com/ignite/cli/v29/ignite/pkg/archive"
"github.com/ignite/cli/v29/ignite/pkg/cache"
"github.com/ignite/cli/v29/ignite/pkg/checksum"
"github.com/ignite/cli/v29/ignite/pkg/cmdrunner"
Expand Down Expand Up @@ -181,11 +179,6 @@ func (c *Chain) BuildRelease(
return "", err
}

tarr, err := archive.Tar(out, archive.Gzip)
if err != nil {
return "", err
}

tarName := fmt.Sprintf("%s_%s_%s.tar.gz", prefix, goos, goarch)
tarPath := filepath.Join(releasePath, tarName)

Expand All @@ -195,10 +188,9 @@ func (c *Chain) BuildRelease(
}
defer tarf.Close()

if _, err := io.Copy(tarf, tarr); err != nil {
if err := archive.CreateArchive(out, tarf); err != nil {
return "", err
}
tarf.Close()
}

checksumPath := filepath.Join(releasePath, releaseChecksumKey)
Expand Down
5 changes: 3 additions & 2 deletions ignite/services/chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"path/filepath"
"testing"

"github.com/moby/moby/pkg/archive"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ignite/cli/v29/ignite/pkg/archive"
)

func TestSourceVersion(t *testing.T) {
Expand Down Expand Up @@ -38,7 +39,7 @@ func tempSource(t *testing.T, tarPath string) (path string) {

dir := t.TempDir()

require.NoError(t, archive.Untar(f, dir, &archive.TarOptions{NoLchown: true}))
require.NoError(t, archive.ExtractArchive(dir, f))

dirs, err := os.ReadDir(dir)
require.NoError(t, err)
Expand Down

0 comments on commit e97d643

Please sign in to comment.