Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Chocolatey support
Browse files Browse the repository at this point in the history
  • Loading branch information
r-darwish committed Nov 8, 2021
1 parent 5ee07ab commit 2b12947
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/ktr0731/go-fuzzyfinder v0.5.0
github.com/pterm/pterm v0.12.33
github.com/stretchr/testify v1.7.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
104 changes: 104 additions & 0 deletions providers/choco.go
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"
}
25 changes: 25 additions & 0 deletions providers/choco_test.go
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")
}
2 changes: 1 addition & 1 deletion providers/providers_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
package providers

func GetOsSpecificProviders() []Provider {
return []Provider{&AppWiz{}}
return []Provider{&AppWiz{}, &Choco{}}
}

0 comments on commit 2b12947

Please sign in to comment.