Skip to content

Commit f8259eb

Browse files
authored
Merge pull request #76 from andmpel/Remove-Sequential-Processing-and-minor-clenaup
Revert to sequential execution + flags
2 parents ece730a + 3252605 commit f8259eb

File tree

5 files changed

+87
-89
lines changed

5 files changed

+87
-89
lines changed

macup/macup.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package macup
22

33
import (
4-
"io"
5-
"sync"
4+
"fmt"
65

76
"github.com/AlecAivazis/survey/v2"
87
)
@@ -11,7 +10,7 @@ import (
1110
type Update struct {
1211
Name string
1312
Description string
14-
Run func(writer io.Writer)
13+
Run func()
1514
}
1615

1716
// Config represents the user's selections.
@@ -30,21 +29,18 @@ var Updates = []Update{
3029
{"macos", "Update macOS system", UpdateMacOS},
3130
}
3231

33-
// Run runs the selected update functions.
34-
func Run(writer io.Writer, selectedUpdates []string) {
35-
var wg sync.WaitGroup
32+
func Run(selectedUpdates []string) {
3633
for _, updateName := range selectedUpdates {
3734
for _, u := range Updates {
3835
if u.Name == updateName {
39-
wg.Add(1)
4036
go func(u Update) {
41-
defer wg.Done()
42-
u.Run(writer)
37+
fmt.Printf("Starting: %s\n", u.Description)
38+
u.Run()
39+
fmt.Printf("Finished: %s\n", u.Description)
4340
}(u)
4441
}
4542
}
4643
}
47-
wg.Wait()
4844
}
4945

5046
// SelectUpdates prompts the user to select which updates to run.

macup/test/macup_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ package macup
44

55
import (
66
"bytes"
7-
"io"
87
"testing"
98
)
109

1110
// Helper to capture output
12-
func captureOutput(f func(io.Writer)) string {
11+
func captureOutput(f func(io.)) string {
1312
buf := &bytes.Buffer{}
1413
f(buf)
1514
return buf.String()
1615
}
1716

1817
func TestPrintlnGreen(t *testing.T) {
19-
out := captureOutput(func(w io.Writer) { printlnGreen(w, "Hello") })
18+
out := captureOutput(func(w io.) { printlnGreen(w, "Hello") })
2019
if !bytes.Contains([]byte(out), []byte("Hello")) {
2120
t.Errorf("Expected output to contain 'Hello', got: %s", out)
2221
}

macup/update.go

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,84 @@
11
package macup
22

33
import (
4-
"io"
54
"os/exec"
65
"strings"
76
)
87

98
// UpdateBrew updates Homebrew formulas and perform diagnostics.
10-
func UpdateBrew(writer io.Writer) {
11-
printlnGreen(writer, "Updating Brew Formulas")
12-
if checkCommand(writer, "brew") {
13-
runCommand(writer, "brew", "update")
14-
runCommand(writer, "brew", "upgrade")
15-
runCommand(writer, "brew", "cleanup", "-s")
16-
printlnGreen(writer, "Brew Diagnostics")
17-
runCommand(writer, "brew", "doctor")
18-
runCommand(writer, "brew", "missing")
9+
func UpdateBrew() {
10+
printlnGreen("Updating Brew Formulas")
11+
if checkCommand("brew") {
12+
runCommand("brew", "update")
13+
runCommand("brew", "upgrade")
14+
runCommand("brew", "cleanup", "-s")
15+
printlnGreen("Brew Diagnostics")
16+
runCommand("brew", "doctor")
17+
runCommand("brew", "missing")
1918
}
2019
}
2120

2221
// UpdateVSCodeExt updates VSCode extensions.
23-
func UpdateVSCodeExt(writer io.Writer) {
24-
printlnGreen(writer, "Updating VSCode Extensions")
25-
if checkCommand(writer, "code") {
26-
runCommand(writer, "code", "--update-extensions")
22+
func UpdateVSCodeExt() {
23+
printlnGreen("Updating VSCode Extensions")
24+
if checkCommand("code") {
25+
runCommand("code", "--update-extensions")
2726
}
2827
}
2928

3029
// UpdateGem updates Ruby gems and clean up.
31-
func UpdateGem(writer io.Writer) {
32-
printlnGreen(writer, "Updating Gems")
30+
func UpdateGem() {
31+
printlnGreen("Updating Gems")
3332
gemPath, err := exec.LookPath("gem")
3433
if err != nil || gemPath == k_gemCmdPath {
35-
printlnYellow(writer, "gem is not installed.")
34+
printlnYellow("gem is not installed.")
3635
return
3736
}
38-
runCommand(writer, "gem", "update", "--user-install")
39-
runCommand(writer, "gem", "cleanup", "--user-install")
37+
runCommand("gem", "update", "--user-install")
38+
runCommand("gem", "cleanup", "--user-install")
4039
}
4140

4241
// UpdateNodePkg updates global Node.js, npm, and Yarn packages.
43-
func UpdateNodePkg(writer io.Writer) {
44-
printlnGreen(writer, "Updating Node Packages")
45-
if checkCommand(writer, "node") {
46-
printlnGreen(writer, "Updating Npm Packages")
47-
if checkCommand(writer, "npm") {
48-
runCommand(writer, "npm", "update", "-g")
42+
func UpdateNodePkg() {
43+
printlnGreen("Updating Node Packages")
44+
if checkCommand("node") {
45+
printlnGreen("Updating Npm Packages")
46+
if checkCommand("npm") {
47+
runCommand("npm", "update", "-g")
4948
}
5049

51-
printlnGreen(writer, "Updating Yarn Packages")
52-
if checkCommand(writer, "yarn") {
53-
runCommand(writer, "yarn", "global", "upgrade", "--latest")
50+
printlnGreen("Updating Yarn Packages")
51+
if checkCommand("yarn") {
52+
runCommand("yarn", "global", "upgrade", "--latest")
5453
}
5554
}
5655
}
5756

5857
// UpdateCargo updates Rust Cargo crates by reinstalling each listed crate.
59-
func UpdateCargo(writer io.Writer) {
60-
printlnGreen(writer, "Updating Rust Cargo Crates")
61-
if checkCommand(writer, "cargo") {
58+
func UpdateCargo() {
59+
printlnGreen("Updating Rust Cargo Crates")
60+
if checkCommand("cargo") {
6261
out, _ := exec.Command("cargo", "install", "--list").Output()
6362
lines := strings.SplitSeq(string(out), "\n")
6463
for line := range lines {
6564
if fields := strings.Fields(line); len(fields) > 0 {
6665
name := fields[0]
67-
runCommand(writer, "cargo", "install", name)
66+
runCommand("cargo", "install", name)
6867
}
6968
}
7069
}
7170
}
7271

7372
// UpdateAppStore updates Mac App Store applications.
74-
func UpdateAppStore(writer io.Writer) {
75-
printlnGreen(writer, "Updating App Store Applications")
76-
if checkCommand(writer, "mas") {
77-
runCommand(writer, "mas", "upgrade")
73+
func UpdateAppStore() {
74+
printlnGreen("Updating App Store Applications")
75+
if checkCommand("mas") {
76+
runCommand("mas", "upgrade")
7877
}
7978
}
8079

8180
// UpdateMacOS updates macOS system software.
82-
func UpdateMacOS(writer io.Writer) {
83-
printlnGreen(writer, "Updating MacOS")
84-
runCommand(writer, "softwareupdate", "-i", "-a")
81+
func UpdateMacOS() {
82+
printlnGreen("Updating MacOS")
83+
runCommand("softwareupdate", "-i", "-a")
8584
}

macup/utils.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package macup
33
import (
44
"encoding/json"
55
"fmt"
6-
"io"
76
"net/http"
87
"os"
98
"os/exec"
@@ -25,32 +24,32 @@ const (
2524
)
2625

2726
// printlnGreen prints a message in green color with a newline.
28-
func printlnGreen(writer io.Writer, msg string) {
29-
fmt.Fprintf(writer, "\n%s%s%s\n", k_green, msg, k_clear)
27+
func printlnGreen(msg string) {
28+
fmt.Fprintf(os.Stdout, "\n%s%s%s\n", k_green, msg, k_clear)
3029
}
3130

3231
// printlnRed prints a message in red color (no newline).
33-
func printlnRed(writer io.Writer, msg string) {
34-
fmt.Fprintf(writer, "%s%s%s", k_red, msg, k_clear)
32+
func printlnRed(msg string) {
33+
fmt.Fprintf(os.Stdout, "%s%s%s", k_red, msg, k_clear)
3534
}
3635

3736
// printlnYellow prints a message in yellow color (no newline).
38-
func printlnYellow(writer io.Writer, msg string) {
39-
fmt.Fprintf(writer, "%s%s%s", k_yellow, msg, k_clear)
37+
func printlnYellow(msg string) {
38+
fmt.Fprintf(os.Stdout, "%s%s%s", k_yellow, msg, k_clear)
4039
}
4140

4241
// checkCommand checks if a command exists in `PATH`, print warning if not.
43-
func checkCommand(writer io.Writer, cmd string) bool {
42+
func checkCommand(cmd string) bool {
4443
_, err := exec.LookPath(cmd)
4544
if err != nil {
46-
printlnYellow(writer, cmd+" is not installed.")
45+
printlnYellow(cmd + " is not installed.")
4746
return false
4847
}
4948
return true
5049
}
5150

52-
// runCommand runs a shell command and directs its output to writer.
53-
func runCommand(writer io.Writer, name string, args ...string) {
51+
// runCommand runs a shell command and directs its output to .
52+
func runCommand(name string, args ...string) {
5453
// Allow only specific commands
5554
allowedCommands := map[string]bool{
5655
"brew": true,
@@ -64,22 +63,22 @@ func runCommand(writer io.Writer, name string, args ...string) {
6463
}
6564

6665
if !allowedCommands[name] {
67-
printlnRed(writer, "Command not allowed: "+name)
66+
printlnRed("Command not allowed: " + name)
6867
return
6968
}
7069
// Optionally validate arguments (e.g., no special characters)
7170
for _, arg := range args {
7271
if strings.ContainsAny(arg, "&|;$><") {
73-
printlnRed(writer, "Invalid Argument: "+arg)
72+
printlnRed("Invalid Argument: " + arg)
7473
return
7574
}
7675
}
7776

7877
cmd := exec.Command(name, args...)
79-
cmd.Stdout = writer
80-
cmd.Stderr = writer
78+
cmd.Stdout = os.Stdout
79+
cmd.Stderr = os.Stderr
8180
if err := cmd.Run(); err != nil {
82-
printlnRed(writer, "Error running command: "+err.Error())
81+
8382
}
8483
}
8584

@@ -91,7 +90,7 @@ func CheckInternet() bool {
9190

9291
resp, err := client.Get(k_testURL)
9392
if err != nil {
94-
fmt.Fprintf(os.Stderr, "\n%s%s%s\n", k_red, "⚠️ No Internet Connection!!!", k_clear)
93+
printlnRed("⚠️ No Internet Connection!!!")
9594
return false
9695
}
9796
defer resp.Body.Close()

main.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package main
22

33
import (
44
"bytes"
5+
"flag"
56
"fmt"
67
"macup/macup"
78
"os"
8-
"sync"
99
)
1010

1111
// Entry point of the update script
1212
func main() {
13+
// Define and parse the --yes flag
14+
yes := flag.Bool("yes", false, "Use previous selections without prompting")
15+
flag.Parse()
16+
1317
// Check for internet connectivity before proceeding
1418
if !macup.CheckInternet() {
1519
os.Exit(1)
@@ -23,40 +27,41 @@ func main() {
2327
}
2428

2529
// Prompt the user to select updates
26-
selectedUpdates, err := macup.SelectUpdates(config)
27-
if err != nil {
28-
fmt.Fprintf(os.Stderr, "Error selecting updates: %v\n", err)
29-
os.Exit(1)
30-
}
30+
var selectedUpdates []string
31+
if *yes && len(config.SelectedUpdates) > 0 {
32+
selectedUpdates = config.SelectedUpdates
33+
} else {
34+
if *yes {
35+
println("No previous updates selected. Prompting for selection.")
36+
}
37+
var err error
38+
selectedUpdates, err = macup.SelectUpdates(config)
39+
if err != nil {
40+
fmt.Fprintf(os.Stderr, "Error selecting updates: %v\n", err)
41+
os.Exit(1)
42+
}
3143

32-
// Save the user's selections
33-
config.SelectedUpdates = selectedUpdates
34-
if err := config.SaveConfig(); err != nil {
35-
fmt.Fprintf(os.Stderr, "Error saving config: %v\n", err)
36-
os.Exit(1)
44+
// Save the user's selections
45+
config.SelectedUpdates = selectedUpdates
46+
if err := config.SaveConfig(); err != nil {
47+
fmt.Fprintf(os.Stderr, "Error saving config: %v\n", err)
48+
os.Exit(1)
49+
}
3750
}
3851

3952
// Run the selected updates
40-
var wg sync.WaitGroup
4153
buffers := make(map[string]*bytes.Buffer)
4254
for _, updateName := range selectedUpdates {
4355
buffers[updateName] = &bytes.Buffer{}
4456
}
45-
4657
for _, updateName := range selectedUpdates {
4758
for _, u := range macup.Updates {
4859
if u.Name == updateName {
49-
wg.Add(1)
50-
go func(u macup.Update) {
51-
defer wg.Done()
52-
u.Run(buffers[u.Name])
53-
}(u)
60+
u.Run()
5461
}
5562
}
5663
}
5764

58-
wg.Wait()
59-
6065
// Print the output of each update function
6166
for _, updateName := range selectedUpdates {
6267
if buffer, ok := buffers[updateName]; ok {

0 commit comments

Comments
 (0)