@@ -5,44 +5,51 @@ import (
5
5
"github.com/slack-go/slack"
6
6
)
7
7
8
- // Service structure
9
- type Service struct {
10
- Log logr.Logger
11
- APIToken string
8
+ // Service interface
9
+ type Service interface {
10
+ CreateChannel (string , bool ) (* string , error )
11
+ SetDescription (string , string ) error
12
+ SetTopic (string , string ) error
13
+ RenameChannel (string , string ) error
14
+ InviteUsers (string , []string ) error
12
15
}
13
16
14
- var api * slack.Client
17
+ // SlackService structure
18
+ type SlackService struct {
19
+ log logr.Logger
20
+ api * slack.Client
21
+ }
15
22
16
- func (s * Service ) init () {
17
- if api == nil {
18
- api = slack .New (s .APIToken )
23
+ // New creates a new SlackService
24
+ func New (APIToken string , logger logr.Logger ) * SlackService {
25
+ return & SlackService {
26
+ api : slack .New (APIToken ),
27
+ log : logger ,
19
28
}
20
29
}
21
30
22
31
// CreateChannel creates a public or private channel on slack with the given name
23
- func (s * Service ) CreateChannel (name string , isPrivate bool ) (* string , error ) {
24
- s .Log .Info ("Creating Slack Channel" , "name" , name , "isPrivate" , isPrivate )
25
- s .init ()
32
+ func (s * SlackService ) CreateChannel (name string , isPrivate bool ) (* string , error ) {
33
+ s .log .Info ("Creating Slack Channel" , "name" , name , "isPrivate" , isPrivate )
26
34
27
- channel , err := api .CreateConversation (name , isPrivate )
35
+ channel , err := s . api .CreateConversation (name , isPrivate )
28
36
if err != nil {
29
- s .Log .Error (err , "Error Creating channel" , "name" , name )
37
+ s .log .Error (err , "Error Creating channel" , "name" , name )
30
38
return nil , err
31
39
}
32
40
33
- s .Log .V (1 ).Info ("Created Slack Channel" , "channel" , channel )
41
+ s .log .V (1 ).Info ("Created Slack Channel" , "channel" , channel )
34
42
35
43
return & channel .ID , nil
36
44
}
37
45
38
46
// SetDescription sets description/"purpose" of the slack channel
39
- func (s * Service ) SetDescription (channelID string , description string ) error {
40
- log := s .Log .WithValues ("channelID" , channelID )
41
- s .init ()
47
+ func (s * SlackService ) SetDescription (channelID string , description string ) error {
48
+ log := s .log .WithValues ("channelID" , channelID )
42
49
43
50
log .V (1 ).Info ("Setting Description of the Slack Channel" )
44
51
45
- _ , err := api .SetPurposeOfConversation (channelID , description )
52
+ _ , err := s . api .SetPurposeOfConversation (channelID , description )
46
53
47
54
if err != nil {
48
55
log .Error (err , "Error setting description of the channel" )
@@ -52,13 +59,12 @@ func (s *Service) SetDescription(channelID string, description string) error {
52
59
}
53
60
54
61
// SetTopic sets "topic" of the slack channel
55
- func (s * Service ) SetTopic (channelID string , topic string ) error {
56
- log := s .Log .WithValues ("channelID" , channelID )
57
- s .init ()
62
+ func (s * SlackService ) SetTopic (channelID string , topic string ) error {
63
+ log := s .log .WithValues ("channelID" , channelID )
58
64
59
65
log .V (1 ).Info ("Setting Topic of the Slack Channel" )
60
66
61
- _ , err := api .SetTopicOfConversation (channelID , topic )
67
+ _ , err := s . api .SetTopicOfConversation (channelID , topic )
62
68
63
69
if err != nil {
64
70
log .Error (err , "Error setting topic of the channel" )
@@ -68,13 +74,12 @@ func (s *Service) SetTopic(channelID string, topic string) error {
68
74
}
69
75
70
76
// RenameChannel renames the slack channel
71
- func (s * Service ) RenameChannel (channelID string , name string ) error {
72
- log := s .Log .WithValues ("channelID" , channelID )
73
- s .init ()
77
+ func (s * SlackService ) RenameChannel (channelID string , newName string ) error {
78
+ log := s .log .WithValues ("channelID" , channelID )
74
79
75
- log .V (1 ).Info ("Renaming Slack Channel" , "name " , name )
80
+ log .V (1 ).Info ("Renaming Slack Channel" , "newName " , newName )
76
81
77
- _ , err := api .RenameConversation (channelID , name )
82
+ _ , err := s . api .RenameConversation (channelID , newName )
78
83
79
84
if err != nil {
80
85
log .Error (err , "Error renaming channel" )
@@ -84,20 +89,19 @@ func (s *Service) RenameChannel(channelID string, name string) error {
84
89
}
85
90
86
91
// InviteUsers invites users to the slack channel
87
- func (s * Service ) InviteUsers (channelID string , userEmails []string ) error {
88
- log := s .Log .WithValues ("channelID" , channelID )
89
- s .init ()
92
+ func (s * SlackService ) InviteUsers (channelID string , userEmails []string ) error {
93
+ log := s .log .WithValues ("channelID" , channelID )
90
94
91
95
for _ , email := range userEmails {
92
- user , err := api .GetUserByEmail (email )
96
+ user , err := s . api .GetUserByEmail (email )
93
97
94
98
if err != nil {
95
- s . Log .Error (err , "Error getting user by email" )
99
+ log .Error (err , "Error getting user by email" )
96
100
return err
97
101
}
98
102
99
103
log .V (1 ).Info ("Inviting user to Slack Channel" , "userID" , user .ID )
100
- _ , err = api .InviteUsersToConversation (channelID , user .ID )
104
+ _ , err = s . api .InviteUsersToConversation (channelID , user .ID )
101
105
102
106
if err != nil {
103
107
log .Error (err , "Error Inviting user to channel" , "userID" , user .ID )
0 commit comments