From 715a44fbcdc7028f35509cfdbb690e2bf36e78aa Mon Sep 17 00:00:00 2001 From: Julian Skupnjak Date: Wed, 4 Jul 2018 10:00:31 +0200 Subject: [PATCH] add branch option; add run --script option --- README.md | 43 ++++- cmd/resc/cmd/list.go | 33 ++-- cmd/resc/cmd/man.go | 23 +-- cmd/resc/cmd/print.go | 36 ++-- cmd/resc/cmd/root.go | 36 +++- cmd/resc/cmd/run.go | 46 +++--- cmd/resc/cmd/set.go | 69 +++++++- models/github/models.go | 6 + scriptmanager/list.go | 50 ++++-- scriptmanager/list_test.go | 9 +- scriptmanager/manager.go | 26 +-- scriptmanager/manager_test.go | 301 +++++++++++++++++++++------------- 12 files changed, 442 insertions(+), 236 deletions(-) create mode 100644 models/github/models.go diff --git a/README.md b/README.md index 6cb86bb..5945cf7 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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! @@ -68,18 +68,24 @@ Running a `resc` script is nothing more than: $ resc run --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 ``` -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 -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`. @@ -94,13 +100,13 @@ 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 / ``` @@ -108,7 +114,7 @@ $ resc list / 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 ``` @@ -116,16 +122,35 @@ $ resc man 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 ``` to save a script, pipe it to a file: -``` +```bash $ resc print > 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 / -b develop +``` + + ## 💻 Development ``` diff --git a/cmd/resc/cmd/list.go b/cmd/resc/cmd/list.go index 042bbc7..d4764ec 100644 --- a/cmd/resc/cmd/list.go +++ b/cmd/resc/cmd/list.go @@ -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: /") } func listScripts(repo string, scripts []string) { diff --git a/cmd/resc/cmd/man.go b/cmd/resc/cmd/man.go index 4c0965f..851e3f6 100644 --- a/cmd/resc/cmd/man.go +++ b/cmd/resc/cmd/man.go @@ -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 ", Short: "print the manual of a remote script", Run: man, } @@ -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) @@ -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: /") + manCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: /") + manCmd.Flags().StringP("branch", "b", "", "branch of the repository containing the script. Default: master") } diff --git a/cmd/resc/cmd/print.go b/cmd/resc/cmd/print.go index 1d49695..871cf26 100644 --- a/cmd/resc/cmd/print.go +++ b/cmd/resc/cmd/print.go @@ -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 ", Short: "prints the desired script", Run: print, } @@ -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: /") + printCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: /") + printCmd.Flags().StringP("branch", "b", "", "branch of the repository containing the script. Default: master") } diff --git a/cmd/resc/cmd/root.go b/cmd/resc/cmd/root.go index 777bd93..f4a1bea 100644 --- a/cmd/resc/cmd/root.go +++ b/cmd/resc/cmd/root.go @@ -6,7 +6,9 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" + "github.com/JulzDiverse/resc/scriptmanager" "github.com/ghodss/yaml" "github.com/spf13/cobra" ) @@ -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) @@ -37,6 +41,32 @@ 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()) @@ -44,7 +74,7 @@ func exitWithError(err error) { } } -func configFromFile() (string, string) { +func configFromFile() (string, string, string) { rc := filepath.Join(os.Getenv("HOME"), ".rsrc") checkIfConfigFileExists(rc) @@ -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) { diff --git a/cmd/resc/cmd/run.go b/cmd/resc/cmd/run.go index 6851914..445ff88 100644 --- a/cmd/resc/cmd/run.go +++ b/cmd/resc/cmd/run.go @@ -4,24 +4,28 @@ import ( "errors" "strings" + "github.com/JulzDiverse/resc/models/github" "github.com/JulzDiverse/resc/runner" - "github.com/JulzDiverse/resc/scriptmanager" "github.com/spf13/cobra" ) type Config struct { - User string `yaml:"user"` - Repo string `yaml:"repo"` + User string `yaml:"user"` + Repo string `yaml:"repo"` + Branch string `yaml:"branch"` } var runCmd = &cobra.Command{ - Use: "run", + Use: "run ", Short: "run a remote script", Run: run, } func run(cmd *cobra.Command, args []string) { - if len(args) == 0 { + scriptPath, err := cmd.Flags().GetString("script") + exitWithError(err) + + if len(args) == 0 && scriptPath == "" { exitWithError(errors.New("No script specified")) } @@ -31,32 +35,28 @@ func run(cmd *cobra.Command, args []string) { argsString, err := cmd.Flags().GetString("args") exitWithError(err) - runArgs := strings.Split(argsString, " ") - - 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, _, _, _ := initScriptManager(github.RawContentUrl, userRepo, branch) runner := runner.New(runner.Default) - scriptManager := scriptmanager.New( - "https://raw.githubusercontent.com", - user, - repo, - ) - script, err := scriptManager.GetScript(args[0]) - exitWithError(err) + var script []byte + if scriptPath == "" { + script, err = scriptManager.Get(args[0]) + exitWithError(err) + } else { + script, err = scriptManager.GetScript(scriptPath) + } + runArgs := strings.Split(argsString, " ") _, err = runner.Run(string(script), runArgs...) exitWithError(err) } func initRun() { - runCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: /") + runCmd.Flags().StringP("repo", "r", "", "name of the repository containing the script. Pattern: /") + runCmd.Flags().StringP("branch", "b", "", "branch of the repository containing the script. Default: master") + runCmd.Flags().StringP("script", "s", "", "path to a specific script file in the specified repository (eg topDir/subDir/script.sh)") runCmd.Flags().StringP("args", "a", "", "space separated list of arguments: eg '-c config -v var'") } diff --git a/cmd/resc/cmd/set.go b/cmd/resc/cmd/set.go index 2a6431d..76c8b64 100644 --- a/cmd/resc/cmd/set.go +++ b/cmd/resc/cmd/set.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "io/ioutil" "os" "path/filepath" @@ -11,20 +12,76 @@ import ( ) var setCmd = &cobra.Command{ - Use: "set", + Use: "set [owner/repository]", Short: "set a default resc repository", Run: set, } func set(cmd *cobra.Command, args []string) { - userRepo := strings.Split(args[0], "/") - config := Config{ - User: userRepo[0], - Repo: userRepo[1], + rsrc := filepath.Join(os.Getenv("HOME"), ".rsrc") + owner, repo, branch := parseFlags(cmd) + + validateInput(args, owner, repo, branch) + + if len(args) != 0 { + userRepo := strings.Split(args[0], "/") + owner = userRepo[0] + repo = userRepo[1] } + config := setConfig(owner, repo, branch, rsrc) + conf, err := yaml.Marshal(config) exitWithError(err) - err = ioutil.WriteFile(filepath.Join(os.Getenv("HOME"), ".rsrc"), conf, 0644) + err = ioutil.WriteFile(rsrc, conf, 0644) + exitWithError(err) +} + +func initSet() { + setCmd.Flags().StringP("branch", "b", "master", "set the default branch of the repository containing the scripts. Default ' master'") + setCmd.Flags().StringP("owner", "o", "", "set the default owner of a repository") + setCmd.Flags().StringP("repo", "r", "", "set the default repository to be used") +} + +func setConfig(owner, repo, branch, rsrc string) Config { + var config Config + if _, err := os.Stat(rsrc); err == nil { + file, err := ioutil.ReadFile(rsrc) + exitWithError(err) + err = yaml.Unmarshal(file, &config) + exitWithError(err) + if owner != "" { + config.User = owner + } + if repo != "" { + config.Repo = repo + } + if branch != "" { + config.Branch = branch + } + } else { + config = Config{ + User: owner, + Repo: repo, + Branch: branch, + } + } + return config +} + +func parseFlags(cmd *cobra.Command) (string, string, string) { + owner, err := cmd.Flags().GetString("owner") + exitWithError(err) + repo, err := cmd.Flags().GetString("repo") + exitWithError(err) + branch, err := cmd.Flags().GetString("branch") + exitWithError(err) + return owner, repo, branch +} + +func validateInput(args []string, owner, repo, branch string) { + if len(args) == 0 && owner == "" && repo == "" && branch == "" { + exitWithError(errors.New("No owner, repository, or branch specified. Use 'resc help set' for more info")) + } } diff --git a/models/github/models.go b/models/github/models.go new file mode 100644 index 0000000..8b03a41 --- /dev/null +++ b/models/github/models.go @@ -0,0 +1,6 @@ +package github + +const ( + ApiUrl = "https://api.github.com" + RawContentUrl = "https://raw.githubusercontent.com" +) diff --git a/scriptmanager/list.go b/scriptmanager/list.go index a1b5602..08ac58a 100644 --- a/scriptmanager/list.go +++ b/scriptmanager/list.go @@ -3,6 +3,7 @@ package scriptmanager import ( "encoding/json" "fmt" + "io/ioutil" "net/http" "github.com/pkg/errors" @@ -14,13 +15,7 @@ type Content struct { } 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) + contents, err := getContents(fmt.Sprintf("%s/repos/%s/%s/contents", s.url, s.user, s.repo), s.branch) if err != nil { return nil, err } @@ -28,7 +23,7 @@ func (s *ScriptManager) List() ([]string, error) { 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 { + if ok, err := exists(fmt.Sprintf("%s/repos/%s/%s/contents/%s/.resc", s.url, s.user, s.repo, content.Name), s.branch); ok { validScripts = append(validScripts, content.Name) } else { if err != nil { @@ -40,8 +35,43 @@ func (s *ScriptManager) List() ([]string, error) { return validScripts, nil } -func exists(url string) (bool, error) { - resp, err := http.Get(url) +func doQuery(url string, branch string) (*http.Response, error) { + client := http.Client{} + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, errors.Wrap(err, "failed to build request") + } + + q := req.URL.Query() + q.Add("ref", branch) + req.URL.RawQuery = q.Encode() + + return client.Do(req) +} + +func getContents(url, branch string) ([]Content, error) { + resp, err := doQuery(url, branch) + if err != nil { + return nil, errors.Wrap(err, "failed to perform http GET") + } + + raw, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var contents []Content + err = json.Unmarshal(raw, &contents) + if err != nil { + return nil, err + } + + return contents, nil +} + +func exists(url string, branch string) (bool, error) { + resp, err := doQuery(url, branch) if err != nil { return false, errors.Wrap(err, "failed to perform http GET") } diff --git a/scriptmanager/list_test.go b/scriptmanager/list_test.go index 344e81e..5842a84 100644 --- a/scriptmanager/list_test.go +++ b/scriptmanager/list_test.go @@ -13,9 +13,9 @@ import ( var _ = Describe("List", func() { var ( - scriptManager *ScriptManager - fakeServer *ghttp.Server - url, user, repo string + scriptManager *ScriptManager + fakeServer *ghttp.Server + url, user, repo, branch string ) BeforeEach(func() { @@ -23,10 +23,11 @@ var _ = Describe("List", func() { url = fakeServer.URL() user = "zeus" repo = "pandora" + branch = "master" }) JustBeforeEach(func() { - scriptManager = New(url, user, repo) + scriptManager = New(url, user, repo, branch) }) Context("listing available scripts in a repository", func() { diff --git a/scriptmanager/manager.go b/scriptmanager/manager.go index 0df3618..5aa3f4a 100644 --- a/scriptmanager/manager.go +++ b/scriptmanager/manager.go @@ -9,25 +9,31 @@ import ( ) type ScriptManager struct { - url string - user string - repo string + url string + user string + repo string + branch string } -func New(url, user, repo string) *ScriptManager { +func New(url, user, repo, branch string) *ScriptManager { return &ScriptManager{ - url: url, - user: user, - repo: repo, + url: url, + user: user, + repo: repo, + branch: branch, } } -func (s *ScriptManager) GetScript(scriptName string) ([]byte, error) { - return getter(fmt.Sprintf("%s/%s/%s/master/%s/run.sh", s.url, s.user, s.repo, scriptName)) +func (s *ScriptManager) Get(scriptName string) ([]byte, error) { + return getter(fmt.Sprintf("%s/%s/%s/%s/%s/run.sh", s.url, s.user, s.repo, s.branch, scriptName)) +} + +func (s *ScriptManager) GetScript(scriptPath string) ([]byte, error) { + return getter(fmt.Sprintf("%s/%s/%s/%s/%s", s.url, s.user, s.repo, s.branch, scriptPath)) } func (s *ScriptManager) GetReadmeForScript(scriptName string) ([]byte, error) { - return getter(fmt.Sprintf("%s/%s/%s/master/%s/README.md", s.url, s.user, s.repo, scriptName)) + return getter(fmt.Sprintf("%s/%s/%s/%s/%s/README.md", s.url, s.user, s.repo, s.branch, scriptName)) } func getter(url string) ([]byte, error) { diff --git a/scriptmanager/manager_test.go b/scriptmanager/manager_test.go index 81121b2..6859cac 100644 --- a/scriptmanager/manager_test.go +++ b/scriptmanager/manager_test.go @@ -12,147 +12,212 @@ import ( var _ = Describe("Manager", func() { - Describe("", func() { + var ( + scriptManager *ScriptManager + fakeServer *ghttp.Server + url, user, repo, branch, scriptName, expected string + ) + + BeforeEach(func() { + fakeServer = ghttp.NewServer() + url = fakeServer.URL() + user = "zeus" + repo = "pandora" + branch = "master" + scriptName = "open-pandora" - var ( - scriptManager *ScriptManager - fakeServer *ghttp.Server - url, user, repo, scriptName, expected string - ) + }) - BeforeEach(func() { - fakeServer = ghttp.NewServer() - url = fakeServer.URL() - user = "zeus" - repo = "pandora" - scriptName = "open-pandora" + JustBeforeEach(func() { + scriptManager = New(url, user, repo, branch) + }) - }) + Context("When requesting a script", func() { + Context("When the request can be performed", func() { + BeforeEach(func() { + fakeServer.AppendHandlers( + ghttp.CombineHandlers( + ghttp.VerifyRequest( + "GET", + "/zeus/pandora/master/open-pandora/run.sh", + ), + ghttp.RespondWith( + http.StatusOK, + expected, + ), + ), + ) + + expected = `!/bin/bash +echo "pandora opened"` + }) + + It("should send the request", func() { + _, err := scriptManager.Get(scriptName) + Expect(err).ToNot(HaveOccurred()) + Expect(fakeServer.ReceivedRequests()).Should(HaveLen(1)) + }) - JustBeforeEach(func() { - scriptManager = New(url, user, repo) + It("should return the expected response", func() { + script, err := scriptManager.Get(scriptName) + Expect(err).ToNot(HaveOccurred()) + Expect(string(script)).To(Equal(expected)) + }) }) - Context("When requesting a script", func() { - Context("When the request can be performed", func() { - BeforeEach(func() { - fakeServer.AppendHandlers( - ghttp.CombineHandlers( - ghttp.VerifyRequest( - "GET", - "/zeus/pandora/master/open-pandora/run.sh", - ), - ghttp.RespondWith( - http.StatusOK, - expected, - ), - ), - ) + Context("When response status is other than 200 OK", func() { + BeforeEach(func() { + fakeServer.AppendHandlers( + ghttp.RespondWith( + http.StatusNotFound, + "", + ), + ) + }) - expected = `!/bin/bash -echo "pandora opened"` - }) + It("should error", func() { + _, err := scriptManager.Get(scriptName) + Expect(err).To(HaveOccurred()) + }) - It("should send the request", func() { - _, err := scriptManager.GetScript(scriptName) - Expect(err).ToNot(HaveOccurred()) - Expect(fakeServer.ReceivedRequests()).Should(HaveLen(1)) - }) + It("should return an nil slice", func() { + script, err := scriptManager.Get(scriptName) + Expect(err).To(HaveOccurred()) + Expect(script).To(BeNil()) + }) - It("should return the expected response", func() { - script, err := scriptManager.GetScript(scriptName) - Expect(err).ToNot(HaveOccurred()) - Expect(string(script)).To(Equal(expected)) - }) + It("should return a meaningful message", func() { + _, err := scriptManager.Get(scriptName) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(ContainSubstring("requesting file failed: 404 Not Found"))) }) + }) + }) + + Context("When requesting a specific script", func() { + var ( + scriptPath string + ) - Context("When response status is other than 200 OK", func() { - BeforeEach(func() { - fakeServer.AppendHandlers( + Context("When the request can be performed", func() { + BeforeEach(func() { + fakeServer.AppendHandlers( + ghttp.CombineHandlers( + ghttp.VerifyRequest( + "GET", + "/zeus/pandora/master/some/specific/script.sh", + ), ghttp.RespondWith( - http.StatusNotFound, - "", + http.StatusOK, + expected, ), - ) - }) - - It("should error", func() { - _, err := scriptManager.GetScript(scriptName) - Expect(err).To(HaveOccurred()) - }) - - It("should return an nil slice", func() { - script, err := scriptManager.GetScript(scriptName) - Expect(err).To(HaveOccurred()) - Expect(script).To(BeNil()) - }) - - It("should return a meaningful message", func() { - _, err := scriptManager.GetScript(scriptName) - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(ContainSubstring("requesting file failed: 404 Not Found"))) - }) + ), + ) + + scriptPath = "some/specific/script.sh" + expected = `!/bin/bash +echo "pandora opened"` + }) + + It("should send the request", func() { + _, err := scriptManager.GetScript(scriptPath) + Expect(err).ToNot(HaveOccurred()) + Expect(fakeServer.ReceivedRequests()).Should(HaveLen(1)) + }) + + It("should return the expected response", func() { + script, err := scriptManager.GetScript(scriptPath) + Expect(err).ToNot(HaveOccurred()) + Expect(string(script)).To(Equal(expected)) }) }) - Context("When requesting a README", func() { - Context("When the request can be performed", func() { - BeforeEach(func() { - fakeServer.AppendHandlers( - ghttp.CombineHandlers( - ghttp.VerifyRequest( - "GET", - "/zeus/pandora/master/open-pandora/README.md", - ), - ghttp.RespondWith( - http.StatusOK, - expected, - ), - ), - ) + Context("When response status is other than 200 OK", func() { + BeforeEach(func() { + fakeServer.AppendHandlers( + ghttp.RespondWith( + http.StatusNotFound, + "", + ), + ) + }) - expected = "# Topic" - }) + It("should error", func() { + _, err := scriptManager.Get(scriptPath) + Expect(err).To(HaveOccurred()) + }) - It("should send the request", func() { - _, err := scriptManager.GetReadmeForScript(scriptName) - Expect(err).ToNot(HaveOccurred()) - Expect(fakeServer.ReceivedRequests()).Should(HaveLen(1)) - }) + It("should return an nil slice", func() { + script, err := scriptManager.Get(scriptPath) + Expect(err).To(HaveOccurred()) + Expect(script).To(BeNil()) + }) - It("should return the expected response", func() { - readme, err := scriptManager.GetReadmeForScript(scriptName) - Expect(err).ToNot(HaveOccurred()) - Expect(string(readme)).To(Equal(expected)) - }) + It("should return a meaningful message", func() { + _, err := scriptManager.Get(scriptPath) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(ContainSubstring("requesting file failed: 404 Not Found"))) }) + }) + }) - Context("When response status is other than 200 OK", func() { - BeforeEach(func() { - fakeServer.AppendHandlers( + Context("When requesting a README", func() { + Context("When the request can be performed", func() { + BeforeEach(func() { + fakeServer.AppendHandlers( + ghttp.CombineHandlers( + ghttp.VerifyRequest( + "GET", + "/zeus/pandora/master/open-pandora/README.md", + ), ghttp.RespondWith( - http.StatusNotFound, - "", + http.StatusOK, + expected, ), - ) - }) - - It("should error", func() { - _, err := scriptManager.GetReadmeForScript(scriptName) - Expect(err).To(HaveOccurred()) - }) - - It("should return an nil slice", func() { - readme, err := scriptManager.GetReadmeForScript(scriptName) - Expect(err).To(HaveOccurred()) - Expect(readme).To(BeNil()) - }) - - It("should return a meaningful message", func() { - _, err := scriptManager.GetReadmeForScript(scriptName) - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(ContainSubstring("requesting file failed: 404 Not Found"))) - }) + ), + ) + + expected = "# Topic" + }) + + It("should send the request", func() { + _, err := scriptManager.GetReadmeForScript(scriptName) + Expect(err).ToNot(HaveOccurred()) + Expect(fakeServer.ReceivedRequests()).Should(HaveLen(1)) + }) + + It("should return the expected response", func() { + readme, err := scriptManager.GetReadmeForScript(scriptName) + Expect(err).ToNot(HaveOccurred()) + Expect(string(readme)).To(Equal(expected)) + }) + }) + + Context("When response status is other than 200 OK", func() { + BeforeEach(func() { + fakeServer.AppendHandlers( + ghttp.RespondWith( + http.StatusNotFound, + "", + ), + ) + }) + + It("should error", func() { + _, err := scriptManager.GetReadmeForScript(scriptName) + Expect(err).To(HaveOccurred()) + }) + + It("should return an nil slice", func() { + readme, err := scriptManager.GetReadmeForScript(scriptName) + Expect(err).To(HaveOccurred()) + Expect(readme).To(BeNil()) + }) + + It("should return a meaningful message", func() { + _, err := scriptManager.GetReadmeForScript(scriptName) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(ContainSubstring("requesting file failed: 404 Not Found"))) }) }) })