Skip to content

Commit

Permalink
implement list option
Browse files Browse the repository at this point in the history
  • Loading branch information
herrjulz committed Jun 29, 2018
1 parent d2e55fc commit d02fed4
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
vendor/
resc-darwin-amd64
resc-linux-amd64
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
### OS X

```
$ wget -O /usr/local/bin/resc https://github.com/JulzDiverse/resc/releases/download/v0.1.0/resc-darwin-amd64 && chmod +x /usr/local/bin/resc
$ wget -O /usr/local/bin/resc https://github.com/JulzDiverse/resc/releases/download/v0.2.0/resc-darwin-amd64 && chmod +x /usr/local/bin/resc
```

OR
Expand All @@ -25,7 +25,7 @@ $ brew install resc
### Linux

```
$ wget -O /usr/bin/resc https://github.com/JulzDiverse/resc/releases/download/v0.1.0/resc-linux-amd64 && chmod +x /usr/bin/resc
$ wget -O /usr/bin/resc https://github.com/JulzDiverse/resc/releases/download/v0.2.0/resc-linux-amd64 && chmod +x /usr/bin/resc
```

## Hello, World!
Expand All @@ -40,16 +40,18 @@ This runs the `hello-world` script located in the `JulzDiverse/remote-scripts` r

## `resc` scripts

`resc` scripts requires one or more top level directories inside a GitHub repository that contain a `run.sh` script and a `README.md` file. In case of the `hello-world` script the directory looks like this:
`resc` scripts requires one or more top level directories inside a GitHub repository that contain a `run.sh` script, a `.resc` file and a `README.md` file. In case of the `hello-world` script the directory looks like this:

```
.remote-scripts
└── hello-world
   ├── run.sh
   ├── .resc
├── run.sh
   └── README.md
```

- The `directory name` (here `hello-world`) indicates the script
- The `.resc` is an empty file that indicates that the directoy is a `resc` script
- The `run.sh` is the bash script that is run by `resc`
- The `README.md` is a Markdown file that provides information for a script (eg description, usage). The `README.md` is processed by the `resc` CLI and should only contain the following markdown syntax:
- H1 (#)
Expand Down Expand Up @@ -78,14 +80,30 @@ You can also provide parameters to a script using `--args|-a` option. Try it:
$ resc run hello-world -r JulzDiverse/remote-scripts -a your-name
```

### 🧐 Set base `resc` script repository (`set`)
### 🧐 Set default `resc` script repository (`set`)

You can set a base `resc` script repository, s.t you are not required to specify the repository of a script everytime you execute the `resc run`.
You can set a default `resc` script repository, s.t you are not required to specify the repository of a script everytime you execute the `resc run`.

```
$ resc set <github-user|github-org>/<github-repo>
```

### ✅ List all available scripts in a resc repository (`list`)

If you want to know what `resc` scripts a repository provides, you can list all of them using `list`.

If you have set a default repository you can run just:

```
$ resc list
```

If you want to list scripts of a specific repository, run:

```
$ resc list <github-user>/<github-repo>
```

### 📖 Get some script info (`man`)

If you want to know what a script does before you run it, you can check the provided README by calling `man`:
Expand Down
45 changes: 45 additions & 0 deletions cmd/resc/cmd/list.go
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)
}
}
5 changes: 5 additions & 0 deletions cmd/resc/cmd/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bufio"
"errors"
"fmt"
"strings"

Expand All @@ -17,6 +18,10 @@ var manCmd = &cobra.Command{
}

func man(cmd *cobra.Command, args []string) {
if len(args) == 0 {
exitWithError(errors.New("No script specified"))
}

userRepo, err := cmd.Flags().GetString("repo")
exitWithError(err)

Expand Down
5 changes: 5 additions & 0 deletions cmd/resc/cmd/print.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"strings"

Expand All @@ -15,6 +16,10 @@ var printCmd = &cobra.Command{
}

func print(cmd *cobra.Command, args []string) {
if len(args) == 0 {
exitWithError(errors.New("No script specified"))
}

userRepo, err := cmd.Flags().GetString("repo")
exitWithError(err)

Expand Down
4 changes: 3 additions & 1 deletion cmd/resc/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ var rootCmd = &cobra.Command{
Use: "resc",
Short: "execute remote scripts",
Long: `This tool is executing scripts located on github`,
Version: "0.1.0",
Version: "0.2.0",
}

func init() {
initRun()
initPrint()
initMan()

rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(printCmd)
rootCmd.AddCommand(manCmd)
rootCmd.AddCommand(listCmd)
}

func Execute() {
Expand Down
5 changes: 5 additions & 0 deletions cmd/resc/cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"strings"

"github.com/JulzDiverse/resc/runner"
Expand All @@ -20,6 +21,10 @@ var runCmd = &cobra.Command{
}

func run(cmd *cobra.Command, args []string) {
if len(args) == 0 {
exitWithError(errors.New("No script specified"))
}

userRepo, err := cmd.Flags().GetString("repo")
exitWithError(err)

Expand Down
56 changes: 56 additions & 0 deletions scriptmanager/list.go
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))
}
}
Loading

0 comments on commit d02fed4

Please sign in to comment.