-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
418 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
vendor/ | ||
resc-darwin-amd64 | ||
resc-linux-amd64 |
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,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/JulzDiverse/resc/scriptmanager" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "list remote script of a resc repository", | ||
Run: list, | ||
} | ||
|
||
func list(cmd *cobra.Command, args []string) { | ||
var user, repo string | ||
if len(args) == 0 { | ||
user, repo = configFromFile() | ||
} else { | ||
sl := strings.Split(args[0], "/") | ||
user = sl[0] | ||
repo = sl[1] | ||
} | ||
|
||
scriptManager := scriptmanager.New( | ||
"https://api.github.com", | ||
user, | ||
repo, | ||
) | ||
|
||
list, err := scriptManager.List() | ||
exitWithError(err) | ||
|
||
listScripts(fmt.Sprintf("%s/%s", user, repo), list) | ||
} | ||
|
||
func listScripts(repo string, scripts []string) { | ||
fmt.Println("ReSc Repository:", repo) | ||
fmt.Println("\nAvailable Scripts:") | ||
for _, script := range scripts { | ||
fmt.Println(" -", script) | ||
} | ||
} |
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
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,56 @@ | ||
package scriptmanager | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type Content struct { | ||
Name string `json:"name"` | ||
ContentType string `json:"type"` | ||
} | ||
|
||
func (s *ScriptManager) List() ([]string, error) { | ||
list, err := getter(fmt.Sprintf("%s/repos/%s/%s/contents", s.url, s.user, s.repo)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var contents []Content | ||
err = json.Unmarshal(list, &contents) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
validScripts := []string{} | ||
for _, content := range contents { | ||
if content.ContentType == "dir" { | ||
if ok, err := exists(fmt.Sprintf("%s/repos/%s/%s/contents/%s/.resc", s.url, s.user, s.repo, content.Name)); ok { | ||
validScripts = append(validScripts, content.Name) | ||
} else { | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
} | ||
return validScripts, nil | ||
} | ||
|
||
func exists(url string) (bool, error) { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return false, errors.Wrap(err, "failed to perform http GET") | ||
} | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
return true, nil | ||
} else if resp.StatusCode == http.StatusNotFound { | ||
return false, nil | ||
} else { | ||
return false, errors.New(fmt.Sprintf("an error ocurred while checking repository: %s", resp.Status)) | ||
} | ||
} |
Oops, something went wrong.