Skip to content

Commit 737e703

Browse files
committed
Initial commit
0 parents  commit 737e703

File tree

9 files changed

+205
-0
lines changed

9 files changed

+205
-0
lines changed

.circleci/config.yml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
version: 2.1
2+
3+
orbs:
4+
go: circleci/[email protected]
5+
gor: hubci/[email protected]
6+
7+
workflows:
8+
main:
9+
jobs:
10+
- build:
11+
matrix:
12+
parameters:
13+
go: [ "1.18.5", "1.19.0" ]
14+
release:
15+
jobs:
16+
- build:
17+
matrix:
18+
parameters:
19+
go: [ "1.18.5", "1.19.0" ]
20+
filters:
21+
branches:
22+
ignore: /.*/
23+
tags:
24+
# Simplified SemVer regex
25+
only: /^v\d+\.\d+\.\d+$/
26+
- gor/release:
27+
version: "1.10.3"
28+
go-version: "1.18.5"
29+
filters:
30+
branches:
31+
ignore: /.*/
32+
tags:
33+
# Simplified SemVer regex
34+
only: /^v\d+\.\d+\.\d+$/
35+
context: main
36+
37+
jobs:
38+
build:
39+
parameters:
40+
go:
41+
type: string
42+
description: "The Go cimg tag to use."
43+
docker:
44+
- image: cimg/go:<< parameters.go >>
45+
steps:
46+
- checkout
47+
- go/load-cache
48+
- go/mod-download
49+
- run:
50+
name: "Try Building"
51+
command: |
52+
go build ./...
53+
- go/save-cache

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main
2+
dist

.goreleaser.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
project_name: winpkg
2+
3+
builds:
4+
- skip: true

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright ©2022 Ricardo N Feliciano
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# winpkg [![CI Status](https://circleci.com/gh/gopherlibs/winpkg.svg?style=shield)](https://app.circleci.com/pipelines/github/gopherlibs/winpkg) [![Go Report Card](https://goreportcard.com/badge/github.com/gopherlibs/winpkg)](https://goreportcard.com/report/github.com/gopherlibs/winpkg) [![GoDoc](https://godoc.org/github.com/gopherlibs/appindicator?status.svg)](https://godoc.org/github.com/gopherlibs/appindicator) [![Software License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/gopherlibs/winpkg/trunk/LICENSE)
2+
3+
`winpkg` is a Go (Golang) module to pull the latest version of a Chocolatey package.
4+
Chocolatey here is a Windows package manager.
5+
6+
7+
## Requirements
8+
9+
This Go module is intended to support the ~~two~~ most recent ~~minor releases~~ release of Go.
10+
Currently this is Go v1.18.x and v1.19.x.
11+
12+
13+
## Installation
14+
15+
`winpkg` is a Go module intended to be used as a library.
16+
So the best way to "install" it is to import into your own code and then run `go mod tidy` to get the source code downloaded.
17+
18+
```go
19+
import(
20+
"github.com/gopherlibs/winpkg/winpkg"
21+
)
22+
```
23+
24+
25+
## Usage
26+
27+
28+
```go
29+
package main
30+
31+
import (
32+
"fmt"
33+
34+
"github.com/gopherlibs/winpkg/winpkg"
35+
)
36+
37+
func main() {
38+
39+
// Let's get the latest version of the 'git' package
40+
if ver, err := winpkg.GetVersion( "git" ); err != nil {
41+
fmt.Println(err)
42+
} else {
43+
fmt.Println(ver)
44+
}
45+
}
46+
```
47+
48+
49+
## Development
50+
51+
This module is written and tested with Go v1.18+ in mind.
52+
`go fmt` is your friend.
53+
Please feel free to open Issues and PRs are you see fit.
54+
Any PR that requires a good amount of work or is a significant change, it would be best to open an Issue to discuss the change first.
55+
56+
57+
## License & Credits
58+
59+
This module was written by Ricardo N Feliciano (FelicianoTech).
60+
This repository is licensed under the MIT license.
61+
This repo's license can be found [here](./LICENSE).

cmd/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/gopherlibs/winpkg/winpkg"
8+
)
9+
10+
func main() {
11+
if ver, err := winpkg.GetVersion(os.Args[1]); err != nil {
12+
fmt.Println(err)
13+
} else {
14+
fmt.Println(ver)
15+
}
16+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/gopherlibs/winpkg
2+
3+
go 1.19

go.sum

Whitespace-only changes.

winpkg/winpkg.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package winpkg
2+
3+
import (
4+
"encoding/xml"
5+
"errors"
6+
"io/ioutil"
7+
"net/http"
8+
)
9+
10+
// The root XML element returned from the Chocolatey Search API.
11+
type Feed struct {
12+
XMLName xml.Name `xml:"feed"`
13+
Entries []Entry `xml:"entry"`
14+
}
15+
16+
// The Chocolatey Search API can return zero or more `entry` items.
17+
// Each entry represents a package.
18+
type Entry struct {
19+
XMLName xml.Name `xml:"entry"`
20+
Title string `xml:"title"`
21+
Version string `xml:"properties>Version"`
22+
}
23+
24+
func GetVersion(pkg string) (string, error) {
25+
26+
resp, err := http.Get("https://community.chocolatey.org/api/v2/Search()?$filter=IsLatestVersion&$skip=0&$top=1&searchTerm=%27" + pkg + "%27&targetFramework=%27%27&includePrerelease=false")
27+
if err != nil {
28+
return "", errors.New("Failed to reach Chocolately API.")
29+
}
30+
defer resp.Body.Close()
31+
32+
data, err := ioutil.ReadAll(resp.Body)
33+
if err != nil {
34+
return "", errors.New("Failed to read in data from API response.")
35+
}
36+
37+
var searchResult Feed
38+
39+
err = xml.Unmarshal(data, &searchResult)
40+
if err != nil {
41+
return "", errors.New("Failed to unmarshal API response.")
42+
}
43+
44+
return searchResult.Entries[0].Version, nil
45+
}

0 commit comments

Comments
 (0)