-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup structure, implement exec (#48)
- Loading branch information
Showing
23 changed files
with
712 additions
and
306 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
*.dll | ||
*.so | ||
*.dylib | ||
bin | ||
bin/ | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.