This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
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,104 @@ | ||
//go:build windows | ||
|
||
package providers | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"golang.org/x/sync/errgroup" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type Choco struct { | ||
} | ||
|
||
func (c Choco) GetApplications() ([]Application, error) { | ||
var result []Application | ||
if _, err := os.Stat("C:\\ProgramData\\chocolatey\\bin\\choco.exe"); errors.Is(err, os.ErrNotExist) { | ||
return result, nil | ||
} | ||
|
||
command := exec.Command("choco", "list", "-localonly", "--limitoutput", "--detailed") | ||
output, err := command.StdoutPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func(output io.ReadCloser) { | ||
_ = output.Close() | ||
}(output) | ||
|
||
err = command.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
scanner := bufio.NewScanner(output) | ||
scanner.Split(bufio.ScanLines) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
splitted := strings.Split(line, "|") | ||
|
||
app := Application{Name: splitted[0], Provider: c} | ||
app.ExtraInfo = map[string]string{ | ||
"Version": splitted[1], | ||
} | ||
result = append(result, app) | ||
} | ||
|
||
g := new(errgroup.Group) | ||
for i := range result { | ||
application := &result[i] | ||
g.Go(func() error { | ||
return fillChocoInfo(application) | ||
}) | ||
} | ||
|
||
_ = g.Wait() | ||
|
||
return result, nil | ||
} | ||
|
||
func fillChocoInfo(application *Application) error { | ||
command := exec.Command("choco", "info", application.Name) | ||
output, err := command.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
defer func(output io.ReadCloser) { | ||
_ = output.Close() | ||
}(output) | ||
|
||
err = command.Start() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
scanner := bufio.NewScanner(output) | ||
scanner.Split(bufio.ScanLines) | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
store := func(chocoField, idntField string) { | ||
if strings.HasPrefix(line, chocoField) { | ||
application.ExtraInfo[idntField] = line[len(chocoField):] | ||
} | ||
} | ||
|
||
store(" Software Site: ", "URL") | ||
store(" Summary: ", "Description") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c Choco) RemoveApplication(application *Application) error { | ||
_, err := exec.Command("choco", "uninstall", application.Name).Output() | ||
return err | ||
} | ||
|
||
func (c Choco) GetName() string { | ||
return "Chocolatey" | ||
} |
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,25 @@ | ||
package providers | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestChoco_GetApplications(t *testing.T) { | ||
choco := Choco{} | ||
applications, err := choco.GetApplications() | ||
|
||
require.NoError(t, err) | ||
require.NotEmpty(t, applications) | ||
|
||
} | ||
|
||
func TestFillChocoInfo(t *testing.T) { | ||
app := Application{Name: "7zip"} | ||
app.ExtraInfo = map[string]string{} | ||
err := fillChocoInfo(&app) | ||
|
||
require.NoError(t, err) | ||
require.Contains(t, app.ExtraInfo, "URL") | ||
require.Contains(t, app.ExtraInfo, "Description") | ||
} |
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