Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
chore: clean up command exec usage (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
bketelsen authored Sep 23, 2023
1 parent 124bf1e commit 9f40f9d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 457 deletions.
22 changes: 7 additions & 15 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"

"github.com/ublue-os/fleek/fin"
"github.com/ublue-os/fleek/internal/cmdutil"
"github.com/ublue-os/fleek/internal/fleek"
"github.com/ublue-os/fleek/internal/xdg"
)

const nixbin = "nix"

type PackageCache struct {
location string
Packages PackageList
Expand Down Expand Up @@ -102,22 +100,16 @@ func (pc *PackageCache) Update() error {
pc.Packages = plist
return nil
}
func (pc *PackageCache) runNix(cmd string, cmdLine []string) ([]byte, error) {
command := exec.Command(cmd, cmdLine...)
command.Stdin = os.Stdin
command.Env = os.Environ()

return command.Output()

}

func (pc *PackageCache) packageIndex() ([]byte, error) {
args := []string{"search", "nixpkgs", "--json"}
cmd, buf := cmdutil.CommandTTYWithBuffer("nix", args...)
cmd.Env = os.Environ()
// nix search nixpkgs --json
indexCmdLine := []string{"search", "nixpkgs", "--json"}
out, err := pc.runNix(nixbin, indexCmdLine)
err := cmd.Run()
if err != nil {
return out, fmt.Errorf("nix search: %w", err)
return buf.Bytes(), fmt.Errorf("nix search: %w", err)
}

return out, nil
return buf.Bytes(), nil
}
21 changes: 10 additions & 11 deletions internal/flake/flake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"text/template"

"github.com/riywo/loginshell"
app "github.com/ublue-os/fleek"
"github.com/ublue-os/fleek/fin"
"github.com/ublue-os/fleek/internal/cmdutil"
"github.com/ublue-os/fleek/internal/fleek"
)

Expand Down Expand Up @@ -573,10 +573,9 @@ func (f *Flake) Apply() error {
return nil
}
func (f *Flake) runNix(cmd string, cmdLine []string) error {
command := exec.Command(cmd, cmdLine...)
command.Stdin = os.Stdin
command.Stderr = os.Stderr
command.Stdout = os.Stdout

command := cmdutil.CommandTTY(cmd, cmdLine...)

command.Dir = f.Config.UserFlakeDir()
fin.Debug.Println("running nix command in ", command.Dir)
command.Env = os.Environ()
Expand All @@ -588,12 +587,12 @@ func (f *Flake) runNix(cmd string, cmdLine []string) error {

}
func ForceProfile() error {
command := exec.Command("nix", "profile", "list")
command.Stdin = os.Stdin
command.Stderr = io.Discard
command.Stdout = io.Discard
command.Env = os.Environ()
return command.Run()
cmd := cmdutil.CommandTTY("nix", "profile", "list")
cmd.Stdin = os.Stdin
cmd.Stderr = io.Discard
cmd.Stdout = io.Discard
cmd.Env = os.Environ()
return cmd.Run()

}

Expand Down
27 changes: 8 additions & 19 deletions internal/flake/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"io/fs"
"os"
"os/exec"

"github.com/go-git/go-git/v5"
"github.com/ublue-os/fleek/fin"
"github.com/ublue-os/fleek/internal/cmdutil"
"github.com/ublue-os/fleek/internal/debug"
fgit "github.com/ublue-os/fleek/internal/git"
)
Expand All @@ -31,27 +31,19 @@ func (f *Flake) Clone(repo string) error {
if err != nil {
return err
}
command := exec.Command(gitbin, cloneCmdline...)
command.Stdin = os.Stdin
command.Dir = home
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Env = os.Environ()
err = command.Run()
cmd := cmdutil.CommandTTY(gitbin, cloneCmdline...)
cmd.Dir = home
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return fmt.Errorf("git clone: %w", err)
}
return nil
}

func (f *Flake) runGit(cmd string, cmdLine []string) error {
command := exec.Command(cmd, cmdLine...)
command.Stdin = os.Stdin
command := cmdutil.CommandTTY(cmd, cmdLine...)
command.Dir = f.Config.UserFlakeDir()
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Env = os.Environ()
return command.Run()
}
Expand Down Expand Up @@ -229,7 +221,7 @@ func (f *Flake) push() error {

func (f *Flake) gitStatus() (*fgit.Status, error) {
// git status --ignored --porcelain=v2
cmd := exec.Command(gitbin, "status", "--ignored", "--porcelain=v2")
cmd := cmdutil.CommandTTY(gitbin, "status", "--ignored", "--porcelain=v2")
cmd.Dir = f.Config.UserFlakeDir()
cmd.Env = os.Environ()
out, err := cmd.Output()
Expand Down Expand Up @@ -270,11 +262,8 @@ func CloneRepository(repo string) (string, error) {
return "", err
}
cloneCmdline := []string{"clone", repo, dirname}
command := cmdutil.CommandTTY(gitbin, cloneCmdline...)

command := exec.Command(gitbin, cloneCmdline...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Env = os.Environ()
err = command.Run()
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/fleek/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/hashicorp/go-version"
"github.com/ublue-os/fleek/fin"
"github.com/ublue-os/fleek/internal/cmdutil"
"github.com/ublue-os/fleek/internal/ux"
"github.com/ublue-os/fleek/internal/xdg"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -126,9 +127,7 @@ func NewSystem() (*System, error) {

// CollectGarbage runs nix-collect-garbage
func CollectGarbage() error {

command := exec.Command("nix-collect-garbage", "-d")
command.Stdin = os.Stdin
command := cmdutil.CommandTTY("nix-collect-garbage", "-d")
command.Stderr = io.Discard
command.Stdout = io.Discard
command.Env = os.Environ()
Expand Down
Loading

0 comments on commit 9f40f9d

Please sign in to comment.