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

Commit 9f40f9d

Browse files
authored
chore: clean up command exec usage (#293)
1 parent 124bf1e commit 9f40f9d

File tree

6 files changed

+27
-457
lines changed

6 files changed

+27
-457
lines changed

internal/cache/cache.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ import (
66
"fmt"
77
"io/fs"
88
"os"
9-
"os/exec"
109
"path/filepath"
1110

1211
"github.com/ublue-os/fleek/fin"
12+
"github.com/ublue-os/fleek/internal/cmdutil"
1313
"github.com/ublue-os/fleek/internal/fleek"
1414
"github.com/ublue-os/fleek/internal/xdg"
1515
)
1616

17-
const nixbin = "nix"
18-
1917
type PackageCache struct {
2018
location string
2119
Packages PackageList
@@ -102,22 +100,16 @@ func (pc *PackageCache) Update() error {
102100
pc.Packages = plist
103101
return nil
104102
}
105-
func (pc *PackageCache) runNix(cmd string, cmdLine []string) ([]byte, error) {
106-
command := exec.Command(cmd, cmdLine...)
107-
command.Stdin = os.Stdin
108-
command.Env = os.Environ()
109-
110-
return command.Output()
111-
112-
}
113103

114104
func (pc *PackageCache) packageIndex() ([]byte, error) {
105+
args := []string{"search", "nixpkgs", "--json"}
106+
cmd, buf := cmdutil.CommandTTYWithBuffer("nix", args...)
107+
cmd.Env = os.Environ()
115108
// nix search nixpkgs --json
116-
indexCmdLine := []string{"search", "nixpkgs", "--json"}
117-
out, err := pc.runNix(nixbin, indexCmdLine)
109+
err := cmd.Run()
118110
if err != nil {
119-
return out, fmt.Errorf("nix search: %w", err)
111+
return buf.Bytes(), fmt.Errorf("nix search: %w", err)
120112
}
121113

122-
return out, nil
114+
return buf.Bytes(), nil
123115
}

internal/flake/flake.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"io"
77
"io/fs"
88
"os"
9-
"os/exec"
109
"path/filepath"
1110
"text/template"
1211

1312
"github.com/riywo/loginshell"
1413
app "github.com/ublue-os/fleek"
1514
"github.com/ublue-os/fleek/fin"
15+
"github.com/ublue-os/fleek/internal/cmdutil"
1616
"github.com/ublue-os/fleek/internal/fleek"
1717
)
1818

@@ -573,10 +573,9 @@ func (f *Flake) Apply() error {
573573
return nil
574574
}
575575
func (f *Flake) runNix(cmd string, cmdLine []string) error {
576-
command := exec.Command(cmd, cmdLine...)
577-
command.Stdin = os.Stdin
578-
command.Stderr = os.Stderr
579-
command.Stdout = os.Stdout
576+
577+
command := cmdutil.CommandTTY(cmd, cmdLine...)
578+
580579
command.Dir = f.Config.UserFlakeDir()
581580
fin.Debug.Println("running nix command in ", command.Dir)
582581
command.Env = os.Environ()
@@ -588,12 +587,12 @@ func (f *Flake) runNix(cmd string, cmdLine []string) error {
588587

589588
}
590589
func ForceProfile() error {
591-
command := exec.Command("nix", "profile", "list")
592-
command.Stdin = os.Stdin
593-
command.Stderr = io.Discard
594-
command.Stdout = io.Discard
595-
command.Env = os.Environ()
596-
return command.Run()
590+
cmd := cmdutil.CommandTTY("nix", "profile", "list")
591+
cmd.Stdin = os.Stdin
592+
cmd.Stderr = io.Discard
593+
cmd.Stdout = io.Discard
594+
cmd.Env = os.Environ()
595+
return cmd.Run()
597596

598597
}
599598

internal/flake/git.go

+8-19
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"fmt"
66
"io/fs"
77
"os"
8-
"os/exec"
98

109
"github.com/go-git/go-git/v5"
1110
"github.com/ublue-os/fleek/fin"
11+
"github.com/ublue-os/fleek/internal/cmdutil"
1212
"github.com/ublue-os/fleek/internal/debug"
1313
fgit "github.com/ublue-os/fleek/internal/git"
1414
)
@@ -31,27 +31,19 @@ func (f *Flake) Clone(repo string) error {
3131
if err != nil {
3232
return err
3333
}
34-
command := exec.Command(gitbin, cloneCmdline...)
35-
command.Stdin = os.Stdin
36-
command.Dir = home
37-
command.Stdin = os.Stdin
38-
command.Stdout = os.Stdout
39-
command.Stderr = os.Stderr
40-
command.Env = os.Environ()
41-
err = command.Run()
34+
cmd := cmdutil.CommandTTY(gitbin, cloneCmdline...)
35+
cmd.Dir = home
36+
cmd.Env = os.Environ()
37+
err = cmd.Run()
4238
if err != nil {
4339
return fmt.Errorf("git clone: %w", err)
4440
}
4541
return nil
4642
}
4743

4844
func (f *Flake) runGit(cmd string, cmdLine []string) error {
49-
command := exec.Command(cmd, cmdLine...)
50-
command.Stdin = os.Stdin
45+
command := cmdutil.CommandTTY(cmd, cmdLine...)
5146
command.Dir = f.Config.UserFlakeDir()
52-
command.Stdin = os.Stdin
53-
command.Stdout = os.Stdout
54-
command.Stderr = os.Stderr
5547
command.Env = os.Environ()
5648
return command.Run()
5749
}
@@ -229,7 +221,7 @@ func (f *Flake) push() error {
229221

230222
func (f *Flake) gitStatus() (*fgit.Status, error) {
231223
// git status --ignored --porcelain=v2
232-
cmd := exec.Command(gitbin, "status", "--ignored", "--porcelain=v2")
224+
cmd := cmdutil.CommandTTY(gitbin, "status", "--ignored", "--porcelain=v2")
233225
cmd.Dir = f.Config.UserFlakeDir()
234226
cmd.Env = os.Environ()
235227
out, err := cmd.Output()
@@ -270,11 +262,8 @@ func CloneRepository(repo string) (string, error) {
270262
return "", err
271263
}
272264
cloneCmdline := []string{"clone", repo, dirname}
265+
command := cmdutil.CommandTTY(gitbin, cloneCmdline...)
273266

274-
command := exec.Command(gitbin, cloneCmdline...)
275-
command.Stdin = os.Stdin
276-
command.Stdout = os.Stdout
277-
command.Stderr = os.Stderr
278267
command.Env = os.Environ()
279268
err = command.Run()
280269
if err != nil {

internal/fleek/config.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/hashicorp/go-version"
1414
"github.com/ublue-os/fleek/fin"
15+
"github.com/ublue-os/fleek/internal/cmdutil"
1516
"github.com/ublue-os/fleek/internal/ux"
1617
"github.com/ublue-os/fleek/internal/xdg"
1718
"gopkg.in/yaml.v3"
@@ -126,9 +127,7 @@ func NewSystem() (*System, error) {
126127

127128
// CollectGarbage runs nix-collect-garbage
128129
func CollectGarbage() error {
129-
130-
command := exec.Command("nix-collect-garbage", "-d")
131-
command.Stdin = os.Stdin
130+
command := cmdutil.CommandTTY("nix-collect-garbage", "-d")
132131
command.Stderr = io.Discard
133132
command.Stdout = io.Discard
134133
command.Env = os.Environ()

0 commit comments

Comments
 (0)