Skip to content

Commit

Permalink
add branch option; add run --script option
Browse files Browse the repository at this point in the history
  • Loading branch information
herrjulz committed Jul 4, 2018
1 parent a8212cb commit 715a44f
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 236 deletions.
43 changes: 34 additions & 9 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.2.0/resc-darwin-amd64 && chmod +x /usr/local/bin/resc
$ wget -O /usr/local/bin/resc https://github.com/JulzDiverse/resc/releases/download/v0.3.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.2.0/resc-linux-amd64 && chmod +x /usr/bin/resc
$ wget -O /usr/bin/resc https://github.com/JulzDiverse/resc/releases/download/v0.3.0/resc-linux-amd64 && chmod +x /usr/bin/resc
```

## Hello, World!
Expand Down Expand Up @@ -68,18 +68,24 @@ Running a `resc` script is nothing more than:
$ resc run <script-name> --repo <github-user|github-org>/<github-repo>
```

or if you have set a repository, it's even simpler:
or if you have set a default repository, it's even simpler:

```
$ resc run <script-name>
```

You can also provide parameters to a script using `--args|-a` option. Try it:
You can provide parameters to a script using `--args|-a` option. Try it:

```
$ resc run hello-world -r JulzDiverse/remote-scripts -a your-name
```

You can also run a specific script located anywhere in a repository by providing the path to the script:

```bash
$ resc run -s <path/to/script.sh> -r JulzDiverse/remote-scripts
```

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

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`.
Expand All @@ -94,38 +100,57 @@ If you want to know which `resc` scripts a repository provides, you can list all

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

```
```bash
$ resc list
```

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

```
```bash
$ 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`:

```
```bash
$ resc man <script-name>
```

### 🖨 Print a script (`print`)

If you want to see what a script exactly does or you want to save it to your local machine, you can use the `print` command:

```
```bash
$ resc print <script-name>
```

to save a script, pipe it to a file:

```
```bash
$ resc print <script-name> > script.sh
```

### 🌿 Specifing a Branch

Each `resc` command has the `--branch|-b` option, where you can specify a specific branch of a repository you want to use to execute a script. For example:

```
$ resc run hello-world -r JulzDiverse/remote-scripts -b develop
```

The default branch used is always `master`. You can, however, set a default branch if required:

```bash
# if you have set your defaults already:
$ resc set -b develop

# if you haven't set your defaults:
$ resc set <owner>/<repo> -b develop
```


## 💻 Development

```
Expand Down
33 changes: 15 additions & 18 deletions cmd/resc/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@ package cmd

import (
"fmt"
"strings"

"github.com/JulzDiverse/resc/scriptmanager"
"github.com/JulzDiverse/resc/models/github"
"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "list remote script of a resc repository",
Use: "list [owner/repository]",
Short: "list remote scripts of a 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]
}
userRepo, err := cmd.Flags().GetString("repo")
exitWithError(err)

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

scriptManager := scriptmanager.New(
"https://api.github.com",
user,
repo,
)
scriptManager, user, repo, branch := initScriptManager(github.ApiUrl, userRepo, branch)

list, err := scriptManager.List()
exitWithError(err)

listScripts(fmt.Sprintf("%s/%s", user, repo), list)
listScripts(fmt.Sprintf("%s/%s on branch %s", user, repo, branch), list)
}

func initList() {
listCmd.Flags().StringP("branch", "b", "", "branch of the repository containing the scripts. Default: master")
listCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: <user|org>/<repo>")
}

func listScripts(repo string, scripts []string) {
Expand Down
23 changes: 7 additions & 16 deletions cmd/resc/cmd/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"fmt"
"strings"

"github.com/JulzDiverse/resc/models/github"
"github.com/JulzDiverse/resc/processor"
"github.com/JulzDiverse/resc/scriptmanager"
"github.com/spf13/cobra"
)

var manCmd = &cobra.Command{
Use: "man",
Use: "man <script-name>",
Short: "print the manual of a remote script",
Run: man,
}
Expand All @@ -25,20 +25,10 @@ func man(cmd *cobra.Command, args []string) {
userRepo, err := cmd.Flags().GetString("repo")
exitWithError(err)

var user, repo string
if userRepo == "" {
user, repo = configFromFile()
} else {
sl := strings.Split(userRepo, "/")
user = sl[0]
repo = sl[1]
}
branch, err := cmd.Flags().GetString("branch")
exitWithError(err)

scriptManager := scriptmanager.New(
"https://raw.githubusercontent.com",
user,
repo,
)
scriptManager, _, _, _ := initScriptManager(github.RawContentUrl, userRepo, branch)

readme, err := scriptManager.GetReadmeForScript(args[0])
exitWithError(err)
Expand All @@ -52,5 +42,6 @@ func man(cmd *cobra.Command, args []string) {
}

func initMan() {
manCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: <user|org>/<repo>")
manCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: <owner>/<repo>")
manCmd.Flags().StringP("branch", "b", "", "branch of the repository containing the script. Default: master")
}
36 changes: 17 additions & 19 deletions cmd/resc/cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package cmd
import (
"errors"
"fmt"
"strings"

"github.com/JulzDiverse/resc/scriptmanager"
"github.com/JulzDiverse/resc/models/github"
"github.com/spf13/cobra"
)

var printCmd = &cobra.Command{
Use: "print",
Use: "print <script-name>",
Short: "prints the desired script",
Run: print,
}
Expand All @@ -23,27 +22,26 @@ func print(cmd *cobra.Command, args []string) {
userRepo, err := cmd.Flags().GetString("repo")
exitWithError(err)

var user, repo string
if userRepo == "" {
user, repo = configFromFile()
} else {
sl := strings.Split(userRepo, "/")
user = sl[0]
repo = sl[1]
}

scriptManager := scriptmanager.New(
"https://raw.githubusercontent.com",
user,
repo,
)
branch, err := cmd.Flags().GetString("branch")
exitWithError(err)

script, err := scriptManager.GetScript(args[0])
scriptPath, err := cmd.Flags().GetString("script")
exitWithError(err)

scriptManager, _, _, _ := initScriptManager(github.RawContentUrl, userRepo, branch)

var script []byte
if scriptPath == "" {
script, err = scriptManager.Get(args[0])
exitWithError(err)
} else {
script, err = scriptManager.GetScript(scriptPath)
}

fmt.Println(string(script))
}

func initPrint() {
printCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: <user|org>/<repo>")
printCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: <owner>/<repo>")
printCmd.Flags().StringP("branch", "b", "", "branch of the repository containing the script. Default: master")
}
36 changes: 33 additions & 3 deletions cmd/resc/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/JulzDiverse/resc/scriptmanager"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
)
Expand All @@ -15,13 +17,15 @@ var rootCmd = &cobra.Command{
Use: "resc",
Short: "execute remote scripts",
Long: `This tool is executing scripts located on github`,
Version: "0.2.0",
Version: "0.3.0",
}

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

rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setCmd)
Expand All @@ -37,14 +41,40 @@ func Execute() {
}
}

func initScriptManager(url, userRepoString, branchFromFlag string) (*scriptmanager.ScriptManager, string, string, string) {
var user, repo, branch string
if branchFromFlag == "" {
branch = "master"
}

if userRepoString == "" {
user, repo, branch = configFromFile()
} else {
sl := strings.Split(userRepoString, "/")
user = sl[0]
repo = sl[1]
}

if branchFromFlag != "" {
branch = branchFromFlag
}

return scriptmanager.New(
url,
user,
repo,
branch,
), user, repo, branch
}

func exitWithError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Exit: %s", err.Error())
os.Exit(1)
}
}

func configFromFile() (string, string) {
func configFromFile() (string, string, string) {
rc := filepath.Join(os.Getenv("HOME"), ".rsrc")
checkIfConfigFileExists(rc)

Expand All @@ -54,7 +84,7 @@ func configFromFile() (string, string) {
config := Config{}
err = yaml.Unmarshal(configFile, &config)
exitWithError(err)
return config.User, config.Repo
return config.User, config.Repo, config.Branch
}

func checkIfConfigFileExists(rc string) {
Expand Down
Loading

0 comments on commit 715a44f

Please sign in to comment.