Skip to content

Commit

Permalink
add logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Sotomski authored and Robert Sotomski committed Feb 20, 2019
1 parent 19271f9 commit f43e7d0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 82 deletions.
6 changes: 3 additions & 3 deletions cmd/issueTestWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/sotomskir/jira-cli/jiraApi"
"github.com/sotomskir/jira-cli/logger"
"os"

"github.com/spf13/cobra"
Expand All @@ -31,11 +31,11 @@ var testWorkflowCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
workflow, err := cmd.Flags().GetString("workflow")
if err != nil {
logger.ErrorLn(err)
logrus.Errorln(err)
os.Exit(1)
}
jiraApi.TestTransitions(workflow, args[0])
logger.SuccessLn("issueTransitionTest PASSED")
logrus.Infoln("issueTransitionTest PASSED")
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/issueTransition.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/sotomskir/jira-cli/jiraApi"
"github.com/sotomskir/jira-cli/logger"
"os"

"github.com/spf13/cobra"
Expand All @@ -33,11 +33,11 @@ var issueTransitionCmd = &cobra.Command{
targetState := args[1]
workflow, err := cmd.Flags().GetString("workflow")
if err != nil {
logger.ErrorLn(err)
logrus.Errorln(err)
os.Exit(1)
}
jiraApi.TransitionIssue(workflow, issueKey, targetState)
logger.SuccessF("Success issue '%s' is in status '%s'\n", issueKey, targetState)
logrus.Infof("Success issue '%s' is in status '%s'\n", issueKey, targetState)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/issueVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/sotomskir/jira-cli/jiraApi"
"github.com/sotomskir/jira-cli/logger"
"github.com/spf13/cobra"
)

Expand All @@ -35,7 +35,7 @@ If version does not exist it will be created`,
version := args[1]
result := jiraApi.SetFixVersion(issueKey, version)
if result {
logger.SuccessF("Success version %s set for issue %s\n", version, issueKey)
logrus.Infof("Success version %s set for issue %s\n", version, issueKey)
}
},
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"github.com/sotomskir/jira-cli/logger"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
Expand Down Expand Up @@ -53,7 +53,7 @@ var loginCmd = &cobra.Command{
loggedIn := login(server, user, password)

if loggedIn {
logger.SuccessF("Success, Logged in to: %s as: %s\n", server, user)
logrus.Infof("Success, Logged in to: %s as: %s\n", server, user)
}
},
}
Expand Down Expand Up @@ -104,33 +104,33 @@ func login(server string, user string, password string) bool {

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
logger.ErrorLn(err)
logrus.Errorln(err)
os.Exit(1)
}

req.SetBasicAuth(user, password)
res, getErr := httpClient.Do(req)
if getErr != nil {
logger.ErrorLn(getErr)
logrus.Errorln(getErr)
os.Exit(1)
}

if res.StatusCode == 401 {
logger.ErrorLn("Server responded with status: 401 Unauthorized")
logrus.Errorln("Server responded with status: 401 Unauthorized")
os.Exit(1)
}

body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
logger.ErrorLn("Cannot read response body: " + readErr.Error())
logrus.Errorln("Cannot read response body: " + readErr.Error())
os.Exit(1)
}

jiraUser := JiraUser{}
jsonErr := json.Unmarshal(body, &jiraUser)
if jsonErr != nil {
logger.ErrorF("%s\n", body)
logger.ErrorLn("Server responded invalid JSON")
logrus.Errorf("%s\n", body)
logrus.Errorln("Server responded invalid JSON")
os.Exit(1)
}

Expand Down
26 changes: 19 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
package cmd

import (
"github.com/fatih/color"
"github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
"github.com/sotomskir/jira-cli/jiraApi"
"github.com/sotomskir/jira-cli/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
Expand All @@ -36,12 +35,14 @@ var rootCmd = &cobra.Command{
jira-cli is a CLI for Atlassian Jira REST API.
It can be used with CI/CD pipelines to automate workflow.`,
}
var debug bool
var trace bool

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
logger.ErrorLn(err)
logrus.Errorln(err)
os.Exit(1)
}
}
Expand All @@ -58,14 +59,25 @@ func init() {
// will be global for your application.
//rootCmd.PersistentFlags().StringVarP(&user, "user", "u", "admin", "Jira username")
//rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "admin", "Jira password")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Debug output")
rootCmd.PersistentFlags().BoolVar(&trace, "trace", false, "Trace output")
// Cobra also supports local flags, which will only run
// when this action is called directly.
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if noColor {
color.NoColor = true
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: noColor,
ForceColors: true,
DisableTimestamp: true,
DisableLevelTruncation: true,
})
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if trace {
logrus.SetLevel(logrus.TraceLevel)
}
if cfgFile != "" {
// Use config file from the flag.
Expand All @@ -74,7 +86,7 @@ func initConfig() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
logger.ErrorLn(err)
logrus.Errorln(err)
os.Exit(1)
}

Expand All @@ -92,7 +104,7 @@ func initConfig() {
} else {
home, err := homedir.Dir()
if err != nil {
logger.ErrorLn(err)
logrus.Errorln(err)
os.Exit(1)
}
viper.WriteConfigAs(path.Join(home, "/.jira-cli.yaml"))
Expand Down
5 changes: 2 additions & 3 deletions cmd/versionCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/sotomskir/jira-cli/jiraApi"
"github.com/sotomskir/jira-cli/logger"

"github.com/spf13/cobra"
)

Expand All @@ -32,7 +31,7 @@ var versionCreateCmd = &cobra.Command{
projectKey := args[0]
version := args[1]
response := jiraApi.CreateVersion(projectKey, version)
logger.SuccessF("Success version created %#v\n", response)
logrus.Infof("Success version created %#v\n", response)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/versionRelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/sotomskir/jira-cli/jiraApi"
"github.com/sotomskir/jira-cli/logger"
"github.com/spf13/cobra"
)

Expand All @@ -31,7 +31,7 @@ var versionReleaseCmd = &cobra.Command{
projectKey := args[0]
version := args[1]
jiraApi.ReleaseVersion(projectKey, version)
logger.SuccessF("Success version %s from project %s released\n", version, projectKey)
logrus.Infof("Success version %s from project %s released\n", version, projectKey)
},
}

Expand Down
Loading

0 comments on commit f43e7d0

Please sign in to comment.