Skip to content

Commit

Permalink
Cleanup structure, implement exec (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
adracus authored Apr 14, 2023
1 parent e2f9795 commit 2520e6a
Show file tree
Hide file tree
Showing 23 changed files with 712 additions and 306 deletions.
52 changes: 0 additions & 52 deletions .github/release-drafter.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.19
go-version-file: 'go.mod'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.49.0
version: v1.52.2
31 changes: 0 additions & 31 deletions .github/workflows/release.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.19
go-version-file: 'go.mod'
- run: make test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*.dll
*.so
*.dylib
bin
bin/

# Test binary, build with `go test -c`
*.test
Expand Down
Empty file removed bin/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -14,12 +15,19 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
40 changes: 40 additions & 0 deletions internal/cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 OnMetal authors
//
// 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 version

import (
"io"

"github.com/onmetal/vgopath/internal/version"
"github.com/spf13/cobra"
)

func Command(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Prints version information.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return Run(out)
},
}

return cmd
}

func Run(out io.Writer) error {
version.FPrint(out)
return nil
}
109 changes: 109 additions & 0 deletions internal/cmd/vgopath/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2023 OnMetal authors
//
// 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 exec

import (
"fmt"
"os"
"os/exec"
"regexp"

"github.com/onmetal/vgopath/internal/link"
"github.com/spf13/cobra"
)

func Command() *cobra.Command {
var (
opts link.Options
dstDir string
shell bool
)

cmd := &cobra.Command{
Use: "exec -- command [args...]",
Short: "Run an executable in a virtual GOPATH.",
Args: func(cmd *cobra.Command, args []string) error {
if !shell {
return cobra.MinimumNArgs(1)(cmd, args)
}
return cobra.ExactArgs(1)(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
executable, executableArgs := executableAndArgs(args, shell)
return Run(dstDir, executable, opts, executableArgs)
},
}

opts.AddFlags(cmd.Flags())
cmd.Flags().StringVarP(&dstDir, "dst-dir", "o", "", "Destination directory. If empty, a temporary directory will be created.")
cmd.Flags().BoolVarP(&shell, "shell", "s", false, "Whether to run the command in a shell.")

return cmd
}

func executableAndArgs(args []string, doShell bool) (string, []string) {
if !doShell {
return args[0], args[1:]
}

shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/sh"
}

return shell, []string{"-c", args[0]}
}

func Run(dstDir, executable string, opts link.Options, args []string) error {
if dstDir == "" {
var err error
dstDir, err = os.MkdirTemp("", "vgopath")
if err != nil {
return fmt.Errorf("error creating temp directory: %w", err)
}
defer func() { _ = os.RemoveAll(dstDir) }()
}

if err := link.Link(dstDir, opts); err != nil {
return err
}

cmd := exec.Command(executable, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dstDir

cmd.Env = mkEnv(dstDir)
return cmd.Run()
}

var filterEnvRegexp = regexp.MustCompile(`^(GOPATH|GO111MODULE)=`)

func mkEnv(gopath string) []string {
env := os.Environ()
res := make([]string, 0, len(env)+2)

for _, kv := range env {
if !filterEnvRegexp.MatchString(kv) {
res = append(res, kv)
}
}

return append(res,
fmt.Sprintf("GOPATH=%s", gopath),
"GO111MODULE=off",
)
}
63 changes: 63 additions & 0 deletions internal/cmd/vgopath/vgopath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2023 OnMetal authors
//
// 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 vgopath

import (
"os"

"github.com/onmetal/vgopath/internal/cmd/version"
"github.com/onmetal/vgopath/internal/cmd/vgopath/exec"
"github.com/onmetal/vgopath/internal/link"
"github.com/spf13/cobra"
)

func Command() *cobra.Command {
var (
opts link.Options
dstDir string
)

cmd := &cobra.Command{
Use: "vgopath",
Short: "Create and operate on virtual GOPATHs",
Long: `Create a 'virtual' GOPATH at the specified directory.
vgopath will setup a GOPATH folder structure, ensuring that any tool used
to the traditional setup will function as normal.
The target module will be mirrored to where its go.mod path (the line
after 'module') points at.
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return Run(dstDir, opts)
},
}

opts.AddFlags(cmd.Flags())
cmd.Flags().StringVarP(&dstDir, "dst-dir", "o", "", "Destination directory.")
_ = cmd.MarkFlagRequired("dst-dir")

cmd.AddCommand(
exec.Command(),
version.Command(os.Stdout),
)

return cmd
}

func Run(dstDir string, opts link.Options) error {
return link.Link(dstDir, opts)
}
1 change: 0 additions & 1 deletion internal/dstGopath/pkg

This file was deleted.

Loading

0 comments on commit 2520e6a

Please sign in to comment.