Skip to content

Commit fa3c60f

Browse files
committed
[chore] added global verbose flag
- use verbose output in login command and requests class - add new printer method for debug output
1 parent bd6db25 commit fa3c60f

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

cmd/login.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ func login(_ *cobra.Command, _ []string) {
4141
var token string
4242

4343
for {
44-
fmt.Print("OpenProject host URL: ")
44+
printer.Debug(Verbose, "Parsing host URL ...")
45+
printer.Input("OpenProject host URL: ")
46+
4547
ok, msg, host := parseHostUrl()
4648
if !ok {
47-
fmt.Println(msg)
49+
printer.ErrorText(msg)
4850
continue
4951
}
5052

51-
requests.Init(host, "")
53+
printer.Debug(Verbose, "Initializing requests client ...")
54+
requests.Init(host, "", Verbose)
5255
ok = checkOpenProjectApi()
5356
if !ok {
54-
fmt.Println(noOpInstanceError)
57+
printer.ErrorText(noOpInstanceError)
5558
continue
5659
}
5760

@@ -69,7 +72,7 @@ func login(_ *cobra.Command, _ []string) {
6972

7073
token = common.SanitizeLineBreaks(t)
7174

72-
requests.Init(hostUrl, token)
75+
requests.Init(hostUrl, token, Verbose)
7376
user, err := users.Me()
7477
if err != nil {
7578
printer.Error(err)
@@ -92,24 +95,39 @@ func parseHostUrl() (ok bool, errMessage string, host *url.URL) {
9295

9396
input, err := reader.ReadString('\n')
9497
if err != nil {
98+
printer.Debug(Verbose, fmt.Sprintf("Error reading string input: %+v", err))
9599
return false, urlInputError, nil
96100
}
97101

102+
printer.Debug(Verbose, fmt.Sprintf("Parsed input %q.", input))
103+
printer.Debug(Verbose, "Sanitizing input ...")
104+
98105
input = common.SanitizeLineBreaks(input)
99106
input = strings.TrimSuffix(input, "/")
107+
108+
printer.Debug(Verbose, fmt.Sprintf("Sanitized input '%s'.", input))
109+
printer.Debug(Verbose, "Parsing input as url ...")
110+
100111
parsed, err := url.Parse(input)
101112
if err != nil {
113+
printer.Debug(Verbose, fmt.Sprintf("Error parsing url: %+v", err))
102114
return false, urlInputError, nil
103115
}
104116

117+
printer.Debug(Verbose, fmt.Sprintf("Parsed url '%s'.", parsed))
118+
printer.Debug(Verbose, "Checking for http host and scheme ...")
119+
105120
if parsed.Scheme == "" || parsed.Host == "" {
106121
return false, missingSchemeError, nil
107122
}
108123

124+
printer.Debug(Verbose, "Parsing input successful, continuing with next steps.")
109125
return true, "", parsed
110126
}
111127

112128
func checkOpenProjectApi() bool {
129+
printer.Debug(Verbose, "Fetching API root to check for instance configuration ...")
130+
113131
response, err := requests.Get(paths.Root(), nil)
114132
if err != nil {
115133
return false

cmd/root.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/opf/openproject-cli/components/routes"
2222
)
2323

24+
var Verbose bool
2425
var showVersionFlag bool
2526

2627
var rootCmd = &cobra.Command{
@@ -70,7 +71,7 @@ func init() {
7071
printer.Error(err)
7172
}
7273

73-
requests.Init(parse, token)
74+
requests.Init(parse, token, Verbose)
7475
routes.Init(parse)
7576

7677
rootCmd.Flags().BoolVarP(
@@ -81,6 +82,14 @@ func init() {
8182
"Show version information of the OpenProject CLI",
8283
)
8384

85+
rootCmd.PersistentFlags().BoolVarP(
86+
&Verbose,
87+
"verbose",
88+
"",
89+
false,
90+
"Print verbose information of any process that supports this output.",
91+
)
92+
8493
rootCmd.AddCommand(
8594
loginCmd,
8695
list.RootCmd,

components/printer/common.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package printer
22

33
import (
44
"encoding/json"
5+
56
"github.com/opf/openproject-cli/components/errors"
67
)
78

@@ -15,6 +16,10 @@ func Info(msg string) {
1516
activePrinter.Println(msg)
1617
}
1718

19+
func Input(prompt string) {
20+
activePrinter.Printf(prompt)
21+
}
22+
1823
func Done() {
1924
activePrinter.Println(Green("DONE"))
2025
}
@@ -29,6 +34,12 @@ func Error(err error) {
2934
}
3035
}
3136

37+
func Debug(verboseFlag bool, msg string) {
38+
if verboseFlag {
39+
activePrinter.Printf("%s %s\n", Magenta("[DEBUG]"), msg)
40+
}
41+
}
42+
3243
func ErrorText(msg string) {
3344
activePrinter.Printf("%s %s\n", Red("[ERROR]"), msg)
3445
}

components/requests/requests.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package requests
22

33
import (
4+
"fmt"
45
"io"
56
"net/http"
67
"net/url"
@@ -13,16 +14,18 @@ import (
1314
var client *http.Client
1415
var host *url.URL
1516
var token string
17+
var verbose bool
1618

1719
type RequestData struct {
1820
ContentType string
1921
Body io.Reader
2022
}
2123

22-
func Init(hostUrl *url.URL, tokenValue string) {
24+
func Init(hostUrl *url.URL, tokenValue string, verboseFlag bool) {
2325
client = &http.Client{}
2426
host = hostUrl
2527
token = tokenValue
28+
verbose = verboseFlag
2629
}
2730

2831
func Get(path string, query *Query) (responseBody []byte, err error) {
@@ -44,6 +47,12 @@ func Patch(path string, requestBody *RequestData) (responseBody []byte, err erro
4447
}
4548

4649
func Do(method string, path string, query *Query, requestData *RequestData) (responseBody []byte, err error) {
50+
printer.Debug(verbose, "Building HTTP request:")
51+
printer.Debug(verbose, fmt.Sprintf("\twith Method: %s", method))
52+
printer.Debug(verbose, fmt.Sprintf("\twith Path: %s", path))
53+
printer.Debug(verbose, fmt.Sprintf("\twith Query: %+v", query))
54+
printer.Debug(verbose, fmt.Sprintf("\twith Body: %+v", requestData))
55+
4756
if client == nil || hostUnitialised() {
4857
return nil, errors.Custom("Cannot execute requests without initializing request client first. Run `op login`")
4958
}
@@ -76,6 +85,8 @@ func Do(method string, path string, query *Query, requestData *RequestData) (res
7685
request.SetBasicAuth("apikey", token)
7786
}
7887

88+
printer.Debug(verbose, fmt.Sprintf("Running HTTP request %s %s", request.Method, request.URL))
89+
7990
resp, err := client.Do(request)
8091
if err != nil {
8192
return nil, err
@@ -87,6 +98,8 @@ func Do(method string, path string, query *Query, requestData *RequestData) (res
8798
return nil, err
8899
}
89100

101+
printer.Debug(verbose, fmt.Sprintf("Received response:\n%s", response))
102+
90103
if !isSuccess(resp.StatusCode) {
91104
return nil, errors.NewResponseError(resp.StatusCode, response)
92105
}

0 commit comments

Comments
 (0)