Skip to content

Commit

Permalink
Merge pull request #15 from kencx/installLockFile
Browse files Browse the repository at this point in the history
add ability to install from stewfile.lock.json
  • Loading branch information
marwanhawari authored Jul 24, 2022
2 parents 7fe8e85 + 484af1d commit 34945f2
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func Install(cliInputs []string) {
stew.CatchAndExit(err)

for _, cliInput := range cliInputs {
if strings.Contains(cliInput, "Stewfile.lock.json") {
cliInputs, err = stew.ReadStewLockFileContents(cliInput)
stew.CatchAndExit(err)
break
}

if strings.Contains(cliInput, "Stewfile") {
cliInputs, err = stew.ReadStewfileContents(cliInput)
stew.CatchAndExit(err)
Expand Down
21 changes: 21 additions & 0 deletions lib/stewfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ func ReadStewfileContents(stewfilePath string) ([]string, error) {
return packages, nil
}

func ReadStewLockFileContents(lockFilePath string) ([]string, error) {

lockFile, err := readLockFileJSON(lockFilePath)
if err != nil {
return []string{}, err
}

var packages []string
for _, pkg := range lockFile.Packages {
switch pkg.Source {
case "other":
packages = append(packages, pkg.URL)
case "github":
path := fmt.Sprintf("%s/%s@%s::%s", pkg.Owner, pkg.Repo, pkg.Tag, pkg.Asset)
packages = append(packages, path)
}
}

return packages, nil
}

// NewLockFile creates a new instance of the LockFile struct
func NewLockFile(stewLockFilePath, userOS, userArch string) (LockFile, error) {
var lockFile LockFile
Expand Down
72 changes: 72 additions & 0 deletions lib/stewfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ var testStewfileSlice []string = []string{
"marwanhawari/[email protected]",
}

var testStewLockFileContents string = `{
"os": "darwin",
"arch": "arm64",
"packages": [
{
"source": "github",
"owner": "cli",
"repo": "cli",
"tag": "v2.4.0",
"asset": "gh_2.4.0_macOS_amd64.tar.gz",
"binary": "gh",
"url": "https://github.com/cli/cli/releases/download/v2.4.0/gh_2.4.0_macOS_amd64.tar.gz"
},
{
"source": "github",
"owner": "junegunn",
"repo": "fzf",
"tag": "0.29.0",
"asset": "fzf-0.29.0-darwin_arm64.zip",
"binary": "fzf",
"url": "https://github.com/junegunn/fzf/releases/download/0.29.0/fzf-0.29.0-darwin_arm64.zip"
},
{
"source": "other",
"owner": "",
"repo": "",
"tag": "",
"asset": "hyperfine-v1.12.0-x86_64-apple-darwin.tar.gz",
"binary": "hyperfine",
"url": "https://github.com/sharkdp/hyperfine/releases/download/v1.12.0/hyperfine-v1.12.0-x86_64-apple-darwin.tar.gz"
}
]
}
`

var testStewLockFileSlice []string = []string{
"cli/[email protected]::gh_2.4.0_macOS_amd64.tar.gz",
"junegunn/[email protected]::fzf-0.29.0-darwin_arm64.zip",
"https://github.com/sharkdp/hyperfine/releases/download/v1.12.0/hyperfine-v1.12.0-x86_64-apple-darwin.tar.gz",
}

func Test_readLockFileJSON(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -215,6 +256,37 @@ func TestReadStewfileContents(t *testing.T) {
}
}

func TestReadStewLockFileContents(t *testing.T) {
tests := []struct {
name string
want []string
wantErr bool
}{
{
name: "test1",
want: testStewLockFileSlice,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
testStewLockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")
ioutil.WriteFile(testStewLockFilePath, []byte(testStewLockFileContents), 0644)

got, err := ReadStewLockFileContents(testStewLockFilePath)
if (err != nil) != tt.wantErr {
t.Errorf("ReadStewLockFileContents() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadStewLockFileContents() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewLockFile(t *testing.T) {
type args struct {
userOS string
Expand Down

0 comments on commit 34945f2

Please sign in to comment.