From 1caa2ce0ee0f676f9b2ffc184794fde6ce5105fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krisztia=CC=81n=20Go=CC=88drei?= Date: Mon, 1 Aug 2016 11:08:32 +0200 Subject: [PATCH] review --- bitrise.yml | 73 +++++++ deplist.json | 8 - deplock.json | 8 - markdownlog/LICENSE | 22 -- markdownlog/README.md | 3 - markdownlog/_scripts/ci.sh | 24 --- markdownlog/_test_logs/LogReference.md | 7 - markdownlog/_test_logs/LogSectionReference.md | 8 - .../_test_logs/LogSectionStartReference.md | 7 - markdownlog/_test_logs/LogSectionStartTest.md | 7 - markdownlog/_test_logs/LogSectionTest.md | 8 - markdownlog/_test_logs/LogTest.md | 7 - markdownlog/markdownlog.go | 102 --------- markdownlog/markdownlog_test.go | 196 ------------------ release_config.yml | 13 ++ step.go | 98 +++++---- step.sh | 17 +- step.yml | 6 +- 18 files changed, 146 insertions(+), 468 deletions(-) delete mode 100644 deplist.json delete mode 100644 deplock.json delete mode 100644 markdownlog/LICENSE delete mode 100644 markdownlog/README.md delete mode 100644 markdownlog/_scripts/ci.sh delete mode 100644 markdownlog/_test_logs/LogReference.md delete mode 100644 markdownlog/_test_logs/LogSectionReference.md delete mode 100644 markdownlog/_test_logs/LogSectionStartReference.md delete mode 100644 markdownlog/_test_logs/LogSectionStartTest.md delete mode 100644 markdownlog/_test_logs/LogSectionTest.md delete mode 100644 markdownlog/_test_logs/LogTest.md delete mode 100644 markdownlog/markdownlog.go delete mode 100644 markdownlog/markdownlog_test.go create mode 100644 release_config.yml diff --git a/bitrise.yml b/bitrise.yml index a88121e..7bb23e6 100644 --- a/bitrise.yml +++ b/bitrise.yml @@ -1,12 +1,85 @@ format_version: 1.0.0 default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git +app: + envs: + - RELEASE_VERSION: 3.0.0 + workflows: + # ---------------------------------------------------------------- + # --- workflow to Step Test test: steps: + - script: + title: Step audit + inputs: + - content: stepman audit --step-yml=./step.yml + - change-workdir: + title: Switch working dir to test / _tmp dir + run_if: true + inputs: + - path: ./_tmp - path::./: - script: inputs: - content: |- #!/bin/bash echo "RANDOM_QUOTE: ${RANDOM_QUOTE}" + + # ---------------------------------------------------------------- + # --- workflow to create Release version + create-release: + steps: + - script: + title: + inputs: + - content: | + #!/bin/bash + set -e + + export CI=true + releaseman create --version $RELEASE_VERSION + + # ---------------------------------------------------------------- + # --- workflow to Share this step into a Step Library + share-this-step: + envs: + # if you want to share this step into a StepLib + - MY_STEPLIB_REPO_FORK_GIT_URL: $MY_STEPLIB_REPO_FORK_GIT_URL + - STEP_ID_IN_STEPLIB: random-quote + - STEP_GIT_VERION_TAG_TO_SHARE: $RELEASE_VERSION + - STEP_GIT_CLONE_URL: https://github.com/bitrise-io/steps-random-quote.git + description: |- + If this is the first time you try to share a Step you should + first call: $ bitrise share + + This will print you a guide, and information about how Step sharing + works. Please read it at least once! + + As noted in the Step sharing guide you'll have to fork the + StepLib you want to share this step into. Once you're done with forking + the repository you should set your own fork's git clone URL + in the `.bitrise.secrets.yml` file, or here in the `envs` section, + as the value of the `MY_STEPLIB_REPO_FORK_GIT_URL` environment. + + You're now ready to share this Step, just make sure that + the `STEP_ID_IN_STEPLIB` and `STEP_GIT_VERION_TAG_TO_SHARE` + environments are set to the desired values! + + To share this Step into a StepLib you can just run: $ bitrise run share-this-step + + Once it finishes the only thing left is to actually create a Pull Request, + the way described in the guide printed at the end of the process. + steps: + - script: + inputs: + - content: |- + #!/bin/bash + set -e + set -x + + bitrise share start -c ${MY_STEPLIB_REPO_FORK_GIT_URL} + + bitrise share create --stepid ${STEP_ID_IN_STEPLIB} --tag ${STEP_GIT_VERION_TAG_TO_SHARE} --git ${STEP_GIT_CLONE_URL} + + bitrise share finish diff --git a/deplist.json b/deplist.json deleted file mode 100644 index e752b2b..0000000 --- a/deplist.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "deps": [ - { - "url": "https://github.com/bitrise-io/go-markdown-log.git", - "store_path": "markdownlog" - } - ] -} diff --git a/deplock.json b/deplock.json deleted file mode 100644 index c02e680..0000000 --- a/deplock.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "dep_locks": [ - { - "url": "https://github.com/bitrise-io/go-markdown-log.git", - "revision": "1463c6632ec369e94985b6217d4d531db3ef8d1e" - } - ] -} \ No newline at end of file diff --git a/markdownlog/LICENSE b/markdownlog/LICENSE deleted file mode 100644 index a6a5c39..0000000 --- a/markdownlog/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2015 Bitrise - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - diff --git a/markdownlog/README.md b/markdownlog/README.md deleted file mode 100644 index 997a58d..0000000 --- a/markdownlog/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# go-markdown-log - -For loggin to file, and stdout diff --git a/markdownlog/_scripts/ci.sh b/markdownlog/_scripts/ci.sh deleted file mode 100644 index 5ea5563..0000000 --- a/markdownlog/_scripts/ci.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -set -e - -THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -cd "${THIS_SCRIPT_DIR}/.." - -export PATH="$PATH:$GOPATH/bin" - -# -# Script for Continuous Integration -# - -set -v - -go get github.com/kisielk/errcheck -go install github.com/kisielk/errcheck - -errcheck -asserts=true -blank=true ./... - -go test -v ./... -# -# ==> DONE - OK -# \ No newline at end of file diff --git a/markdownlog/_test_logs/LogReference.md b/markdownlog/_test_logs/LogReference.md deleted file mode 100644 index c5a1f98..0000000 --- a/markdownlog/_test_logs/LogReference.md +++ /dev/null @@ -1,7 +0,0 @@ -Message successfully sent! -From: -Krissz -To Romm: -12345 -Message: -Test \ No newline at end of file diff --git a/markdownlog/_test_logs/LogSectionReference.md b/markdownlog/_test_logs/LogSectionReference.md deleted file mode 100644 index 8c6aa7e..0000000 --- a/markdownlog/_test_logs/LogSectionReference.md +++ /dev/null @@ -1,8 +0,0 @@ - -Message successfully sent! -From: -Krissz -To Romm: -12345 -Message: -Test diff --git a/markdownlog/_test_logs/LogSectionStartReference.md b/markdownlog/_test_logs/LogSectionStartReference.md deleted file mode 100644 index b2965fa..0000000 --- a/markdownlog/_test_logs/LogSectionStartReference.md +++ /dev/null @@ -1,7 +0,0 @@ -Message successfully sent! -From: -Krissz -To Romm: -12345 -Message: -Test diff --git a/markdownlog/_test_logs/LogSectionStartTest.md b/markdownlog/_test_logs/LogSectionStartTest.md deleted file mode 100644 index b2965fa..0000000 --- a/markdownlog/_test_logs/LogSectionStartTest.md +++ /dev/null @@ -1,7 +0,0 @@ -Message successfully sent! -From: -Krissz -To Romm: -12345 -Message: -Test diff --git a/markdownlog/_test_logs/LogSectionTest.md b/markdownlog/_test_logs/LogSectionTest.md deleted file mode 100644 index 8c6aa7e..0000000 --- a/markdownlog/_test_logs/LogSectionTest.md +++ /dev/null @@ -1,8 +0,0 @@ - -Message successfully sent! -From: -Krissz -To Romm: -12345 -Message: -Test diff --git a/markdownlog/_test_logs/LogTest.md b/markdownlog/_test_logs/LogTest.md deleted file mode 100644 index c5a1f98..0000000 --- a/markdownlog/_test_logs/LogTest.md +++ /dev/null @@ -1,7 +0,0 @@ -Message successfully sent! -From: -Krissz -To Romm: -12345 -Message: -Test \ No newline at end of file diff --git a/markdownlog/markdownlog.go b/markdownlog/markdownlog.go deleted file mode 100644 index 884a42a..0000000 --- a/markdownlog/markdownlog.go +++ /dev/null @@ -1,102 +0,0 @@ -package markdownlog - -import ( - "fmt" - "os" - "strings" -) - -var pth string - -func Setup(logPath string) { - pth = logPath -} - -func ClearLogFile() error { - if pth != "" { - err := os.Remove(pth) - if err != nil { - return err - } - - fmt.Println("Log file cleared") - } else { - fmt.Errorf("No log path defined!") - } - - return nil -} - -func ErrorMessageToOutput(msg string) { - lines := strings.Split(msg, "\n") - for _, line := range lines { - fmt.Println(line) - } - - if pth != "" { - f, err := os.OpenFile(pth, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - fmt.Errorf("Failed to open log file:", err) - } - defer func() { - err := f.Close() - if err != nil { - fmt.Errorf("Failed to close log file:", err) - } - }() - - _, err = f.Write([]byte(msg)) - if err != nil { - fmt.Errorf("Failed to write log:", err) - } - } else { - fmt.Errorf("No log path defined!") - } -} - -func ErrorSectionToOutput(section string) { - msg := "\n" + section + "\n" - ErrorMessageToOutput(msg) -} - -func ErrorSectionStartToOutput(section string) { - msg := section + "\n" - ErrorMessageToOutput(msg) -} - -func MessageToOutput(msg string) { - lines := strings.Split(msg, "\n") - for _, line := range lines { - fmt.Println(line) - } - - if pth != "" { - f, err := os.OpenFile(pth, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - fmt.Errorf("Failed to open log file:", err) - } - defer func() { - err := f.Close() - if err != nil { - fmt.Errorf("Failed to close log file:", err) - } - }() - - _, err = f.Write([]byte(msg)) - if err != nil { - fmt.Errorf("Failed to write log:", err) - } - } else { - fmt.Errorf("No log path defined!") - } -} - -func SectionToOutput(section string) { - msg := "\n" + section + "\n" - MessageToOutput(msg) -} - -func SectionStartToOutput(section string) { - msg := section + "\n" - MessageToOutput(msg) -} diff --git a/markdownlog/markdownlog_test.go b/markdownlog/markdownlog_test.go deleted file mode 100644 index dfd561b..0000000 --- a/markdownlog/markdownlog_test.go +++ /dev/null @@ -1,196 +0,0 @@ -package markdownlog - -import ( - "errors" - "fmt" - "io/ioutil" - "os" - "testing" -) - -const ( - REFERENCE_LOG_FILE_PATH string = "./_test_logs/LogReference.md" - REFERENCE_SECTION_LOG_FILE_PATH string = "./_test_logs/LogSectionReference.md" - REFERENCE_SECTION_START_LOG_FILE_PATH string = "./_test_logs/LogSectionStartReference.md" - - LOG_TEST_FILE_PATH string = "./_test_logs/LogTest.md" - LOG_SECTION_TEST_FILE_PATH string = "./_test_logs/LogSectionTest.md" - LOG_SECTION_START_FILE_PATH string = "./_test_logs/LogSectionStartTest.md" -) - -func IsPathExists(pth string) (bool, error) { - if pth == "" { - return false, errors.New("No path provided") - } - _, err := os.Stat(pth) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} - -func TestClearLogFile(t *testing.T) { - t.Log("Should remove LogFileTest.md") - - Setup(LOG_TEST_FILE_PATH) - - if pth != LOG_TEST_FILE_PATH { - t.Error("Log path setup failed") - } - - exist, err := IsPathExists(pth) - if err != nil { - t.Error("Failed to check path:", err) - } - if exist { - e := ClearLogFile() - if e != nil { - t.Error("Failed to clear LogFileTest.md:", e) - } - } -} - -func TestMessageToOutput(t *testing.T) { - t.Log("LogReference.md should be same as LogTest.md") - - Setup(LOG_TEST_FILE_PATH) - if pth != LOG_TEST_FILE_PATH { - t.Error("Log path setup failed") - } - - exist, err := IsPathExists(pth) - if err != nil { - t.Error("Failed to check path:", err) - } - if exist { - e := ClearLogFile() - if e != nil { - t.Error("Failed to clear LogFileTest.md:", e) - } - } - - // Read reference log file - content, err := ioutil.ReadFile(REFERENCE_LOG_FILE_PATH) - if err != nil { - t.Error("Failed to open reference log file:", err) - } - refLog := string(content) - - // Generate test log file - testlog := generateTestMessage() - MessageToOutput(testlog) - content, err = ioutil.ReadFile(LOG_TEST_FILE_PATH) - if err != nil { - t.Error("Failed to open reference log file:", err) - } - testlog = string(content) - - if refLog != testlog { - t.Error("Wrong log file format") - } -} - -func TestSectionToOutput(t *testing.T) { - t.Log("LogSectionReference.md should be same as LogTest.md") - - Setup(LOG_SECTION_TEST_FILE_PATH) - if pth != LOG_SECTION_TEST_FILE_PATH { - t.Error("Log path setup failed") - } - - exist, err := IsPathExists(pth) - if err != nil { - t.Error("Failed to check path:", err) - } - if exist { - e := ClearLogFile() - if e != nil { - t.Error("Failed to clear LogFileTest.md:", e) - } - } - - // Read reference log file - content, err := ioutil.ReadFile(REFERENCE_SECTION_LOG_FILE_PATH) - if err != nil { - t.Error("Failed to open reference log file:", err) - } - refLog := string(content) - - // Generate test log file - testlog := generateTestMessage() - SectionToOutput(testlog) - content, err = ioutil.ReadFile(LOG_SECTION_TEST_FILE_PATH) - if err != nil { - t.Error("Failed to open reference log file:", err) - } - testlog = string(content) - - if refLog != testlog { - t.Error("Wrong log file format") - - fmt.Println("Reference: ") - fmt.Print(refLog) - fmt.Println("Generated:") - fmt.Print(testlog) - } -} - -func TestSectionStartToOutput(t *testing.T) { - t.Log("LogSectionStartReference.md should be same as LogTest.md") - - Setup(LOG_SECTION_START_FILE_PATH) - if pth != LOG_SECTION_START_FILE_PATH { - t.Error("Log path setup failed") - } - - exist, err := IsPathExists(pth) - if err != nil { - t.Error("Failed to check path:", err) - } - if exist { - e := ClearLogFile() - if e != nil { - t.Error("Failed to clear LogFileTest.md:", e) - } - } - - // Read reference log file - content, err := ioutil.ReadFile(REFERENCE_SECTION_START_LOG_FILE_PATH) - if err != nil { - t.Error("Failed to open reference log file:", err) - } - refLog := string(content) - - // Generate test log file - testlog := generateTestMessage() - SectionStartToOutput(testlog) - content, err = ioutil.ReadFile(LOG_SECTION_START_FILE_PATH) - if err != nil { - t.Error("Failed to open reference log file:", err) - } - testlog = string(content) - - if refLog != testlog { - t.Error("Wrong log file format") - - fmt.Println("Reference: ") - fmt.Print(refLog) - fmt.Println("Generated:") - fmt.Print(testlog) - } -} - -func generateTestMessage() string { - message := "Message successfully sent!\n" - message = message + "From:\n" - message = message + "Krissz" + "\n" - message = message + "To Romm:\n" - message = message + "12345" + "\n" - message = message + "Message:\n" - message = message + "Test" - - return message -} diff --git a/release_config.yml b/release_config.yml new file mode 100644 index 0000000..4a1f737 --- /dev/null +++ b/release_config.yml @@ -0,0 +1,13 @@ +release: + development_branch: master + release_branch: master +changelog: + path: CHANGELOG.md + content_template: |- + {{range .ContentItems}}### {{.EndTaggedCommit.Tag}} ({{.EndTaggedCommit.Date.Format "2006 Jan 02"}}) + + {{range .Commits}}* [{{firstChars .Hash 7}}] {{.Message}} + {{end}} + {{end}} + header_template: '## Changelog (Current version: {{.Version}})' + footer_template: 'Updated: {{.CurrentDate.Format "2006 Jan 02"}}' diff --git a/step.go b/step.go index b34e589..c3b08d8 100644 --- a/step.go +++ b/step.go @@ -9,93 +9,101 @@ import ( "os" "os/exec" "strings" +) - "./markdownlog" +const ( + urlString = "http://api.icndb.com/jokes/random" ) -func errorMessageToOutput(msg string) { - message := "Quote generation failed!\n" - message = message + "Error message:\n" - message = message + msg - markdownlog.ErrorSectionToOutput(message) +func fail(format string, v ...interface{}) { + errorMsg := fmt.Sprintf(format, v...) + fmt.Printf("\x1b[31;1m%s\x1b[0m\n", errorMsg) + os.Exit(1) +} + +func warn(format string, v ...interface{}) { + errorMsg := fmt.Sprintf(format, v...) + fmt.Printf("\x1b[33;1m%s\x1b[0m\n", errorMsg) +} + +func info(format string, v ...interface{}) { + fmt.Println() + errorMsg := fmt.Sprintf(format, v...) + fmt.Printf("\x1b[34;1m%s\x1b[0m\n", errorMsg) } -func successMessageToOutput(msg string) { - message := "Quote successfully generated!\n" - message = message + "Quote:\n" - message = message + msg + "\n" - markdownlog.SectionToOutput(message) +func details(format string, v ...interface{}) { + errorMsg := fmt.Sprintf(format, v...) + fmt.Printf(" %s\n", errorMsg) } -// RunPipedEnvmanAdd ... -func RunPipedEnvmanAdd(key, value string) error { - args := []string{"add", "-k", key} - envman := exec.Command("envman", args...) - envman.Stdin = strings.NewReader(value) +func done(format string, v ...interface{}) { + errorMsg := fmt.Sprintf(format, v...) + fmt.Printf(" \x1b[32;1m%s\x1b[0m\n", errorMsg) +} + +func exportEnvironmentWithEnvman(keyStr, valueStr string) error { + envman := exec.Command("envman", "add", "--key", keyStr) + envman.Stdin = strings.NewReader(valueStr) envman.Stdout = os.Stdout envman.Stderr = os.Stderr return envman.Run() } func main() { - // init / cleanup the formatted output - pth := os.Getenv("BITRISE_STEP_FORMATTED_OUTPUT_FILE_PATH") - markdownlog.Setup(pth) - // request - urlString := "http://api.icndb.com/jokes/random" - request, err := http.NewRequest("GET", urlString, nil) if err != nil { - errorMessageToOutput(fmt.Sprintf("Failed to create requestuest, err: %s", err)) - os.Exit(1) + fail("Failed to create requestuest, err: %s", err) } request.Header.Add("Accept", "application/json") request.Header.Add("Content-Type", "application/x-www-form-urlencoded") // perform request + info("Getting random quote from: %s", urlString) + client := &http.Client{} response, err := client.Do(request) - - var data map[string]interface{} - bodyBytes, err := ioutil.ReadAll(response.Body) if err != nil { - errorMessageToOutput(fmt.Sprintf("Failed to read request body, err: %s", err)) - os.Exit(1) + fail("Failed to perform request, err: %s", err) } - err = json.Unmarshal(bodyBytes, &data) + + defer func() { + if err := response.Body.Close(); err != nil { + warn("Failed to close response body, error: %s", err) + } + }() + + bodyBytes, err := ioutil.ReadAll(response.Body) if err != nil { - errorMessageToOutput(fmt.Sprintf("Json unmarshal failed, err: %s", err)) - os.Exit(1) + fail("Failed to read request body, err: %s", err) } if response.StatusCode == 200 { + var data map[string]interface{} + if err = json.Unmarshal(bodyBytes, &data); err != nil { + fail("Failed to unmarshal (%s), err: %s", string(bodyBytes), err) + } + value := data["value"] valueMap, isKind := value.(map[string]interface{}) if isKind == false { - errorMessageToOutput(fmt.Sprintf("Failed to convert response, err: %s", err)) - os.Exit(1) + fail("Failed to convert response: %s", value) } joke := valueMap["joke"].(string) joke, err = url.QueryUnescape(joke) if err != nil { - errorMessageToOutput(fmt.Sprintf("Failed to url decode response, err: %s", err)) - os.Exit(1) + fail("Failed to url decode response (%s), err: %s", joke, err) } - err := RunPipedEnvmanAdd("RANDOM_QUOTE", joke) - if err != nil { - errorMessageToOutput(fmt.Sprintf("Failed to add output to envman, err: %s", err)) - os.Exit(1) + if err := exportEnvironmentWithEnvman("RANDOM_QUOTE", joke); err != nil { + fail("Failed to add output to envman, err: %s", err) } - successMessageToOutput(fmt.Sprintf("%v", valueMap["joke"])) + done(joke) } else { - errorMsg := fmt.Sprintf("Status code: %d Body: %s", response.StatusCode, response.Body) - errorMessageToOutput(errorMsg) - - os.Exit(1) + fail("Status code: %d Body: %s", response.StatusCode, response.Body) } } diff --git a/step.sh b/step.sh index efad1a1..673f501 100755 --- a/step.sh +++ b/step.sh @@ -1,15 +1,6 @@ #!/bin/bash -THIS_SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -# Start go program -cd "${THIS_SCRIPTDIR}" - -go run ./step.go -ex_code=$? - -if [ ${ex_code} -eq 0 ] ; then - exit 0 -fi - -exit 1 +THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +export GO15VENDOREXPERIMENT="1" +go run "${THIS_SCRIPT_DIR}/step.go" +exit $? diff --git a/step.yml b/step.yml index 2861a65..feb1395 100644 --- a/step.yml +++ b/step.yml @@ -16,9 +16,9 @@ type_tags: is_requires_admin_user: false is_always_run: false is_skippable: false -dependencies: - - manager: brew - name: go +deps: + brew: + - name: go run_if: "" inputs: [] outputs: