Skip to content

Commit 8cd74e7

Browse files
committed
refactor: add debug logging support and constants
1 parent 62f1300 commit 8cd74e7

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

internal/app/gitops-commit/cmd/root.go

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

3-
import "github.com/spf13/cobra"
3+
import (
4+
"github.com/google/martian/log"
5+
"github.com/spf13/cobra"
6+
"os"
7+
)
48

59
func NewRootCommand() *cobra.Command {
610
c := cobra.Command{
@@ -11,6 +15,12 @@ func NewRootCommand() *cobra.Command {
1115
},
1216
}
1317

18+
d := os.Getenv("DEBUG")
19+
20+
if len(d) > 0 {
21+
log.SetLevel(log.Debug)
22+
}
23+
1424
c.AddCommand(newRunCommand())
1525
c.AddCommand(newServeCommand())
1626

internal/app/gitops-commit/cmd/serve.go

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

33
import (
44
"fmt"
5+
"github.com/google/martian/log"
56
"github.com/gsdevme/gitops-commit/internal/app/gitops-commit/slackhttp"
67
"github.com/gsdevme/gitops-commit/internal/pkg/gitops"
78
"github.com/spf13/cobra"
@@ -40,7 +41,7 @@ func newServeCommand() *cobra.Command {
4041
ReadTimeout: 3 * time.Second,
4142
WriteTimeout: 5 * time.Second,
4243
ConnState: func(conn net.Conn, state http.ConnState) {
43-
//fmt.Fprintf(os.Stdout, "%s - %s\n", conn.RemoteAddr(), state.String())
44+
log.Debugf("%s - %s\n", conn.RemoteAddr(), state.String())
4445
},
4546
}
4647

internal/app/gitops-commit/slackhttp/handler.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ package slackhttp
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/google/martian/log"
67
"github.com/gsdevme/gitops-commit/internal/pkg/gitops"
78
"github.com/slack-go/slack"
89
"net/http"
910
"strings"
1011
)
1112

13+
const (
14+
SlackUnknownResponse = "Incorrect usage, expected /gitops-commit [command] [name] [tag]"
15+
SlackUnknownCommandResponse = "Unknown command '%s', expected /gitops-commit [command] [name] [tag]"
16+
)
17+
1218
func (s *server) handleSlackCommand(registry *NamedRepositoryRegistry) func(http.ResponseWriter, slack.SlashCommand) {
1319
return func(w http.ResponseWriter, sl slack.SlashCommand) {
1420
text := strings.Split(sl.Text, " ")
1521

1622
if len(text) < 3 || len(text) > 3 {
17-
respondSlack("Incorrect usage, expected /gitops-commit [command] [name] [tag]", slack.ResponseTypeEphemeral, w)
23+
respondSlack(SlackUnknownResponse, slack.ResponseTypeEphemeral, w)
1824

1925
return
2026
}
@@ -28,7 +34,7 @@ func (s *server) handleSlackCommand(registry *NamedRepositoryRegistry) func(http
2834
return
2935
default:
3036
respondSlack(
31-
fmt.Sprintf("Unknown command %s, expected /gitops-commit [command] [name] [tag]", command),
37+
fmt.Sprintf(SlackUnknownCommandResponse, command),
3238
slack.ResponseTypeEphemeral,
3339
w,
3440
)
@@ -73,7 +79,7 @@ func deploy(s *server, w http.ResponseWriter, registry *NamedRepositoryRegistry,
7379
go func() {
7480
err = gitops.DeployVersionHandler(command)
7581
if err != nil {
76-
respondSlack(fmt.Sprintf("failed to deploy: %s", err), slack.ResponseTypeEphemeral, w)
82+
log.Errorf("failed to deploy %w", err)
7783

7884
return
7985
}
@@ -92,7 +98,6 @@ func respondSlack(m string, t string, w http.ResponseWriter) {
9298
ResponseType: t,
9399
})
94100
if err != nil {
95-
fmt.Println(err)
96101
w.WriteHeader(http.StatusInternalServerError)
97102
return
98103
}

0 commit comments

Comments
 (0)