Skip to content

Commit

Permalink
Merge pull request #104 from giusdp/main
Browse files Browse the repository at this point in the history
refactor: check for https as well in -plugin tool
  • Loading branch information
francescotimperi authored Sep 30, 2023
2 parents 60b34b5 + cd9ef8d commit 5bbfd7d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
23 changes: 12 additions & 11 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
Expand All @@ -39,34 +40,33 @@ func pluginTool() error {
return err
}

args := flag.Args()
if len(args) != 1 {
if flag.NArg() != 1 {
flag.Usage()
return nil
return errors.New("invalid number of arguments. Expected 1")
}

return downloadPluginTasksFromRepo(args[0])
return downloadPluginTasksFromRepo(flag.Arg(0))
}

func printPluginUsage() {
fmt.Println(`Usage: nuv -plugin <repo>
fmt.Println(`Usage: nuv -plugin <repo-url>
Install/update plugins from a repository.
Install/update plugins from a remote repository.
The name of the repository must start with 'olaris-'.`)
}

func downloadPluginTasksFromRepo(repo string) error {
isNameValid, repoName := checkGitRepo(repo)
if !isNameValid {
return fmt.Errorf("plugin repository names must start with 'olaris-'")
return fmt.Errorf("plugin repository must be a https url and plugin must start with 'olaris-'")
}

pluginDir, err := homedir.Expand("~/.nuv/" + repoName)
if err != nil {
return err
}

if _, err := os.Stat(pluginDir); !os.IsNotExist(err) {
if isDir(pluginDir) {
fmt.Println("Updating plugin", repoName)

r, err := git.PlainOpen(pluginDir)
Expand Down Expand Up @@ -120,10 +120,11 @@ func checkGitRepo(url string) (bool, string) {
parts := strings.Split(url, "/")
repoName := parts[len(parts)-1]

// Check if the repository name matches the pattern "olaris-*"
match, _ := regexp.MatchString("^olaris-.*$", repoName)
// Check if the repository name matches the pattern "https://...olaris-*"
matchProtocol, _ := regexp.MatchString(`^https://.*$`, url)
matchName, _ := regexp.MatchString(`^olaris-.*$`, repoName)

if match {
if matchName && matchProtocol {
return true, repoName
}
return false, ""
Expand Down
7 changes: 1 addition & 6 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ func TestCheckGitRepo(t *testing.T) {
expectedRepo: true,
expectedName: "olaris-test",
},
{
url: "[email protected]:giusdp/olaris-test.git",
expectedRepo: true,
expectedName: "olaris-test",
},
{
url: "https://github.com/giusdp/some-repo",
expectedRepo: false,
Expand All @@ -217,8 +212,8 @@ func TestCheckGitRepo(t *testing.T) {

for _, test := range tests {
isOlarisRepo, repoName := checkGitRepo(test.url)
require.Equal(t, test.expectedRepo, isOlarisRepo)
require.Equal(t, test.expectedName, repoName)
require.Equal(t, test.expectedRepo, isOlarisRepo)
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/plugin.bats
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ setup() {

@test "nuv -plugin with wrong name" {
run nuv -plugin https://github.com/giusdp/olari
assert_line "error: plugin repository names must start with 'olaris-'"
assert_line "error: plugin repository must be a https url and plugin must start with 'olaris-'"
assert_failure

run nuv -plugin olaris-test
assert_line "error: plugin repository must be a https url and plugin must start with 'olaris-'"
assert_failure
}

Expand Down

0 comments on commit 5bbfd7d

Please sign in to comment.