Skip to content

Commit

Permalink
feat: Use color for print messages
Browse files Browse the repository at this point in the history
Resolves: #3
Related to: #1
  • Loading branch information
erkenes committed Oct 12, 2022
1 parent 54b013e commit 55b94ca
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 152 deletions.
13 changes: 6 additions & 7 deletions ask-confirmation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package main

import (
"fmt"
"log"
)

func askForConfirmation() bool {
func askForConfirmation(withPrefix bool) bool {
var response string
_, err := fmt.Scan(&response)
if err != nil {
log.Fatal(err)
if withPrefix == true {
fmt.Printf("> " + colorGreen)
}
fmt.Scanln(&response)
okayResponses := []string{"y", "Y", "yes", "Yes", "YES"}
nokayResponses := []string{"n", "N", "no", "No", "NO"}
if containsString(okayResponses, response) {
return true
} else if containsString(nokayResponses, response) {
return false
} else {
fmt.Println("Please type yes or no and then press enter:")
return askForConfirmation()
fmt.Println(colorRed + "Please type yes or no and then press enter:" + colorReset)
return askForConfirmation(withPrefix)
}
}

Expand Down
36 changes: 22 additions & 14 deletions authelia.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ type AutheliaConfigStruct struct {
Notifier AutheliaNotifierStruct `yaml:"notifier"`
}

func createAutheliaConfig(rootDomain string, smtpUsername string, smtpHost string, smtpPort int, smptSender string, smtpStartupAddress string, policyForRootDomain string) []byte {
func createAutheliaConfig() {
policyRootDomain := "one_factor"

if policyForRootDomain == "two_factor" {
if configPolicyForRootDomain == "two_factor" {
policyRootDomain = "two_factor"
}
if policyForRootDomain == "bypass" {
if configPolicyForRootDomain == "bypass" {
policyRootDomain = "bypass"
}
autheliaConfig := AutheliaConfigStruct{
Expand All @@ -160,7 +160,7 @@ func createAutheliaConfig(rootDomain string, smtpUsername string, smtpHost strin
FilePath: "/config/authelia.log",
KeepStdout: true,
},
DefaultRedirectionUrl: "https://authelia." + rootDomain,
DefaultRedirectionUrl: "https://authelia." + configRootDomain,
Ntp: AutheliaNtpStruct{
Address: "time.cloudflare.com:123",
Version: 3,
Expand Down Expand Up @@ -204,15 +204,15 @@ func createAutheliaConfig(rootDomain string, smtpUsername string, smtpHost strin
DefaultPolicy: "deny",
Rules: []AutheliaAccessControlRulesStruct{
{
Domain: []string{"authelia." + rootDomain},
Domain: []string{"authelia." + configRootDomain},
Policy: "bypass",
},
{
Domain: []string{rootDomain},
Domain: []string{configRootDomain},
Policy: policyRootDomain,
},
{
Domain: []string{"traefik." + rootDomain},
Domain: []string{"traefik." + configRootDomain},
Policy: "two_factor",
Subject: []string{"group:admins", "group:traefik"},
},
Expand All @@ -229,7 +229,7 @@ func createAutheliaConfig(rootDomain string, smtpUsername string, smtpHost strin
Name: "authelia_session",
Expiration: 3600,
Inactivity: 300,
Domain: rootDomain,
Domain: configRootDomain,
},
Regulation: AutheliaRegulationStruct{
MaxRetries: 3,
Expand Down Expand Up @@ -258,12 +258,12 @@ func createAutheliaConfig(rootDomain string, smtpUsername string, smtpHost strin
Notifier: AutheliaNotifierStruct{
DisableStartupCheck: false,
Smtp: AutheliaNotifierSmtpStruct{
Username: smtpUsername,
Host: smtpHost,
Port: smtpPort,
Sender: "Authelia <" + smptSender + ">",
Username: configSmtpUsername,
Host: configSmtpHost,
Port: configSmtpPort,
Sender: "Authelia <" + configSmtpSender + ">",
Subject: "[Authelia] {title}",
StartupCheckAddress: smtpStartupAddress,
StartupCheckAddress: configSmtpStartupAddress,
},
},
}
Expand All @@ -274,5 +274,13 @@ func createAutheliaConfig(rootDomain string, smtpUsername string, smtpHost strin
fmt.Printf("Error while Marshaling. %v", err)
}

return yamlData
writeFile("AppData/authelia", "configuration.yml", yamlData, 0644)

// Secrets
writeFile("secrets", "authelia_notifier_smtp_password", []byte(configSmtpPassword), 0600)
writeFile("secrets", "authelia_jwt_secret", []byte(randomString(32)), 0600)
writeFile("secrets", "authelia_session_secret", []byte(randomString(32)), 0600)
writeFile("secrets", "authelia_storage_encryption_key", []byte(randomString(32)), 0600)
writeFile("secrets", "mysql_password", []byte(randomString(16)), 0600)
writeFile("secrets", "mysql_root_password", []byte(randomString(16)), 0600)
}
Binary file modified build/traefik-installer
Binary file not shown.
Binary file modified build/traefik-installer.exe
Binary file not shown.
13 changes: 7 additions & 6 deletions docker-compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"gopkg.in/yaml.v3"
)

func createDockerComposeFile(useTraefikHub bool, useCloudflare bool, isLocalEnvironment bool, secureRootDomain bool) []byte {
func createDockerComposeFile() {

type IpamConfigSubnetStruct struct {
Subnet string `yaml:"subnet"`
Expand Down Expand Up @@ -152,7 +152,7 @@ func createDockerComposeFile(useTraefikHub bool, useCloudflare bool, isLocalEnvi
},
}

if useTraefikHub == true {
if configUseTraefikHub == true {
traefikHubServiceDef := ServiceStruct{
ContainerName: "traefik-hub-agent",
Image: "ghcr.io/traefik/hub-agent-traefik:v0.7.2",
Expand Down Expand Up @@ -182,7 +182,7 @@ func createDockerComposeFile(useTraefikHub bool, useCloudflare bool, isLocalEnvi
traefikProxyPorts = append(traefikProxyPorts, "0.0.0.0:9900:9900", "0.0.0.0:9901:9901")
}

if useCloudflare == true {
if configUseCloudflare == true {
cloudflareSecrets := SecretsStruct{
CfEmail: SecretStruct{
File: "./secrets/cf_email",
Expand All @@ -198,11 +198,11 @@ func createDockerComposeFile(useTraefikHub bool, useCloudflare bool, isLocalEnvi
mergo.Merge(&secrets, cloudflareSecrets)
}

if isLocalEnvironment == true {
if configIsLocalEnvironment == true {
traefikProxyVolumes = append(traefikProxyVolumes, "./certs:/certs:ro")
}

if secureRootDomain == true {
if configSecureRootDomain == true {
traefikProxyLabels = append(traefikProxyLabels, "traefik.http.routers.traefik-rtr.middlewares=chain-authelia@file")
}

Expand Down Expand Up @@ -438,5 +438,6 @@ func createDockerComposeFile(useTraefikHub bool, useCloudflare bool, isLocalEnvi
if err != nil {
fmt.Printf("Error while Marshaling. %v", err)
}
return yamlData

writeFile(".", "docker-compose.yml", yamlData, 0644)
}
10 changes: 10 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"os"
)

const colorReset = "\033[0m"

const colorRed = "\033[31m"
const colorGreen = "\033[32m"
const colorYellow = "\033[33m"
const colorBlue = "\033[34m"
const colorPurple = "\033[35m"
const colorCyan = "\033[36m"
const colorWhite = "\033[37m"

func writeFile(filepath string, filename string, content []byte, filePerm os.FileMode) {
fullFilepath := filename

Expand Down
47 changes: 47 additions & 0 deletions inputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"strconv"
)

func getTextInput(question string, required bool) string {
var output string

fmt.Println(question)
fmt.Printf("> " + colorGreen)
fmt.Scanln(&output)
fmt.Println(colorReset)

if required == true && output == "" {
fmt.Println(colorRed + "An empty value is not allowed here" + colorReset)
output = getTextInput(question, required)
}

return output
}

func getNumberInput(question string, required bool) int {
var output int
var configSmtpPortError error

output, configSmtpPortError = strconv.Atoi(getTextInput(question, required))
if configSmtpPortError != nil {
fmt.Println(colorRed + "Please enter a valid port" + colorReset)
output = getNumberInput(question, required)
}

return output
}

func getConfirmInput(question string) bool {
fmt.Println(question)
output := askForConfirmation(true)
fmt.Println(colorReset)

return output
}

func printSectionHeader(title string) {
fmt.Println("\n\n\n --- " + colorCyan + title + colorReset + " --- \n")
}
Loading

0 comments on commit 55b94ca

Please sign in to comment.