-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[teamup-rocketchat] Added support for custom config file and log path…
… location via cli parameters (#13)
- Loading branch information
Showing
8 changed files
with
351 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"path" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Configuration holds different options | ||
// required to run the bot | ||
type Configuration struct { | ||
URL string `yaml:"URL"` | ||
Username string `yaml:"USERNAME"` | ||
Password string `yaml:"PASSWORD"` | ||
UseSSL bool `yaml:"USE_SSL"` | ||
Room string `yaml:"ROOM"` | ||
MeetingsCode string `yaml:"MEETINGS_TEAMUP"` | ||
TeamupToken string `yaml:"TOKEN_TEAMUP"` | ||
RepeatIn int `yaml:"REPEAT_IN"` | ||
LogPath string `yaml:"LOG_PATH"` | ||
LogFileName string `yaml:"LOG_FILE_NAME"` | ||
} | ||
|
||
// Prints beatiful | ||
func (config Configuration) String() string { | ||
return fmt.Sprintf( | ||
"URL:%s\nUsername:%v\nPassword:%s\nUseSSL:%v\nRoom:%s\nMeetingsCode:%s\nTeamupToken:%s\nRepeatIn:%d\nLogPath:%s\nLogFileName:%s\n", | ||
config.URL, | ||
config.Username, | ||
"hidden-for-security-purpose", | ||
config.UseSSL, | ||
config.Room, | ||
"hidden-for-security-purpose", | ||
"hidden-for-security-purpose", | ||
config.RepeatIn, | ||
config.LogPath, | ||
config.LogFileName, | ||
) | ||
|
||
} | ||
|
||
// IsUrl does basic url checking | ||
// Scheme must not be empty | ||
func isUrl(str string) bool { | ||
u, err := url.Parse(str) | ||
return err == nil && u.Scheme != "" && u.Host != "" && (u.Scheme == "https" || u.Scheme == "http") | ||
} | ||
|
||
// checkValidity checks the validity of the loaded configuration | ||
// and returns error if any field value is not valid | ||
func (config *Configuration) checkValidity(isCustomPath bool) (*Configuration, error) { | ||
if !isUrl(config.URL) { | ||
return nil, fmt.Errorf("invalid URL: %s", config.URL) | ||
} | ||
|
||
if len(config.Username) == 0 { | ||
return nil, fmt.Errorf("empty USERNAME field") | ||
} | ||
|
||
if len(config.Password) == 0 { | ||
return nil, fmt.Errorf("empty PASSWORD field") | ||
} | ||
|
||
if len(config.Room) == 0 { | ||
return nil, fmt.Errorf("empty ROOM field") | ||
} | ||
|
||
if len(config.MeetingsCode) == 0 { | ||
return nil, fmt.Errorf("empty MEETINGS_TEAMUP field") | ||
} | ||
|
||
if len(config.TeamupToken) == 0 { | ||
return nil, fmt.Errorf("empty TOKEN_TEAMUP field") | ||
} | ||
|
||
if len(config.Room) == 0 { | ||
return nil, fmt.Errorf("empty ROOM field") | ||
} | ||
// if only custom path is not provided | ||
if !isCustomPath { | ||
if len(config.LogPath) == 0 { | ||
// set to default | ||
config.LogPath = defaultLogFilePath | ||
} else { | ||
config.LogPath = path.Clean(config.LogPath) // Clean the path user provided location only | ||
} | ||
// Check if the log output folder is accessible | ||
_, err := os.Stat(config.LogPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("the log output folder %s is inaccessible because %s", config.LogPath, err.Error()) | ||
} | ||
} | ||
|
||
if len(config.LogFileName) == 0 { | ||
// set to default | ||
config.LogFileName = defaultLogFileName | ||
} | ||
|
||
if config.RepeatIn == 0 { | ||
// set to default | ||
config.RepeatIn = defaultRepeatIn | ||
} | ||
return config, nil | ||
} | ||
|
||
// readConfig reads config.yml files | ||
// and returns config or | ||
// corresponding error if any | ||
func readConfig(filePath string, isCustomPath bool) (*Configuration, error) { | ||
config := Configuration{} | ||
|
||
yamlData, err := os.ReadFile(filePath) | ||
|
||
// Check for errors | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = yaml.Unmarshal(yamlData, &config) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
validconfig, err := config.checkValidity(isCustomPath) | ||
|
||
if err != nil { | ||
return &config, err | ||
} | ||
|
||
return validconfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// banner for the app | ||
const bannerText = ` | ||
░░░░▒█░█▀▀▄░█▀▀▄░█░▄░█▀▀▄░█▀▀▄░░▀░░▀▀█▀▀░█▀▀░█▀▄░█░░░ | ||
░░░░▒█░█▄▄█░█░▒█░█▀▄░█▄▄█░█▄▄▀░░█▀░░▒█░░░█▀▀░█░░░█▀▀█ | ||
░▒█▄▄█░▀░░▀░▀░░▀░▀░▀░▀░░▀░▀░▀▀░▀▀▀░░▒█░░░▀▀▀░▀▀▀░▀░░▀ | ||
` | ||
const about = "About:\tA bot that fetches events from teamup calendar and notifies gophers of JankariTech through rocket.chat" | ||
|
||
// displayHelpMessage displays help message | ||
func displayHelpMessage(bannerText string) { | ||
|
||
msg := "Usage: teamup-rocketchat-bot --option <value>\n\n" | ||
msg += "Options:\n" | ||
msg += "-h, --help \t Show this screen\n" | ||
msg += "--logpath\t Set the custom logpath. It overrides the log path specified in configuration (config.yml) file\n" | ||
msg += "--config\t Point to the configuration (config) file. It overrides the default configuration file located at app directory\n" | ||
msg += "\nExample:\n" | ||
msg += "teamup-rocketchat-bot --config /home/user/.config/bot/config.yml\n\n\tHere, the bot will read configuration from the pointed file\n\n" | ||
msg += "teamup-rocketchat-bot --logpath /tmp/\n\n\tHere, the configuration (config.yml) will be read from default path (i.e the app directory) and\n\t the log path provided will override the path specified in configuration file\n\n" | ||
msg += "teamup-rocketchat-bot --config /home/user/.config/bot/config.yml --logpath /tmp/\n\n\tHere, the configuration (config.yml) will be read from the pointed file and\n\t the log path provided will override the path specified in configuration file\n\n" | ||
msg += "\nNote:\tMake sure the app has necessary premissions required to read or write at custom locations provided\n" | ||
msg += "\tAlso, trying to run the bot without providing config.yml at app directory and without providing --config switch will result in failure of application start.\n" | ||
fmt.Printf("%s\n%s\n%s\n", bannerText, about, msg) | ||
} | ||
|
||
func displayHelpMessageWithoutBanner() { | ||
displayHelpMessage("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.